okhttp3.Request.headers()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(407)

本文整理了Java中okhttp3.Request.headers方法的一些代码示例,展示了Request.headers的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.headers方法的具体详情如下:
包路径:okhttp3.Request
类名称:Request
方法名:headers

Request.headers介绍

暂无

代码示例

代码示例来源:origin: square/okhttp

  1. @Override public Response intercept(Chain chain) throws IOException {
  2. long t1 = System.nanoTime();
  3. Request request = chain.request();
  4. logger.info(String.format("Sending request %s on %s%n%s",
  5. request.url(), chain.connection(), request.headers()));
  6. Response response = chain.proceed(request);
  7. long t2 = System.nanoTime();
  8. logger.info(String.format("Received response for %s in %.1fms%n%s",
  9. request.url(), (t2 - t1) / 1e6d, response.headers()));
  10. return response;
  11. }
  12. }

代码示例来源:origin: square/okhttp

  1. /**
  2. * Extracts an immutable request header map from the supplied {@link Headers}.
  3. */
  4. static Map<String, List<String>> extractJavaHeaders(Request request) {
  5. return JavaNetHeaders.toMultimap(request.headers(), null);
  6. }

代码示例来源:origin: square/okhttp

  1. @Override
  2. public Map<String, List<String>> getRequestProperties() {
  3. // The RI and OkHttp's HttpURLConnectionImpl fail this call after connect() as required by the
  4. // spec. There seems no good reason why this should fail while getRequestProperty() is ok.
  5. // We don't fail here, because we need all request header values for caching Vary responses
  6. // correctly.
  7. return JavaNetHeaders.toMultimap(request.headers(), null);
  8. }

代码示例来源:origin: square/okhttp

  1. @Override
  2. public long getIfModifiedSince() {
  3. return stringToLong(request.headers().get("If-Modified-Since"));
  4. }

代码示例来源:origin: square/okhttp

  1. /**
  2. * Returns the subset of the headers in {@code response}'s request that impact the content of
  3. * response's body.
  4. */
  5. public static Headers varyHeaders(Response response) {
  6. // Use the request headers sent over the network, since that's what the
  7. // response varies on. Otherwise OkHttp-supplied headers like
  8. // "Accept-Encoding: gzip" may be lost.
  9. Headers requestHeaders = response.networkResponse().request().headers();
  10. Headers responseHeaders = response.headers();
  11. return varyHeaders(requestHeaders, responseHeaders);
  12. }

代码示例来源:origin: prestodb/presto

  1. @Override
  2. public Request authenticate(Route route, Response response)
  3. {
  4. // skip if we already tried or were not asked for Kerberos
  5. if (response.request().headers(AUTHORIZATION).stream().anyMatch(SpnegoHandler::isNegotiate) ||
  6. response.headers(WWW_AUTHENTICATE).stream().noneMatch(SpnegoHandler::isNegotiate)) {
  7. return null;
  8. }
  9. return authenticate(response.request());
  10. }

代码示例来源:origin: square/okhttp

  1. /**
  2. * Returns true if none of the Vary headers have changed between {@code cachedRequest} and {@code
  3. * newRequest}.
  4. */
  5. public static boolean varyMatches(
  6. Response cachedResponse, Headers cachedRequest, Request newRequest) {
  7. for (String field : varyFields(cachedResponse)) {
  8. if (!Objects.equals(cachedRequest.values(field), newRequest.headers(field))) return false;
  9. }
  10. return true;
  11. }

代码示例来源:origin: facebook/stetho

  1. @Override
  2. public int headerCount() {
  3. return mRequest.headers().size();
  4. }

代码示例来源:origin: square/okhttp

  1. @Override public Response intercept(Chain chain) throws IOException {
  2. Request request = chain.request();
  3. Headers newHeaders = request.headers()
  4. .newBuilder()
  5. .add("Date", new Date())
  6. .build();
  7. Request newRequest = request.newBuilder()
  8. .headers(newHeaders)
  9. .build();
  10. return chain.proceed(newRequest);
  11. }
  12. }

代码示例来源:origin: facebook/stetho

  1. @Override
  2. public String headerName(int index) {
  3. return mRequest.headers().name(index);
  4. }

代码示例来源:origin: facebook/stetho

  1. @Override
  2. public String headerValue(int index) {
  3. return mRequest.headers().value(index);
  4. }

