@Override public String toString() { return "Response{protocol=" + protocol + ", code=" + code + ", message=" + message + ", url=" + request.url() + '}'; }
@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 boolean matches(Request request, Response response) { return url.equals(request.url().toString()) && requestMethod.equals(request.method()) && HttpHeaders.varyMatches(response, varyHeaders, request); }
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (request.url().host().equals(host)) { request = request.newBuilder() .header("Authorization", credentials) .build(); } return chain.proceed(request); } }
@Override public void onFailure(Call<Page> call, Throwable t) { System.out.println(call.request().url() + ": failed: " + t); } });
String host() { return originalRequest.url().host(); }
String redactedUrl() { return originalRequest.url().redact(); }
/** * Returns the request status line, like "GET / HTTP/1.1". This is exposed to the application by * {@link HttpURLConnection#getHeaderFields}, so it needs to be set even if the transport is * HTTP/2. */ public static String get(Request request, Proxy.Type proxyType) { StringBuilder result = new StringBuilder(); result.append(request.method()); result.append(' '); if (includeAuthorityInRequestLine(request, proxyType)) { result.append(request.url()); } else { result.append(requestPath(request.url())); } result.append(" HTTP/1.1"); return result.toString(); }
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 EventListener create(Call call) { long callId = nextCallId.getAndIncrement(); System.out.printf("%04d %s%n", callId, call.request().url()); return new PrintingEventListener(callId, System.nanoTime()); } };
@Override public okhttp3.Response intercept(Chain chain) throws IOException { Request request = chain.request(); String host = this.host; if (host != null) { HttpUrl newUrl = request.url().newBuilder() .host(host) .build(); request = request.newBuilder() .url(newUrl) .build(); } return chain.proceed(request); } }
@Override public void onResponse(Call call, Response response) { synchronized (lock) { this.response = response; this.handshake = response.handshake(); this.url = response.request().url().url(); lock.notifyAll(); } }
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(); }
public static void main(String... args) throws IOException { HostSelectionInterceptor hostSelectionInterceptor = new HostSelectionInterceptor(); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(hostSelectionInterceptor) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://www.github.com/") .callFactory(okHttpClient) .build(); Pop pop = retrofit.create(Pop.class); Response<ResponseBody> response1 = pop.robots().execute(); System.out.println("Response from: " + response1.raw().request().url()); System.out.println(response1.body().string()); hostSelectionInterceptor.setHost("www.pepsi.com"); Response<ResponseBody> response2 = pop.robots().execute(); System.out.println("Response from: " + response2.raw().request().url()); System.out.println(response2.body().string()); } }
void remove(Request request) throws IOException { cache.remove(key(request.url())); }
/** * Returns the {@link CacheResponse} from the delegate by converting the OkHttp {@link Request} * into the arguments required by the {@link ResponseCache}. */ private CacheResponse getJavaCachedResponse(Request request) throws IOException { Map<String, List<String>> headers = JavaApiConverter.extractJavaHeaders(request); return delegate.get(request.url().uri(), request.method(), headers); } }
@Override public void onResponse(Call<Page> call, Response<Page> response) { if (!response.isSuccessful()) { System.out.println(call.request().url() + ": failed: " + response.code()); return; } // Print this page's URL and title. Page page = response.body(); HttpUrl base = response.raw().request().url(); System.out.println(base + ": " + page.title); // Enqueue its links for visiting. for (String link : page.links) { HttpUrl linkUrl = base.resolve(link); if (linkUrl != null && fetchedUrls.add(linkUrl)) { crawlPage(linkUrl); } } }
@Override public CacheRequest put(Response response) throws IOException { URI uri = response.request().url().uri(); HttpURLConnection connection = JavaApiConverter.createJavaUrlConnectionForCachePut(response); final java.net.CacheRequest request = delegate.put(uri, connection); if (request == null) { return null; } return new CacheRequest() { @Override public Sink body() throws IOException { OutputStream body = request.getBody(); return body != null ? Okio.sink(body) : null; } @Override public void abort() { request.abort(); } }; }
/** * Returns true if an HTTP request for {@code followUp} can reuse the connection used by this * engine. */ private boolean sameConnection(Response response, HttpUrl followUp) { HttpUrl url = response.request().url(); return url.host().equals(followUp.host()) && url.port() == followUp.port() && url.scheme().equals(followUp.scheme()); } }
@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())); }