/** HTTP headers. */ public Headers headers() { return rawResponse.headers(); }
@Override public HttpHeaders getHeaders() { HttpHeaders headers = this.headers; if (headers == null) { headers = new HttpHeaders(); for (String headerName : this.response.headers().names()) { for (String headerValue : this.response.headers(headerName)) { headers.add(headerName, headerValue); } } this.headers = headers; } return headers; }
@Override public String getHeaderFieldKey(int position) { // Deal with index 0 meaning "status line" if (position < 0) { throw new IllegalArgumentException("Invalid header index: " + position); } if (position == 0 || position > response.headers().size()) { return null; } return response.headers().name(position - 1); }
/** * Returns true if a Vary header contains an asterisk. Such responses cannot be cached. */ public static boolean hasVaryAll(Response response) { return hasVaryAll(response.headers()); }
public static long contentLength(Response response) { return contentLength(response.headers()); }
private static Set<String> varyFields(Response response) { return varyFields(response.headers()); }
@Override public Response intercept(Chain chain) throws IOException { long t1 = System.nanoTime(); Request request = chain.request(); logger.info(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); Response response = chain.proceed(request); long t2 = System.nanoTime(); logger.info(String.format("Received response for %s in %.1fms%n%s", request.url(), (t2 - t1) / 1e6d, response.headers())); return response; } }
public void run() throws Exception { Request request = new Request.Builder() .url("https://api.github.com/repos/square/okhttp/issues") .header("User-Agent", "OkHttp Headers.java") .addHeader("Accept", "application/json; q=0.5") .addHeader("Accept", "application/vnd.github.v3+json") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println("Server: " + response.header("Server")); System.out.println("Date: " + response.header("Date")); System.out.println("Vary: " + response.headers("Vary")); } }
public void run() throws Exception { Request request = new Request.Builder() .url("https://publicobject.com/helloworld.txt") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Headers responseHeaders = response.headers(); for (int i = 0; i < responseHeaders.size(); i++) { System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); } System.out.println(response.body().string()); } }
public void run() throws Exception { Request request = new Request.Builder() .url("https://publicobject.com/helloworld.txt") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Headers responseHeaders = response.headers(); for (int i = 0; i < responseHeaders.size(); i++) { System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); } System.out.println(response.body().string()); } }
@Override public void onResponse(Call call, Response response) throws IOException { try (ResponseBody responseBody = response.body()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Headers responseHeaders = response.headers(); for (int i = 0, size = responseHeaders.size(); i < size; i++) { System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); } System.out.println(responseBody.string()); } } });
/** * Returns the RFC 7235 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. * * <p>If a challenge uses the {@code token68} variant instead of auth params, there is exactly one * auth param in the challenge at key {@code null}. Invalid headers and challenges are ignored. * No semantic validation is done, for example that {@code Basic} auth must have a {@code realm} * auth param, this is up to the caller that interprets these 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); }
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(); }
@Override public String getHeaderField(int position) { // Deal with index 0 meaning "status line" if (position < 0) { throw new IllegalArgumentException("Invalid header index: " + position); } if (position == 0) { return StatusLine.get(response).toString(); } if (position > response.headers().size()) { return null; } return response.headers().value(position - 1); }
/** * Returns the subset of the headers in {@code response}'s request that impact the content of * response's body. */ public static Headers varyHeaders(Response response) { // Use the request headers sent over the network, since that's what the // response varies on. Otherwise OkHttp-supplied headers like // "Accept-Encoding: gzip" may be lost. Headers requestHeaders = response.networkResponse().request().headers(); Headers responseHeaders = response.headers(); return varyHeaders(requestHeaders, responseHeaders); }
@Override public String getHeaderField(String fieldName) { return fieldName == null ? StatusLine.get(response).toString() : response.headers().get(fieldName); }
@Override public Map<String, List<String>> getHeaderFields() { return JavaNetHeaders.toMultimap(response.headers(), StatusLine.get(response).toString()); }
private static Headers withSyntheticHeaders(Response okResponse) { return okResponse.headers().newBuilder() .add(SENT_MILLIS, Long.toString(okResponse.sentRequestAtMillis())) .add(RECEIVED_MILLIS, Long.toString(okResponse.receivedResponseAtMillis())) .build(); }
private static HttpResponse transformResponse(Response response) { int code = response.code(); String message = response.message(); BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message); ResponseBody body = response.body(); InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength()); httpResponse.setEntity(entity); Headers headers = response.headers(); for (int i = 0, size = headers.size(); i < size; i++) { String name = headers.name(i); String value = headers.value(i); httpResponse.addHeader(name, value); if ("Content-Type".equalsIgnoreCase(name)) { entity.setContentType(value); } else if ("Content-Encoding".equalsIgnoreCase(name)) { entity.setContentEncoding(value); } } return httpResponse; }
private Headers getHeaders() throws IOException { if (responseHeaders == null) { Response response = getResponse(true); Headers headers = response.headers(); responseHeaders = headers.newBuilder() .add(SELECTED_PROTOCOL, response.protocol().toString()) .add(RESPONSE_SOURCE, responseSourceHeader(response)) .build(); } return responseHeaders; }