Java源码示例:com.google.android.exoplayer2.upstream.DataSpec.HttpMethod

示例1
/**
 * Establishes a connection, following redirects to do so where permitted.
 */
private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException {
  URL url = new URL(dataSpec.uri.toString());
  @HttpMethod int httpMethod = dataSpec.httpMethod;
  byte[] httpBody = dataSpec.httpBody;
  long position = dataSpec.position;
  long length = dataSpec.length;
  boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);

  if (!allowCrossProtocolRedirects) {
    // HttpURLConnection disallows cross-protocol redirects, but otherwise performs redirection
    // automatically. This is the behavior we want, so use it.
    return makeConnection(
        url,
        httpMethod,
        httpBody,
        position,
        length,
        allowGzip,
        /* followRedirects= */ true,
        dataSpec.httpRequestHeaders);
  }

  // We need to handle redirects ourselves to allow cross-protocol redirects.
  int redirectCount = 0;
  while (redirectCount++ <= MAX_REDIRECTS) {
    HttpURLConnection connection =
        makeConnection(
            url,
            httpMethod,
            httpBody,
            position,
            length,
            allowGzip,
            /* followRedirects= */ false,
            dataSpec.httpRequestHeaders);
    int responseCode = connection.getResponseCode();
    String location = connection.getHeaderField("Location");
    if ((httpMethod == DataSpec.HTTP_METHOD_GET || httpMethod == DataSpec.HTTP_METHOD_HEAD)
        && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE
            || responseCode == HttpURLConnection.HTTP_MOVED_PERM
            || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
            || responseCode == HttpURLConnection.HTTP_SEE_OTHER
            || responseCode == HTTP_STATUS_TEMPORARY_REDIRECT
            || responseCode == HTTP_STATUS_PERMANENT_REDIRECT)) {
      connection.disconnect();
      url = handleRedirect(url, location);
    } else if (httpMethod == DataSpec.HTTP_METHOD_POST
        && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE
            || responseCode == HttpURLConnection.HTTP_MOVED_PERM
            || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
            || responseCode == HttpURLConnection.HTTP_SEE_OTHER)) {
      // POST request follows the redirect and is transformed into a GET request.
      connection.disconnect();
      httpMethod = DataSpec.HTTP_METHOD_GET;
      httpBody = null;
      url = handleRedirect(url, location);
    } else {
      return connection;
    }
  }

  // If we get here we've been redirected more times than are permitted.
  throw new NoRouteToHostException("Too many redirects: " + redirectCount);
}
 
示例2
/**
 * Configures a connection and opens it.
 *
 * @param url The url to connect to.
 * @param httpMethod The http method.
 * @param httpBody The body data.
 * @param position The byte offset of the requested data.
 * @param length The length of the requested data, or {@link C#LENGTH_UNSET}.
 * @param allowGzip Whether to allow the use of gzip.
 * @param followRedirects Whether to follow redirects.
 * @param requestParameters parameters (HTTP headers) to include in request.
 */
private HttpURLConnection makeConnection(
    URL url,
    @HttpMethod int httpMethod,
    byte[] httpBody,
    long position,
    long length,
    boolean allowGzip,
    boolean followRedirects,
    Map<String, String> requestParameters)
    throws IOException {
  HttpURLConnection connection = openConnection(url);
  connection.setConnectTimeout(connectTimeoutMillis);
  connection.setReadTimeout(readTimeoutMillis);

  Map<String, String> requestHeaders = new HashMap<>();
  if (defaultRequestProperties != null) {
    requestHeaders.putAll(defaultRequestProperties.getSnapshot());
  }
  requestHeaders.putAll(requestProperties.getSnapshot());
  requestHeaders.putAll(requestParameters);

  for (Map.Entry<String, String> property : requestHeaders.entrySet()) {
    connection.setRequestProperty(property.getKey(), property.getValue());
  }

  if (!(position == 0 && length == C.LENGTH_UNSET)) {
    String rangeRequest = "bytes=" + position + "-";
    if (length != C.LENGTH_UNSET) {
      rangeRequest += (position + length - 1);
    }
    connection.setRequestProperty("Range", rangeRequest);
  }
  connection.setRequestProperty("User-Agent", userAgent);
  connection.setRequestProperty("Accept-Encoding", allowGzip ? "gzip" : "identity");
  connection.setInstanceFollowRedirects(followRedirects);
  connection.setDoOutput(httpBody != null);
  connection.setRequestMethod(DataSpec.getStringForHttpMethod(httpMethod));
  
  if (httpBody != null) {
    connection.setFixedLengthStreamingMode(httpBody.length);
    connection.connect();
    OutputStream os = connection.getOutputStream();
    os.write(httpBody);
    os.close();
  } else {
    connection.connect();
  }
  return connection;
}
 
