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

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

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

Request.header介绍

暂无

代码示例

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

  1. /**
  2. * Returns true if the request contains conditions that save the server from sending a response
  3. * that the client has locally. When a request is enqueued with its own conditions, the built-in
  4. * response cache won't be used.
  5. */
  6. private static boolean hasConditions(Request request) {
  7. return request.header("If-Modified-Since") != null || request.header("If-None-Match") != null;
  8. }
  9. }

代码示例来源: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. @Override
  2. public String getRequestProperty(String key) {
  3. return request.header(key);
  4. }

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

  1. /**
  2. * Returns true if the request contains conditions that save the server from sending a response
  3. * that the client has locally. When a request is enqueued with its own conditions, the built-in
  4. * response cache won't be used.
  5. */
  6. private static boolean hasConditions(Request request) {
  7. return request.header("If-Modified-Since") != null || request.header("If-None-Match") != null;
  8. }
  9. }

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

  1. public void run() throws Exception {
  2. Request request = new Request.Builder()
  3. .url("https://publicobject.com/helloworld.txt")
  4. .build();
  5. try (Response response = client.newCall(request).execute()) {
  6. System.out.println(response.request().header("Date"));
  7. }
  8. }

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

  1. @Nullable
  2. @Override
  3. public String firstHeaderValue(String name) {
  4. return mRequest.header(name);
  5. }
  6. }

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

  1. @Override public Request authenticate(Route route, Response response) throws IOException {
  2. if (response.request().header("Authorization") != null) {
  3. return null; // Give up, we've already attempted to authenticate.
  4. }
  5. System.out.println("Authenticating for response: " + response);
  6. System.out.println("Challenges: " + response.challenges());
  7. String credential = Credentials.basic("jesse", "password1");
  8. return response.request().newBuilder()
  9. .header("Authorization", credential)
  10. .build();
  11. }
  12. })

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

  1. @Override public Response intercept(Chain chain) throws IOException {
  2. Request originalRequest = chain.request();
  3. if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
  4. return chain.proceed(originalRequest);
  5. }
  6. Request compressedRequest = originalRequest.newBuilder()
  7. .header("Content-Encoding", "gzip")
  8. .method(originalRequest.method(), gzip(originalRequest.body()))
  9. .build();
  10. return chain.proceed(compressedRequest);
  11. }

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

  1. @Override public Sink createRequestBody(Request request, long contentLength) {
  2. if ("chunked".equalsIgnoreCase(request.header("Transfer-Encoding"))) {
  3. // Stream a request body of unknown length.
  4. return newChunkedSink();
  5. }
  6. if (contentLength != -1) {
  7. // Stream a request body of a known length.
  8. return newFixedLengthSink(contentLength);
  9. }
  10. throw new IllegalStateException(
  11. "Cannot stream a request body without chunked encoding or a known content length!");
  12. }

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

  1. /**
  2. * Now that we've buffered the entire request body, update the request headers and the body
  3. * itself. This happens late to enable HttpURLConnection users to complete the socket connection
  4. * before sending request body bytes.
  5. */
  6. @Override public Request prepareToSendRequest(Request request) throws IOException {
  7. if (request.header("Content-Length") != null) return request;
  8. outputStream().close();
  9. contentLength = buffer.size();
  10. return request.newBuilder()
  11. .removeHeader("Transfer-Encoding")
  12. .header("Content-Length", Long.toString(buffer.size()))
  13. .build();
  14. }

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

  1. @Override public Sink createRequestBody(Request request, long contentLength) {
  2. if ("chunked".equalsIgnoreCase(request.header("Transfer-Encoding"))) {
  3. // Stream a request body of unknown length.
  4. return newChunkedSink();
  5. }
  6. if (contentLength != -1) {
  7. // Stream a request body of a known length.
  8. return newFixedLengthSink(contentLength);
  9. }
  10. throw new IllegalStateException(
  11. "Cannot stream a request body without chunked encoding or a known content length!");
  12. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. @Override
  2. public void onResponse(Response okHttpResponse, String response) {
  3. responseRef.set(response);
  4. responseBodySuccess.set(okHttpResponse.isSuccessful());
  5. headerRef.set(okHttpResponse.request().header("headerKey"));
  6. latch.countDown();
  7. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. @Override
  2. public void onResponse(Response okHttpResponse, String response) {
  3. responseRef.set(response);
  4. responseBodySuccess.set(okHttpResponse.isSuccessful());
  5. headerRef.set(okHttpResponse.request().header("headerKey"));
  6. latch.countDown();
  7. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. @Override
  2. public void onResponse(Response okHttpResponse, User user) {
  3. firstNameRef.set(user.firstName);
  4. lastNameRef.set(user.lastName);
  5. responseBodySuccess.set(okHttpResponse.isSuccessful());
  6. headerRef.set(okHttpResponse.request().header("headerKey"));
  7. latch.countDown();
  8. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. @Override
  2. public void onResponse(Response okHttpResponse, User user) {
  3. firstNameRef.set(user.firstName);
  4. lastNameRef.set(user.lastName);
  5. responseBodySuccess.set(okHttpResponse.isSuccessful());
  6. headerRef.set(okHttpResponse.request().header("headerKey"));
  7. latch.countDown();
  8. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. @Override
  2. public void onResponse(Response okHttpResponse, User user) {
  3. firstNameRef.set(user.firstName);
  4. lastNameRef.set(user.lastName);
  5. responseBodySuccess.set(okHttpResponse.isSuccessful());
  6. headerRef.set(okHttpResponse.request().header("headerKey"));
  7. latch.countDown();
  8. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. @Override
  2. public void onResponse(Response okHttpResponse, User user) {
  3. firstNameRef.set(user.firstName);
  4. lastNameRef.set(user.lastName);
  5. responseBodySuccess.set(okHttpResponse.isSuccessful());
  6. headerRef.set(okHttpResponse.request().header("headerKey"));
  7. latch.countDown();
  8. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. @Override
  2. public void onResponse(Response okHttpResponse, JSONObject response) {
  3. try {
  4. firstNameRef.set(response.getString("firstName"));
  5. lastNameRef.set(response.getString("lastName"));
  6. responseBodySuccess.set(okHttpResponse.isSuccessful());
  7. headerRef.set(okHttpResponse.request().header("headerKey"));
  8. latch.countDown();
  9. } catch (JSONException e) {
  10. assertTrue(false);
  11. }
  12. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. @Override
  2. public void onResponse(Response okHttpResponse, JSONObject response) {
  3. try {
  4. firstNameRef.set(response.getString("firstName"));
  5. lastNameRef.set(response.getString("lastName"));
  6. responseBodySuccess.set(okHttpResponse.isSuccessful());
  7. headerRef.set(okHttpResponse.request().header("headerKey"));
  8. latch.countDown();
  9. } catch (JSONException e) {
  10. assertTrue(false);
  11. }
  12. }

代码示例来源:origin: SonarSource/sonarqube

  1. @Test
  2. public void get_returns_a_OkHttpClient_with_proxy_authentication() throws Exception {
  3. settings.setProperty("http.proxyUser", "the-login");
  4. settings.setProperty("http.proxyPassword", "the-password");
  5. OkHttpClient client = underTest.provide(settings.asConfig(), runtime);
  6. Response response = new Response.Builder().protocol(Protocol.HTTP_1_1).request(new Request.Builder().url("http://foo").build()).code(407).build();
  7. Request request = client.proxyAuthenticator().authenticate(null, response);
  8. assertThat(request.header("Proxy-Authorization")).isEqualTo("Basic " + Base64.getEncoder().encodeToString("the-login:the-password".getBytes()));
  9. }

相关文章