代码示例来源:origin: lingochamp/okdownload

  1. @Override public Map<String, List<String>> getRequestProperties() {
  2. if (request != null) {
  3. return request.headers().toMultimap();
  4. } else {
  5. return requestBuilder.build().headers().toMultimap();
  6. }
  7. }

代码示例来源:origin: com.squareup.okhttp3/okhttp

  1. /**
  2. * Returns the subset of the headers in {@code response}'s request that impact the content of
  3. * response's body.
  4. */
  5. public static Headers varyHeaders(Response response) {
  6. // Use the request headers sent over the network, since that's what the
  7. // response varies on. Otherwise OkHttp-supplied headers like
  8. // "Accept-Encoding: gzip" may be lost.
  9. Headers requestHeaders = response.networkResponse().request().headers();
  10. Headers responseHeaders = response.headers();
  11. return varyHeaders(requestHeaders, responseHeaders);
  12. }

代码示例来源:origin: com.squareup.okhttp3/okhttp

  1. /**
  2. * Returns true if none of the Vary headers have changed between {@code cachedRequest} and {@code
  3. * newRequest}.
  4. */
  5. public static boolean varyMatches(
  6. Response cachedResponse, Headers cachedRequest, Request newRequest) {
  7. for (String field : varyFields(cachedResponse)) {
  8. if (!Objects.equals(cachedRequest.values(field), newRequest.headers(field))) return false;
  9. }
  10. return true;
  11. }

代码示例来源:origin: JessYanCoding/MVPArms

  1. private static String[] getRequest(Request request) {
  2. String log;
  3. String header = request.headers().toString();
  4. log = METHOD_TAG + request.method() + DOUBLE_SEPARATOR +
  5. (isEmpty(header) ? "" : HEADERS_TAG + LINE_SEPARATOR + dotHeaders(header));
  6. return log.split(LINE_SEPARATOR);
  7. }

代码示例来源:origin: square/okhttp

  1. public static List<Header> http2HeadersList(Request request) {
  2. Headers headers = request.headers();
  3. List<Header> result = new ArrayList<>(headers.size() + 4);
  4. result.add(new Header(TARGET_METHOD, request.method()));
  5. result.add(new Header(TARGET_PATH, RequestLine.requestPath(request.url())));
  6. String host = request.header("Host");
  7. if (host != null) {
  8. result.add(new Header(TARGET_AUTHORITY, host)); // Optional.
  9. }
  10. result.add(new Header(TARGET_SCHEME, request.url().scheme()));
  11. for (int i = 0, size = headers.size(); i < size; i++) {
  12. // header names must be lowercase.
  13. String name = headers.name(i).toLowerCase(Locale.US);
  14. if (!HTTP_2_SKIPPED_REQUEST_HEADERS.contains(name)
  15. || name.equals(TE) && headers.value(i).equals("trailers")) {
  16. result.add(new Header(name, headers.value(i)));
  17. }
  18. }
  19. return result;
  20. }

代码示例来源:origin: square/okhttp

  1. /**
  2. * Prepares the HTTP headers and sends them to the server.
  3. *
  4. * <p>For streaming requests with a body, headers must be prepared <strong>before</strong> the
  5. * output stream has been written to. Otherwise the body would need to be buffered!
  6. *
  7. * <p>For non-streaming requests with a body, headers must be prepared <strong>after</strong> the
  8. * output stream has been written to and closed. This ensures that the {@code Content-Length}
  9. * header field receives the proper value.
  10. */
  11. @Override public void writeRequestHeaders(Request request) throws IOException {
  12. String requestLine = RequestLine.get(
  13. request, streamAllocation.connection().route().proxy().type());
  14. writeRequest(request.headers(), requestLine);
  15. }

代码示例来源:origin: square/okhttp

  1. varyHeaders = HttpHeaders.varyHeaders(request.headers(), responseHeaders);

代码示例来源:origin: square/okhttp

  1. source.timeout().timeout(readTimeout, MILLISECONDS);
  2. sink.timeout().timeout(writeTimeout, MILLISECONDS);
  3. tunnelConnection.writeRequest(tunnelRequest.headers(), requestLine);
  4. tunnelConnection.finishRequest();
  5. Response response = tunnelConnection.readResponseHeaders(false)

代码示例来源:origin: square/okhttp

  1. Headers.Builder conditionalRequestHeaders = request.headers().newBuilder();
  2. Internal.instance.addLenient(conditionalRequestHeaders, conditionName, conditionValue);

相关文章