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


Java HttpHeaders类代码示例

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


HttpHeaders类属于okhttp3.internal.http包,在下文中一共展示了HttpHeaders类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: readChunkSize

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
private void readChunkSize() throws IOException {
  // Read the suffix of the previous chunk.
  if (bytesRemainingInChunk != NO_CHUNK_YET) {
    source.readUtf8LineStrict();
  }
  try {
    bytesRemainingInChunk = source.readHexadecimalUnsignedLong();
    String extensions = source.readUtf8LineStrict().trim();
    if (bytesRemainingInChunk < 0 || (!extensions.isEmpty() && !extensions.startsWith(";"))) {
      throw new ProtocolException("expected chunk size and optional extensions but was \""
          + bytesRemainingInChunk + extensions + "\"");
    }
  } catch (NumberFormatException e) {
    throw new ProtocolException(e.getMessage());
  }
  if (bytesRemainingInChunk == 0L) {
    hasMoreChunks = false;
    HttpHeaders.receiveHeaders(client.cookieJar(), url, readHeaders());
    endOfInput(true);
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:22,代码来源:Http1Codec.java

示例4: readChunkSize

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
private void readChunkSize() throws IOException {
  // Read the suffix of the previous chunk.
  if (bytesRemainingInChunk != NO_CHUNK_YET) {
    source.readUtf8LineStrict();
  }
  try {
    bytesRemainingInChunk = source.readHexadecimalUnsignedLong();
    String extensions = source.readUtf8LineStrict().trim();
    if (bytesRemainingInChunk < 0 || (!extensions.isEmpty() && !extensions.startsWith(";"))) {
      throw new ProtocolException("expected chunk size and optional extensions but was \""
          + bytesRemainingInChunk + extensions + "\"");
    }
  } catch (NumberFormatException e) {
    throw new ProtocolException(e.getMessage());
  }
  if (bytesRemainingInChunk == 0L) {
    hasMoreChunks = false;
    HttpHeaders.receiveHeaders(client.cookieJar(), url, readHeaders());
    endOfInput(true, null);
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:22,代码来源:Http1Codec.java

示例5: execute

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
protected Response execute(Request request) throws IOException {
    if (Http.sCache != null) {
        if (HttpMethod.invalidatesCache(request.method())) {
            try {
                Http.sCache.remove(getUrl());
            } catch (IOException ignore) {
            }
        }
        if (!"GET".equals(request.method())) {
            setShouldCache(false);
        }
    }
    OkHttpClient client = getClient();
    Call call = client.newCall(request);
    long start = System.currentTimeMillis();
    Response response = call.execute();
    if (LOG.isLoggable(Log.INFO))
        LOG.i("Took " + (System.currentTimeMillis() - start) + "ms to execute request (" + getLogKey() + ")");
    if (Http.sCache != null && HttpHeaders.hasVaryAll(response)) {
        setShouldCache(false);
    }
    return response;
}
 
开发者ID:lifechurch,项目名称:nuclei-android,代码行数:24,代码来源:HttpTask.java

示例6: parseResponse

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
static void parseResponse(Response response) throws IOException {
  if (!HttpHeaders.hasBody(response)) {
    if (response.isSuccessful()) {
      return;
    } else {
      throw new IllegalStateException("response failed: " + response);
    }
  }
  try (ResponseBody responseBody = response.body()) {
    BufferedSource content = responseBody.source();
    if ("gzip".equalsIgnoreCase(response.header("Content-Encoding"))) {
      content = Okio.buffer(new GzipSource(responseBody.source()));
    }
    if (!response.isSuccessful()) {
      throw new IllegalStateException(
          "response for " + response.request().tag() + " failed: " + content.readUtf8());
    }
  }
}
 
开发者ID:openzipkin,项目名称:zipkin-reporter-java,代码行数:20,代码来源:HttpCall.java

示例7: varyHeaders

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
private static Headers varyHeaders(URLConnection urlConnection, Headers responseHeaders) {
  if (HttpHeaders.hasVaryAll(responseHeaders)) {
    // "*" means that this will be treated as uncacheable anyway.
    return null;
  }
  Set<String> varyFields = HttpHeaders.varyFields(responseHeaders);
  if (varyFields.isEmpty()) {
    return new Headers.Builder().build();
  }

  // This probably indicates another HTTP stack is trying to use the shared ResponseCache.
  // We cannot guarantee this case will work properly because we cannot reliably extract *all*
  // the request header values, and we can't get multiple Vary request header values.
  // We also can't be sure about the Accept-Encoding behavior of other stacks.
  if (!(urlConnection instanceof CacheHttpURLConnection
      || urlConnection instanceof CacheHttpsURLConnection)) {
    return null;
  }

  // This is the case we expect: The URLConnection is from a call to
  // JavaApiConverter.createJavaUrlConnection() and we have access to the user's request headers.
  Map<String, List<String>> requestProperties = urlConnection.getRequestProperties();
  Headers.Builder result = new Headers.Builder();
  for (String fieldName : varyFields) {
    List<String> fieldValues = requestProperties.get(fieldName);
    if (fieldValues == null) {
      if (fieldName.equals("Accept-Encoding")) {
        // Accept-Encoding is special. If OkHttp sees Accept-Encoding is unset it will add
        // "gzip". We don't have access to the request that was actually made so we must do the
        // same.
        result.add("Accept-Encoding", "gzip");
      }
    } else {
      for (String fieldValue : fieldValues) {
        Internal.instance.addLenient(result, fieldName, fieldValue);
      }
    }
  }
  return result.build();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:41,代码来源:JavaApiConverter.java

示例8: put

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
CacheRequest put(Response response) {
  String requestMethod = response.request().method();

  if (HttpMethod.invalidatesCache(response.request().method())) {
    try {
      remove(response.request());
    } catch (IOException ignored) {
      // The cache cannot be written.
    }
    return null;
  }
  if (!requestMethod.equals("GET")) {
    // Don't cache non-GET responses. We're technically allowed to cache
    // HEAD requests and some POST requests, but the complexity of doing
    // so is high and the benefit is low.
    return null;
  }

  if (HttpHeaders.hasVaryAll(response)) {
    return null;
  }

  Entry entry = new Entry(response);
  DiskLruCache.Editor editor = null;
  try {
    editor = cache.edit(key(response.request().url()));
    if (editor == null) {
      return null;
    }
    entry.writeTo(editor);
    return new CacheRequestImpl(editor);
  } catch (IOException e) {
    abortQuietly(editor);
    return null;
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:37,代码来源:Cache.java

示例9: Entry

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
public Entry(Response response) {
  this.url = response.request().url().toString();
  this.varyHeaders = HttpHeaders.varyHeaders(response);
  this.requestMethod = response.request().method();
  this.protocol = response.protocol();
  this.code = response.code();
  this.message = response.message();
  this.responseHeaders = response.headers();
  this.handshake = response.handshake();
  this.sentRequestMillis = response.sentRequestAtMillis();
  this.receivedResponseMillis = response.receivedResponseAtMillis();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:13,代码来源:Cache.java

示例10: challenges

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
/**
 * Returns the authorization challenges appropriate for this response's code. If the response code
 * is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the response code is
 * 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges. Otherwise this
 * returns an empty list of challenges.
 */
public List<Challenge> challenges() {
  String responseField;
  if (code == HTTP_UNAUTHORIZED) {
    responseField = "WWW-Authenticate";
  } else if (code == HTTP_PROXY_AUTH) {
    responseField = "Proxy-Authenticate";
  } else {
    return Collections.emptyList();
  }
  return HttpHeaders.parseChallenges(headers(), responseField);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:18,代码来源:Response.java

示例11: Factory

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
public Factory(long nowMillis, Request request, Response cacheResponse) {
  this.nowMillis = nowMillis;
  this.request = request;
  this.cacheResponse = cacheResponse;

  if (cacheResponse != null) {
    this.sentRequestMillis = cacheResponse.sentRequestAtMillis();
    this.receivedResponseMillis = cacheResponse.receivedResponseAtMillis();
    Headers headers = cacheResponse.headers();
    for (int i = 0, size = headers.size(); i < size; i++) {
      String fieldName = headers.name(i);
      String value = headers.value(i);
      if ("Date".equalsIgnoreCase(fieldName)) {
        servedDate = HttpDate.parse(value);
        servedDateString = value;
      } else if ("Expires".equalsIgnoreCase(fieldName)) {
        expires = HttpDate.parse(value);
      } else if ("Last-Modified".equalsIgnoreCase(fieldName)) {
        lastModified = HttpDate.parse(value);
        lastModifiedString = value;
      } else if ("ETag".equalsIgnoreCase(fieldName)) {
        etag = value;
      } else if ("Age".equalsIgnoreCase(fieldName)) {
        ageSeconds = HttpHeaders.parseSeconds(value, -1);
      }
    }
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:29,代码来源:CacheStrategy.java

示例12: getErrorStream

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
/**
 * Returns an input stream from the server in the case of error such as the requested file (txt,
 * htm, html) is not found on the remote server.
 */
@Override public InputStream getErrorStream() {
  try {
    Response response = getResponse(true);
    if (HttpHeaders.hasBody(response) && response.code() >= HTTP_BAD_REQUEST) {
      return response.body().byteStream();
    }
    return null;
  } catch (IOException e) {
    return null;
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:16,代码来源:OkHttpURLConnection.java

示例13: put

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
@Nullable CacheRequest put(Response response) {
  String requestMethod = response.request().method();

  if (HttpMethod.invalidatesCache(response.request().method())) {
    try {
      remove(response.request());
    } catch (IOException ignored) {
      // The cache cannot be written.
    }
    return null;
  }
  if (!requestMethod.equals("GET")) {
    // Don't cache non-GET responses. We're technically allowed to cache
    // HEAD requests and some POST requests, but the complexity of doing
    // so is high and the benefit is low.
    return null;
  }

  if (HttpHeaders.hasVaryAll(response)) {
    return null;
  }

  Entry entry = new Entry(response);
  DiskLruCache.Editor editor = null;
  try {
    editor = cache.edit(key(response.request().url()));
    if (editor == null) {
      return null;
    }
    entry.writeTo(editor);
    return new CacheRequestImpl(editor);
  } catch (IOException e) {
    abortQuietly(editor);
    return null;
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:37,代码来源:Cache.java

示例14: Entry

import okhttp3.internal.http.HttpHeaders; //导入依赖的package包/类
Entry(Response response) {
  this.url = response.request().url().toString();
  this.varyHeaders = HttpHeaders.varyHeaders(response);
  this.requestMethod = response.request().method();
  this.protocol = response.protocol();
  this.code = response.code();
  this.message = response.message();
  this.responseHeaders = response.headers();
  this.handshake = response.handshake();
  this.sentRequestMillis = response.sentRequestAtMillis();
  this.receivedResponseMillis = response.receivedResponseAtMillis();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:13,代码来源:Cache.java

示例15: 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


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