当前位置: 首页>>代码示例>>Java>>正文


Java HttpHeaders.contentLength方法代码示例

本文整理汇总了Java中okhttp3.internal.http.HttpHeaders.contentLength方法的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaders.contentLength方法的具体用法?Java HttpHeaders.contentLength怎么用?Java HttpHeaders.contentLength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在okhttp3.internal.http.HttpHeaders的用法示例。


在下文中一共展示了HttpHeaders.contentLength方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createOkBody

import okhttp3.internal.http.HttpHeaders; //导入方法依赖的package包/类
/**
 * Creates an OkHttp Response.Body containing the supplied information.
 */
private static ResponseBody createOkBody(final Headers okHeaders,
    final CacheResponse cacheResponse) throws IOException {
  final BufferedSource body = Okio.buffer(Okio.source(cacheResponse.getBody()));
  return new ResponseBody() {
    @Override
    public MediaType contentType() {
      String contentTypeHeader = okHeaders.get("Content-Type");
      return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader);
    }

    @Override
    public long contentLength() {
      return HttpHeaders.contentLength(okHeaders);
    }

    @Override public BufferedSource source() {
      return body;
    }
  };
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:24,代码来源:JavaApiConverter.java

示例2: getTransferStream

import okhttp3.internal.http.HttpHeaders; //导入方法依赖的package包/类
private Source getTransferStream(Response response) throws IOException {
  if (!HttpHeaders.hasBody(response)) {
    return newFixedLengthSource(0);
  }

  if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) {
    return newChunkedSource(response.request().url());
  }

  long contentLength = HttpHeaders.contentLength(response);
  if (contentLength != -1) {
    return newFixedLengthSource(contentLength);
  }

  // Wrap the input stream from the connection (rather than just returning
  // "socketIn" directly here), so that we can control its use after the
  // reference escapes.
  return newUnknownLengthSource();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:20,代码来源:Http1Codec.java

示例3: openResponseBody

import okhttp3.internal.http.HttpHeaders; //导入方法依赖的package包/类
@Override public ResponseBody openResponseBody(Response response) throws IOException {
  streamAllocation.eventListener.responseBodyStart(streamAllocation.call);
  String contentType = response.header("Content-Type");
  long contentLength = HttpHeaders.contentLength(response);
  Source source = new StreamFinishingSource(stream.getSource());
  return new RealResponseBody(contentType, contentLength, Okio.buffer(source));
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:8,代码来源:Http2Codec.java

示例4: createTunnel

import okhttp3.internal.http.HttpHeaders; //导入方法依赖的package包/类
/**
 * To make an HTTPS connection over an HTTP proxy, send an unencrypted CONNECT request to create
 * the proxy connection. This may need to be retried if the proxy requires authorization.
 */
private Request createTunnel(int readTimeout, int writeTimeout, Request tunnelRequest,
    HttpUrl url) throws IOException {
  // Make an SSL Tunnel on the first message pair of each SSL + proxy connection.
  String requestLine = "CONNECT " + Util.hostHeader(url, true) + " HTTP/1.1";
  while (true) {
    Http1Codec tunnelConnection = new Http1Codec(null, null, source, sink);
    source.timeout().timeout(readTimeout, MILLISECONDS);
    sink.timeout().timeout(writeTimeout, MILLISECONDS);
    tunnelConnection.writeRequest(tunnelRequest.headers(), requestLine);
    tunnelConnection.finishRequest();
    Response response = tunnelConnection.readResponseHeaders(false)
        .request(tunnelRequest)
        .build();
    // The response body from a CONNECT should be empty, but if it is not then we should consume
    // it before proceeding.
    long contentLength = HttpHeaders.contentLength(response);
    if (contentLength == -1L) {
      contentLength = 0L;
    }
    Source body = tunnelConnection.newFixedLengthSource(contentLength);
    Util.skipAll(body, Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
    body.close();

    switch (response.code()) {
      case HTTP_OK:
        // Assume the server won't send a TLS ServerHello until we send a TLS ClientHello. If
        // that happens, then we will have buffered bytes that are needed by the SSLSocket!
        // This check is imperfect: it doesn't tell us whether a handshake will succeed, just
        // that it will almost certainly fail because the proxy has sent unexpected data.
        if (!source.buffer().exhausted() || !sink.buffer().exhausted()) {
          throw new IOException("TLS tunnel buffered too many bytes!");
        }
        return null;

      case HTTP_PROXY_AUTH:
        tunnelRequest = route.address().proxyAuthenticator().authenticate(route, response);
        if (tunnelRequest == null) throw new IOException("Failed to authenticate with proxy");

        if ("close".equalsIgnoreCase(response.header("Connection"))) {
          return tunnelRequest;
        }
        break;

      default:
        throw new IOException(
            "Unexpected response code for CONNECT: " + response.code());
    }
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:54,代码来源:RealConnection.java

示例5: contentLength

import okhttp3.internal.http.HttpHeaders; //导入方法依赖的package包/类
public static long contentLength(Response<?> response) {
    return HttpHeaders.contentLength(response.headers());
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:4,代码来源:Utils.java

示例6: createTunnel

import okhttp3.internal.http.HttpHeaders; //导入方法依赖的package包/类
/**
 * To make an HTTPS connection over an HTTP proxy, send an unencrypted CONNECT request to create
 * the proxy connection. This may need to be retried if the proxy requires authorization.
 */
private Request createTunnel(int readTimeout, int writeTimeout, Request tunnelRequest,
    HttpUrl url) throws IOException {
  // Make an SSL Tunnel on the first message pair of each SSL + proxy connection.
  String requestLine = "CONNECT " + Util.hostHeader(url, true) + " HTTP/1.1";
  while (true) {
    Http1Codec tunnelConnection = new Http1Codec(null, null, source, sink);
    source.timeout().timeout(readTimeout, MILLISECONDS);
    sink.timeout().timeout(writeTimeout, MILLISECONDS);
    tunnelConnection.writeRequest(tunnelRequest.headers(), requestLine);
    tunnelConnection.finishRequest();
    Response response = tunnelConnection.readResponse().request(tunnelRequest).build();
    // The response body from a CONNECT should be empty, but if it is not then we should consume
    // it before proceeding.
    long contentLength = HttpHeaders.contentLength(response);
    if (contentLength == -1L) {
      contentLength = 0L;
    }
    Source body = tunnelConnection.newFixedLengthSource(contentLength);
    Util.skipAll(body, Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
    body.close();

    switch (response.code()) {
      case HTTP_OK:
        // Assume the server won't send a TLS ServerHello until we send a TLS ClientHello. If
        // that happens, then we will have buffered bytes that are needed by the SSLSocket!
        // This check is imperfect: it doesn't tell us whether a handshake will succeed, just
        // that it will almost certainly fail because the proxy has sent unexpected data.
        if (!source.buffer().exhausted() || !sink.buffer().exhausted()) {
          throw new IOException("TLS tunnel buffered too many bytes!");
        }
        return null;

      case HTTP_PROXY_AUTH:
        tunnelRequest = route.address().proxyAuthenticator().authenticate(route, response);
        if (tunnelRequest == null) throw new IOException("Failed to authenticate with proxy");

        if ("close".equalsIgnoreCase(response.header("Connection"))) {
          return tunnelRequest;
        }
        break;

      default:
        throw new IOException(
            "Unexpected response code for CONNECT: " + response.code());
    }
  }
}
 
开发者ID:RunningTheSnail,项目名称:Okhttp,代码行数:52,代码来源:RealConnection.java


注:本文中的okhttp3.internal.http.HttpHeaders.contentLength方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。