示例3
/**
 * Establishes a connection, following redirects to do so where permitted.
 */
private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException {
    URL url = new URL(dataSpec.uri.toString());
    @HttpMethod int httpMethod = dataSpec.httpMethod;
    byte[] httpBody = dataSpec.httpBody;
    long position = dataSpec.position;
    long length = dataSpec.length;
    boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);

    if (!allowCrossProtocolRedirects) {
        // HttpURLConnection disallows cross-protocol redirects, but otherwise performs redirection
        // automatically. This is the behavior we want, so use it.
        return makeConnection(
                url,
                httpMethod,
                httpBody,
                position,
                length,
                allowGzip,
                /* followRedirects= */ true,
                dataSpec.httpRequestHeaders);
    }

    // We need to handle redirects ourselves to allow cross-protocol redirects.
    int redirectCount = 0;
    while (redirectCount++ <= MAX_REDIRECTS) {
        HttpURLConnection connection =
                makeConnection(
                        url,
                        httpMethod,
                        httpBody,
                        position,
                        length,
                        allowGzip,
                        /* followRedirects= */ false,
                        dataSpec.httpRequestHeaders);
        int responseCode = connection.getResponseCode();
        String location = connection.getHeaderField("Location");
        if ((httpMethod == DataSpec.HTTP_METHOD_GET || httpMethod == DataSpec.HTTP_METHOD_HEAD)
                && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE
                || responseCode == HttpURLConnection.HTTP_MOVED_PERM
                || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
                || responseCode == HttpURLConnection.HTTP_SEE_OTHER
                || responseCode == HTTP_STATUS_TEMPORARY_REDIRECT
                || responseCode == HTTP_STATUS_PERMANENT_REDIRECT)) {
            connection.disconnect();
            url = handleRedirect(url, location);
        } else if (httpMethod == DataSpec.HTTP_METHOD_POST
                && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE
                || responseCode == HttpURLConnection.HTTP_MOVED_PERM
                || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
                || responseCode == HttpURLConnection.HTTP_SEE_OTHER)) {
            // POST request follows the redirect and is transformed into a GET request.
            connection.disconnect();
            httpMethod = DataSpec.HTTP_METHOD_GET;
            httpBody = null;
            url = handleRedirect(url, location);
        } else {
            return connection;
        }
    }

    // If we get here we've been redirected more times than are permitted.
    throw new NoRouteToHostException("Too many redirects: " + redirectCount);
}
 
示例4
/**
 * Establishes a connection, following redirects to do so where permitted.
 */
