private boolean requestIsUnrepeatable(IOException e, Request userRequest) { return userRequest.body() instanceof UnrepeatableRequestBody || e instanceof FileNotFoundException; }
private boolean requestIsUnrepeatable(IOException e, Request userRequest) { return userRequest.body() instanceof UnrepeatableRequestBody || e instanceof FileNotFoundException; }
CacheHttpURLConnection(Response response) { super(response.request().url().url()); this.request = response.request(); this.response = response; // Configure URLConnection inherited fields. this.connected = true; this.doOutput = request.body() != null; this.doInput = true; this.useCaches = true; // Configure HttpUrlConnection inherited fields. this.method = request.method(); }
@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); }
OkHttpClient okClient = new OkHttpClient(); okClient.interceptors().add(new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); // Request customization: add request headers Request.Builder requestBuilder = original.newBuilder() .header("Authorization", token) .method(original.method(), original.body()); Request request = requestBuilder.build(); return chain.proceed(request); } });
OkHttpClient okClient = new OkHttpClient.Builder() .addInterceptor( new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); // Request customization: add request headers Request.Builder requestBuilder = original.newBuilder() .header("Authorization", token) .method(original.method(), original.body()); Request request = requestBuilder.build(); return chain.proceed(request); } }) .build();
private static String bodyToString(final Request request){ try { final Request copy = request.newBuilder().build(); final Buffer buffer = new Buffer(); copy.body().writeTo(buffer); return buffer.readUtf8(); } catch (final IOException e) { return "did not work"; } }
@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(), forceContentLength(gzip(originalRequest.body()))) .build(); return chain.proceed(compressedRequest); }
private Response followPostRedirect(Response response, PostRequest postRequest) { String location = response.header("Location"); if (location == null) { throw new IllegalStateException(format("Missing HTTP header 'Location' in redirect of %s", response.request().url())); } HttpUrl url = response.request().url().resolve(location); // Don't follow redirects to unsupported protocols. if (url == null) { throw new IllegalStateException(format("Unsupported protocol in redirect of %s to %s", response.request().url(), location)); } Request.Builder redirectRequest = response.request().newBuilder(); redirectRequest.post(response.request().body()); response.body().close(); return doCall(prepareOkHttpClient(noRedirectOkHttpClient, postRequest), redirectRequest.url(url).build()); }
networkInterceptor.proceed(); OutputStreamRequestBody requestBody = (OutputStreamRequestBody) call.request().body(); if (requestBody != null) requestBody.outputStream().close();
private void bodyToString(Request request) { try { Request copy = request.newBuilder().build(); RequestBody body = copy.body(); if (body == null) return; Buffer buffer = new Buffer(); body.writeTo(buffer); Charset charset = getCharset(body.contentType()); log("\tbody:" + buffer.readString(charset)); } catch (Exception e) { OkLogger.printStackTrace(e); } } }
Request.Builder requestBuilder = userRequest.newBuilder(); RequestBody body = userRequest.body(); if (body != null) { MediaType contentType = body.contentType();
@Nullable @Override public byte[] body() throws IOException { RequestBody body = mRequest.body(); if (body == null) { return null; } OutputStream out = mRequestBodyHelper.createBodySink(firstHeaderValue("Content-Encoding")); BufferedSink bufferedSink = Okio.buffer(Okio.sink(out)); try { body.writeTo(bufferedSink); } finally { bufferedSink.close(); } return mRequestBodyHelper.getDisplayBody(); }
@Override public void writeRequestHeaders(Request request) throws IOException { if (stream != null) return; boolean hasRequestBody = request.body() != null; List<Header> requestHeaders = http2HeadersList(request); stream = connection.newStream(requestHeaders, hasRequestBody); // We may have been asked to cancel while creating the new stream and sending the request // headers, but there was still no stream to close. if (canceled) { stream.closeLater(ErrorCode.CANCEL); throw new IOException("Canceled"); } stream.readTimeout().timeout(chain.readTimeoutMillis(), TimeUnit.MILLISECONDS); stream.writeTimeout().timeout(chain.writeTimeoutMillis(), TimeUnit.MILLISECONDS); }
@Test public void upload() throws IOException { ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class); settings.setProperty(SONAR_TELEMETRY_URL.getKey(), TELEMETRY_URL); underTest.start(); underTest.upload(JSON); verify(okHttpClient).newCall(requestCaptor.capture()); Request request = requestCaptor.getValue(); assertThat(request.method()).isEqualTo("POST"); assertThat(request.body().contentType()).isEqualTo(MediaType.parse("application/json; charset=utf-8")); Buffer body = new Buffer(); request.body().writeTo(body); assertThat(body.readUtf8()).isEqualTo(JSON); assertThat(request.url().toString()).isEqualTo(TELEMETRY_URL); }
@Test public void opt_out() throws IOException { ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class); settings.setProperty(SONAR_TELEMETRY_URL.getKey(), TELEMETRY_URL); underTest.start(); underTest.optOut(JSON); verify(okHttpClient).newCall(requestCaptor.capture()); Request request = requestCaptor.getValue(); assertThat(request.method()).isEqualTo("DELETE"); assertThat(request.body().contentType()).isEqualTo(MediaType.parse("application/json; charset=utf-8")); Buffer body = new Buffer(); request.body().writeTo(body); assertThat(body.readUtf8()).isEqualTo(JSON); assertThat(request.url().toString()).isEqualTo(TELEMETRY_URL); } }
@Override public OutputStream getOutputStream() throws IOException { OutputStreamRequestBody requestBody = (OutputStreamRequestBody) buildCall().request().body(); if (requestBody == null) { throw new ProtocolException("method does not support a request body: " + method); } // If this request needs to stream bytes to the server, build a physical connection immediately // and start streaming those bytes over that connection. if (requestBody instanceof StreamedRequestBody) { connect(); networkInterceptor.proceed(); } if (requestBody.isClosed()) { throw new ProtocolException("cannot write request body after response has been read"); } return requestBody.outputStream(); }
@Override public okhttp3.Response intercept(Chain chain) throws IOException { Request request = chain.request(); this.requestBody = request.body(); okhttp3.Response response = new okhttp3.Response.Builder() .request(chain.request()) .protocol(Protocol.HTTP_2) .code(200) .message("") .build(); return response; }
@Override public void writeRequestHeaders(Request request) throws IOException { if (stream != null) return; boolean hasRequestBody = request.body() != null; List<Header> requestHeaders = http2HeadersList(request); stream = connection.newStream(requestHeaders, hasRequestBody); // We may have been asked to cancel while creating the new stream and sending the request // headers, but there was still no stream to close. if (canceled) { stream.closeLater(ErrorCode.CANCEL); throw new IOException("Canceled"); } stream.readTimeout().timeout(chain.readTimeoutMillis(), TimeUnit.MILLISECONDS); stream.writeTimeout().timeout(chain.writeTimeoutMillis(), TimeUnit.MILLISECONDS); }