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


Java Http1Codec.finishRequest方法代码示例

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


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

示例1: createTunnel

import okhttp3.internal.http1.Http1Codec; //导入方法依赖的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

示例2: createTunnel

import okhttp3.internal.http1.Http1Codec; //导入方法依赖的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.http1.Http1Codec.finishRequest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。