private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException {
  URL url = new URL(dataSpec.uri.toString());
  @HttpMethod int httpMethod = dataSpec.httpMethod;
  byte[] httpBody = dataSpec.httpBody;
  long position = dataSpec.position;
  long length = dataSpec.length;
  boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);
  boolean allowIcyMetadata = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_ICY_METADATA);

  if (!allowCrossProtocolRedirects) {
    // HttpURLConnection disallows cross-protocol redirects, but otherwise performs redirection
    // automatically. This is the behavior we want, so use it.
    return makeConnection(
        url,
        httpMethod,
        httpBody,
        position,
        length,
        allowGzip,
        allowIcyMetadata,
        /* followRedirects= */ true);
  }

  // We need to handle redirects ourselves to allow cross-protocol redirects.
  int redirectCount = 0;
  while (redirectCount++ <= MAX_REDIRECTS) {
    HttpURLConnection connection =
        makeConnection(
            url,
            httpMethod,
            httpBody,
            position,
            length,
            allowGzip,
            allowIcyMetadata,
            /* followRedirects= */ false);
    int responseCode = connection.getResponseCode();
    String location = connection.getHeaderField("Location");
    if ((httpMethod == DataSpec.HTTP_METHOD_GET || httpMethod == DataSpec.HTTP_METHOD_HEAD)
        && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE
            || responseCode == HttpURLConnection.HTTP_MOVED_PERM
            || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
            || responseCode == HttpURLConnection.HTTP_SEE_OTHER
            || responseCode == HTTP_STATUS_TEMPORARY_REDIRECT
            || responseCode == HTTP_STATUS_PERMANENT_REDIRECT)) {
      connection.disconnect();
      url = handleRedirect(url, location);
    } else if (httpMethod == DataSpec.HTTP_METHOD_POST
        && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE
            || responseCode == HttpURLConnection.HTTP_MOVED_PERM
            || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
            || responseCode == HttpURLConnection.HTTP_SEE_OTHER)) {
      // POST request follows the redirect and is transformed into a GET request.
      connection.disconnect();
      httpMethod = DataSpec.HTTP_METHOD_GET;
      httpBody = null;
      url = handleRedirect(url, location);
    } else {
      return connection;
    }
  }

  // If we get here we've been redirected more times than are permitted.
  throw new NoRouteToHostException("Too many redirects: " + redirectCount);
}
 
示例5
/**
 * Establishes a connection, following redirects to do so where permitted.
 */
private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException {
  URL url = new URL(dataSpec.uri.toString());
  @HttpMethod int httpMethod = dataSpec.httpMethod;
  byte[] httpBody = dataSpec.httpBody;
  long position = dataSpec.position;
  long length = dataSpec.length;
  boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP);
  boolean allowIcyMetadata = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_ICY_METADATA);

  if (!allowCrossProtocolRedirects) {
    // HttpURLConnection disallows cross-protocol redirects, but otherwise performs redirection
    // automatically. This is the behavior we want, so use it.
    return makeConnection(
        url,
        httpMethod,
        httpBody,
        position,
        length,
        allowGzip,
        allowIcyMetadata,
        /* followRedirects= */ true);
  }

  // We need to handle redirects ourselves to allow cross-protocol redirects.
  int redirectCount = 0;
  while (redirectCount++ <= MAX_REDIRECTS) {
    HttpURLConnection connection =
        makeConnection(
            url,
            httpMethod,
            httpBody,
            position,
            length,
            allowGzip,
            allowIcyMetadata,
            /* followRedirects= */ false);
    int responseCode = connection.getResponseCode();
    String location = connection.getHeaderField("Location");
    if ((httpMethod == DataSpec.HTTP_METHOD_GET || httpMethod == DataSpec.HTTP_METHOD_HEAD)
        && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE
            || responseCode == HttpURLConnection.HTTP_MOVED_PERM
            || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
            || responseCode == HttpURLConnection.HTTP_SEE_OTHER
            || responseCode == HTTP_STATUS_TEMPORARY_REDIRECT
            || responseCode == HTTP_STATUS_PERMANENT_REDIRECT)) {
      connection.disconnect();
      url = handleRedirect(url, location);
    } else if (httpMethod == DataSpec.HTTP_METHOD_POST
        && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE
            || responseCode == HttpURLConnection.HTTP_MOVED_PERM
            || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
            || responseCode == HttpURLConnection.HTTP_SEE_OTHER)) {
      // POST request follows the redirect and is transformed into a GET request.
      connection.disconnect();
      httpMethod = DataSpec.HTTP_METHOD_GET;
      httpBody = null;
      url = handleRedirect(url, location);
    } else {
      return connection;
    }
  }

  // If we get here we've been redirected more times than are permitted.
  throw new NoRouteToHostException("Too many redirects: " + redirectCount);
}