當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。