本文整理了Java中okhttp3.Request.header
方法的一些代码示例,展示了Request.header
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.header
方法的具体详情如下:
包路径:okhttp3.Request
类名称:Request
方法名:header
暂无
代码示例来源:origin: square/okhttp
/**
* Returns true if the request contains conditions that save the server from sending a response
* that the client has locally. When a request is enqueued with its own conditions, the built-in
* response cache won't be used.
*/
private static boolean hasConditions(Request request) {
return request.header("If-Modified-Since") != null || request.header("If-None-Match") != null;
}
}
代码示例来源:origin: square/okhttp
public static List<Header> http2HeadersList(Request request) {
Headers headers = request.headers();
List<Header> result = new ArrayList<>(headers.size() + 4);
result.add(new Header(TARGET_METHOD, request.method()));
result.add(new Header(TARGET_PATH, RequestLine.requestPath(request.url())));
String host = request.header("Host");
if (host != null) {
result.add(new Header(TARGET_AUTHORITY, host)); // Optional.
}
result.add(new Header(TARGET_SCHEME, request.url().scheme()));
for (int i = 0, size = headers.size(); i < size; i++) {
// header names must be lowercase.
String name = headers.name(i).toLowerCase(Locale.US);
if (!HTTP_2_SKIPPED_REQUEST_HEADERS.contains(name)
|| name.equals(TE) && headers.value(i).equals("trailers")) {
result.add(new Header(name, headers.value(i)));
}
}
return result;
}
代码示例来源:origin: square/okhttp
@Override
public String getRequestProperty(String key) {
return request.header(key);
}
代码示例来源:origin: com.squareup.okhttp3/okhttp
/**
* Returns true if the request contains conditions that save the server from sending a response
* that the client has locally. When a request is enqueued with its own conditions, the built-in
* response cache won't be used.
*/
private static boolean hasConditions(Request request) {
return request.header("If-Modified-Since") != null || request.header("If-None-Match") != null;
}
}
代码示例来源:origin: square/okhttp
public void run() throws Exception {
Request request = new Request.Builder()
.url("https://publicobject.com/helloworld.txt")
.build();
try (Response response = client.newCall(request).execute()) {
System.out.println(response.request().header("Date"));
}
}
代码示例来源:origin: facebook/stetho
@Nullable
@Override
public String firstHeaderValue(String name) {
return mRequest.header(name);
}
}
代码示例来源:origin: square/okhttp
@Override public Request authenticate(Route route, Response response) throws IOException {
if (response.request().header("Authorization") != null) {
return null; // Give up, we've already attempted to authenticate.
}
System.out.println("Authenticating for response: " + response);
System.out.println("Challenges: " + response.challenges());
String credential = Credentials.basic("jesse", "password1");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
})
代码示例来源:origin: square/okhttp
@Override public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
return chain.proceed(originalRequest);
}
Request compressedRequest = originalRequest.newBuilder()
.header("Content-Encoding", "gzip")
.method(originalRequest.method(), gzip(originalRequest.body()))
.build();
return chain.proceed(compressedRequest);
}
代码示例来源:origin: square/okhttp
@Override public Sink createRequestBody(Request request, long contentLength) {
if ("chunked".equalsIgnoreCase(request.header("Transfer-Encoding"))) {
// Stream a request body of unknown length.
return newChunkedSink();
}
if (contentLength != -1) {
// Stream a request body of a known length.
return newFixedLengthSink(contentLength);
}
throw new IllegalStateException(
"Cannot stream a request body without chunked encoding or a known content length!");
}
代码示例来源:origin: square/okhttp
/**
* Now that we've buffered the entire request body, update the request headers and the body
* itself. This happens late to enable HttpURLConnection users to complete the socket connection
* before sending request body bytes.
*/
@Override public Request prepareToSendRequest(Request request) throws IOException {
if (request.header("Content-Length") != null) return request;
outputStream().close();
contentLength = buffer.size();
return request.newBuilder()
.removeHeader("Transfer-Encoding")
.header("Content-Length", Long.toString(buffer.size()))
.build();
}
代码示例来源:origin: com.squareup.okhttp3/okhttp
@Override public Sink createRequestBody(Request request, long contentLength) {
if ("chunked".equalsIgnoreCase(request.header("Transfer-Encoding"))) {
// Stream a request body of unknown length.
return newChunkedSink();
}
if (contentLength != -1) {
// Stream a request body of a known length.
return newFixedLengthSink(contentLength);
}
throw new IllegalStateException(
"Cannot stream a request body without chunked encoding or a known content length!");
}
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
@Override
public void onResponse(Response okHttpResponse, String response) {
responseRef.set(response);
responseBodySuccess.set(okHttpResponse.isSuccessful());
headerRef.set(okHttpResponse.request().header("headerKey"));
latch.countDown();
}
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
@Override
public void onResponse(Response okHttpResponse, String response) {
responseRef.set(response);
responseBodySuccess.set(okHttpResponse.isSuccessful());
headerRef.set(okHttpResponse.request().header("headerKey"));
latch.countDown();
}
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
@Override
public void onResponse(Response okHttpResponse, User user) {
firstNameRef.set(user.firstName);
lastNameRef.set(user.lastName);
responseBodySuccess.set(okHttpResponse.isSuccessful());
headerRef.set(okHttpResponse.request().header("headerKey"));
latch.countDown();
}
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
@Override
public void onResponse(Response okHttpResponse, User user) {
firstNameRef.set(user.firstName);
lastNameRef.set(user.lastName);
responseBodySuccess.set(okHttpResponse.isSuccessful());
headerRef.set(okHttpResponse.request().header("headerKey"));
latch.countDown();
}
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
@Override
public void onResponse(Response okHttpResponse, User user) {
firstNameRef.set(user.firstName);
lastNameRef.set(user.lastName);
responseBodySuccess.set(okHttpResponse.isSuccessful());
headerRef.set(okHttpResponse.request().header("headerKey"));
latch.countDown();
}
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
@Override
public void onResponse(Response okHttpResponse, User user) {
firstNameRef.set(user.firstName);
lastNameRef.set(user.lastName);
responseBodySuccess.set(okHttpResponse.isSuccessful());
headerRef.set(okHttpResponse.request().header("headerKey"));
latch.countDown();
}
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
@Override
public void onResponse(Response okHttpResponse, JSONObject response) {
try {
firstNameRef.set(response.getString("firstName"));
lastNameRef.set(response.getString("lastName"));
responseBodySuccess.set(okHttpResponse.isSuccessful());
headerRef.set(okHttpResponse.request().header("headerKey"));
latch.countDown();
} catch (JSONException e) {
assertTrue(false);
}
}
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
@Override
public void onResponse(Response okHttpResponse, JSONObject response) {
try {
firstNameRef.set(response.getString("firstName"));
lastNameRef.set(response.getString("lastName"));
responseBodySuccess.set(okHttpResponse.isSuccessful());
headerRef.set(okHttpResponse.request().header("headerKey"));
latch.countDown();
} catch (JSONException e) {
assertTrue(false);
}
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void get_returns_a_OkHttpClient_with_proxy_authentication() throws Exception {
settings.setProperty("http.proxyUser", "the-login");
settings.setProperty("http.proxyPassword", "the-password");
OkHttpClient client = underTest.provide(settings.asConfig(), runtime);
Response response = new Response.Builder().protocol(Protocol.HTTP_1_1).request(new Request.Builder().url("http://foo").build()).code(407).build();
Request request = client.proxyAuthenticator().authenticate(null, response);
assertThat(request.header("Proxy-Authorization")).isEqualTo("Basic " + Base64.getEncoder().encodeToString("the-login:the-password".getBytes()));
}
内容来源于网络,如有侵权,请联系作者删除!