public @Nullable String header(String name) { return header(name, null); }
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")); } }
private int retryAfter(Response userResponse, int defaultDelay) { String header = userResponse.header("Retry-After"); if (header == null) { return defaultDelay; } // https://tools.ietf.org/html/rfc7231#section-7.1.3 // currently ignores a HTTP-date, and assumes any non int 0 is a delay if (header.matches("\\d+")) { return Integer.valueOf(header); } return Integer.MAX_VALUE; }
&& "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding")) && HttpHeaders.hasBody(networkResponse)) { GzipSource responseBody = new GzipSource(networkResponse.body().source()); .build(); responseBuilder.headers(strippedHeaders); String contentType = networkResponse.header("Content-Type"); responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
@Override public String contentType() { return okResponse.header("Content-Type"); }
@Nullable @Override public String firstHeaderValue(String name) { return mResponse.header(name); } }
@Override public Optional<String> header(String name) { return Optional.ofNullable(okResponse.header(name)); }
private int retryAfter(Response userResponse, int defaultDelay) { String header = userResponse.header("Retry-After"); if (header == null) { return defaultDelay; } // https://tools.ietf.org/html/rfc7231#section-7.1.3 // currently ignores a HTTP-date, and assumes any non int 0 is a delay if (header.matches("\\d+")) { return Integer.valueOf(header); } return Integer.MAX_VALUE; }
/** Returns true if the response must have a (possibly 0-length) body. See RFC 7231. */ public static boolean hasBody(Response response) { // HEAD requests never yield a body regardless of the response headers. if (response.request().method().equals("HEAD")) { return false; } int responseCode = response.code(); if ((responseCode < HTTP_CONTINUE || responseCode >= 200) && responseCode != HTTP_NO_CONTENT && responseCode != HTTP_NOT_MODIFIED) { return true; } // If the Content-Length or Transfer-Encoding headers disagree with the response code, the // response is malformed. For best compatibility, we honor the headers. if (contentLength(response) != -1 || "chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) { return true; } return false; }
void checkResponse(Response response) throws ProtocolException { if (response.code() != 101) { throw new ProtocolException("Expected HTTP 101 response but was '" + response.code() + " " + response.message() + "'"); } String headerConnection = response.header("Connection"); if (!"Upgrade".equalsIgnoreCase(headerConnection)) { throw new ProtocolException("Expected 'Connection' header value 'Upgrade' but was '" + headerConnection + "'"); } String headerUpgrade = response.header("Upgrade"); if (!"websocket".equalsIgnoreCase(headerUpgrade)) { throw new ProtocolException( "Expected 'Upgrade' header value 'websocket' but was '" + headerUpgrade + "'"); } String headerAccept = response.header("Sec-WebSocket-Accept"); String acceptExpected = ByteString.encodeUtf8(key + WebSocketProtocol.ACCEPT_MAGIC) .sha1().base64(); if (!acceptExpected.equals(headerAccept)) { throw new ProtocolException("Expected 'Sec-WebSocket-Accept' header value '" + acceptExpected + "' but was '" + headerAccept + "'"); } }
String contentType = response.header("Content-Type"); if (responseCode != 200 || contentType == null) { return;
@Test public void currentSpanVisibleToOtherFilters() throws Exception { delegate = userFilter; String path = "/foo"; Request request = new Request.Builder().url(url(path)) .header(EXTRA_KEY, "abcdefg").build(); try (Response response = client.newCall(request).execute()) { assertThat(response.isSuccessful()).isTrue(); assertThat(response.header(EXTRA_KEY)) .isEqualTo("abcdefg"); } takeSpan(); }
ResponseBodyProxy(@NotNull HttpCacheRecordEditor cacheRecordEditor, @NotNull Response sourceResponse, @NotNull ApolloLogger logger) { checkNotNull(cacheRecordEditor, "cacheRecordEditor == null"); checkNotNull(sourceResponse, "sourceResponse == null"); checkNotNull(logger, "logger == null"); this.contentType = sourceResponse.header("Content-Type"); this.contentLength = sourceResponse.header("Content-Length"); this.responseBodySource = new ProxySource(cacheRecordEditor, sourceResponse.body().source(), logger); }
@Override public ResponseBody openResponseBody(Response response) throws IOException { streamAllocation.eventListener.responseBodyStart(streamAllocation.call); String contentType = response.header("Content-Type"); if (!HttpHeaders.hasBody(response)) { Source source = newFixedLengthSource(0); return new RealResponseBody(contentType, 0, Okio.buffer(source)); } if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) { Source source = newChunkedSource(response.request().url()); return new RealResponseBody(contentType, -1L, Okio.buffer(source)); } long contentLength = HttpHeaders.contentLength(response); if (contentLength != -1) { Source source = newFixedLengthSource(contentLength); return new RealResponseBody(contentType, contentLength, Okio.buffer(source)); } return new RealResponseBody(contentType, -1L, Okio.buffer(newUnknownLengthSource())); }
String contentType = response.header("Content-Type"); long contentLength = response.body().contentLength(); return response.newBuilder()
@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)); }
@Test public void mime_type_is_set_on_response() throws Exception { system.pluginStream = IOUtils.toInputStream("bar"); when(pluginRepository.hasPlugin("myplugin")).thenReturn(true); Response response = call("/static/myplugin/foo.css"); assertThat(response.header("Content-Type")).isEqualTo("text/css"); assertThat(response.body().string()).isEqualTo("bar"); }
@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)); }
if (tunnelRequest == null) throw new IOException("Failed to authenticate with proxy"); if ("close".equalsIgnoreCase(response.header("Connection"))) { return tunnelRequest;
if (response.header("Expires") != null || response.cacheControl().maxAgeSeconds() != -1 || response.cacheControl().isPublic()