private void run() { OkHttpClient client = new OkHttpClient.Builder() .readTimeout(0, TimeUnit.MILLISECONDS) .build(); Request request = new Request.Builder() .url("ws://echo.websocket.org") .build(); client.newWebSocket(request, this); // Trigger shutdown of the dispatcher's executor so this process can exit cleanly. client.dispatcher().executorService().shutdown(); }
public CacheResponse(File cacheDirectory) throws Exception { int cacheSize = 10 * 1024 * 1024; // 10 MiB Cache cache = new Cache(cacheDirectory, cacheSize); client = new OkHttpClient.Builder() .cache(cache) .build(); }
public CertificatePinning() { client = new OkHttpClient.Builder() .certificatePinner( new CertificatePinner.Builder() .add("publicobject.com", "sha256/afwiKY3RxoMmLkuRW1l7QsPZTJPwDS2pdDROQjXw8ig=") .build()) .build(); }
@Override public void setConnectTimeout(int timeoutMillis) { client = client.newBuilder() .connectTimeout(timeoutMillis, TimeUnit.MILLISECONDS) .build(); }
private static Response stripBody(Response response) { return response != null && response.body() != null ? response.newBuilder().body(null).build() : response; }
@Override public void setHostnameVerifier(HostnameVerifier hostnameVerifier) { delegate.client = delegate.client.newBuilder() .hostnameVerifier(hostnameVerifier) .build(); }
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(); }
/** * Returns a string with containing this URL with its username, password, query, and fragment * stripped, and its path replaced with {@code /...}. For example, redacting {@code * http://username:password@example.com/path} returns {@code http://example.com/...}. */ public String redact() { return newBuilder("/...") .username("") .password("") .build() .toString(); }
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; }
/** * Adds all headers from an existing collection. */ public Builder addAll(Headers headers) { for (int i = 0, size = headers.size(); i < size; i++) { addLenient(headers.name(i), headers.value(i)); } return this; }
/** * Returns true if computeFreshnessLifetime used a heuristic. If we used a heuristic to serve a * cached response older than 24 hours, we are required to attach a warning. */ private boolean isFreshnessLifetimeHeuristic() { return cacheResponse.cacheControl().maxAgeSeconds() == -1 && expires == null; }
/** * Add a header with the specified name and value. Does validation of header names and values. */ public Builder add(String name, String value) { checkName(name); checkValue(value, name); return addLenient(name, value); }
static RealCall newRealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) { // Safely publish the Call instance to the EventListener. RealCall call = new RealCall(client, originalRequest, forWebSocket); call.eventListener = client.eventListenerFactory().create(call); return call; }
/** * Add a header with the specified name and value. Does validation of header names, allowing * non-ASCII values. */ public Builder addUnsafeNonAscii(String name, String value) { checkName(name); return addLenient(name, value); }
public RewriteResponseCacheControl(File cacheDirectory) throws Exception { Cache cache = new Cache(cacheDirectory, 1024 * 1024); cache.evictAll(); client = new OkHttpClient.Builder() .cache(cache) .build(); }