@Override public void setReadTimeout(int timeoutMillis) { client = client.newBuilder() .readTimeout(timeoutMillis, TimeUnit.MILLISECONDS) .build(); }
@Override public void setConnectTimeout(int timeoutMillis) { client = client.newBuilder() .connectTimeout(timeoutMillis, TimeUnit.MILLISECONDS) .build(); }
/** * Set the underlying write timeout in milliseconds. * A value of 0 specifies an infinite timeout. */ public void setWriteTimeout(int writeTimeout) { this.client = this.client.newBuilder() .writeTimeout(writeTimeout, TimeUnit.MILLISECONDS) .build(); }
/** * Set the underlying read timeout in milliseconds. * A value of 0 specifies an infinite timeout. */ public void setReadTimeout(int readTimeout) { this.client = this.client.newBuilder() .readTimeout(readTimeout, TimeUnit.MILLISECONDS) .build(); }
/** * Set the underlying connect timeout in milliseconds. * A value of 0 specifies an infinite timeout. */ public void setConnectTimeout(int connectTimeout) { this.client = this.client.newBuilder() .connectTimeout(connectTimeout, TimeUnit.MILLISECONDS) .build(); }
@Override public void setInstanceFollowRedirects(boolean followRedirects) { client = client.newBuilder() .followRedirects(followRedirects) .build(); }
@Override public void setHostnameVerifier(HostnameVerifier hostnameVerifier) { delegate.client = delegate.client.newBuilder() .hostnameVerifier(hostnameVerifier) .build(); }
@Override public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) { if (sslSocketFactory == null) { throw new IllegalArgumentException("sslSocketFactory == null"); } // This fails in JDK 9 because OkHttp is unable to extract the trust manager. delegate.client = delegate.client.newBuilder() .sslSocketFactory(sslSocketFactory) .build(); }
@Override public HttpParams setParameter(String name, Object value) { if (name.equals(ConnRouteParams.DEFAULT_PROXY)) { HttpHost host = (HttpHost) value; Proxy proxy = null; if (host != null) { proxy = new Proxy(HTTP, new InetSocketAddress(host.getHostName(), host.getPort())); } client = client.newBuilder() .proxy(proxy) .build(); return this; } throw new IllegalArgumentException(name); }
/** * Set the underlying read timeout in milliseconds. * A value of 0 specifies an infinite timeout. */ public void setReadTimeout(int readTimeout) { this.client = this.client.newBuilder() .readTimeout(readTimeout, TimeUnit.MILLISECONDS) .build(); }
/** * Set the underlying write timeout in milliseconds. * A value of 0 specifies an infinite timeout. */ public void setWriteTimeout(int writeTimeout) { this.client = this.client.newBuilder() .writeTimeout(writeTimeout, TimeUnit.MILLISECONDS) .build(); }
/** * Set the underlying connect timeout in milliseconds. * A value of 0 specifies an infinite timeout. */ public void setConnectTimeout(int connectTimeout) { this.client = this.client.newBuilder() .connectTimeout(connectTimeout, TimeUnit.MILLISECONDS) .build(); }
public void run() throws Exception { Request request = new Request.Builder() .url("http://httpbin.org/delay/1") // This URL is served with a 1 second delay. .build(); // Copy to customize OkHttp for this request. OkHttpClient client1 = client.newBuilder() .readTimeout(500, TimeUnit.MILLISECONDS) .build(); try (Response response = client1.newCall(request).execute()) { System.out.println("Response 1 succeeded: " + response); } catch (IOException e) { System.out.println("Response 1 failed: " + e); } // Copy to customize OkHttp for this request. OkHttpClient client2 = client.newBuilder() .readTimeout(3000, TimeUnit.MILLISECONDS) .build(); try (Response response = client2.newCall(request).execute()) { System.out.println("Response 2 succeeded: " + response); } catch (IOException e) { System.out.println("Response 2 failed: " + e); } }
HttpURLConnection open(URL url, @Nullable Proxy proxy) { String protocol = url.getProtocol(); OkHttpClient copy = client.newBuilder() .proxy(proxy) .build(); if (protocol.equals("http")) return new OkHttpURLConnection(url, copy, urlFilter); if (protocol.equals("https")) return new OkHttpsURLConnection(url, copy, urlFilter); throw new IllegalArgumentException("Unexpected protocol: " + protocol); }
private StatementClient startInternalQuery(ClientSession session, String query) { OkHttpClient.Builder builder = httpClient.newBuilder(); sslSetup.accept(builder); OkHttpClient client = builder.build(); return newStatementClient(client, session, query); }
DnsOverHttps(Builder builder) { if (builder.client == null) { throw new NullPointerException("client not set"); } if (builder.url == null) { throw new NullPointerException("url not set"); } this.url = builder.url; this.includeIPv6 = builder.includeIPv6; this.post = builder.post; this.resolvePrivateAddresses = builder.resolvePrivateAddresses; this.resolvePublicAddresses = builder.resolvePublicAddresses; this.client = builder.client.newBuilder().dns(buildBootstrapClient(builder)).build(); }
public void connect(OkHttpClient client) { client = client.newBuilder() .eventListener(EventListener.NONE) .build(); call = client.newCall(request); call.timeout().clearTimeout(); call.enqueue(this); }
public void run() throws Exception { for (int i = 0; i < 5; i++) { System.out.println(" Request: " + i); Request request = new Request.Builder() .url("https://api.github.com/search/repositories?q=http") .build(); OkHttpClient clientForCall; if (i == 2) { // Force this request's response to be written to the cache. This way, subsequent responses // can be read from the cache. System.out.println("Force cache: true"); clientForCall = client.newBuilder() .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) .build(); } else { System.out.println("Force cache: false"); clientForCall = client; } try (Response response = clientForCall.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(" Network: " + (response.networkResponse() != null)); System.out.println(); } } }
@Inject public HTTPFileRetriever(OkHttpClient httpClient) { this.client = httpClient.newBuilder() .followRedirects(true) .followSslRedirects(true) .build(); }
/** Sets the response cache to be used to read and write cached responses. */ public static void setResponseCache(OkUrlFactory okUrlFactory, ResponseCache responseCache) { OkHttpClient.Builder builder = okUrlFactory.client().newBuilder(); if (responseCache instanceof OkCacheContainer) { // Avoid adding layers of wrappers. Rather than wrap the ResponseCache in yet another layer to // make the ResponseCache look like an InternalCache, we can unwrap the Cache instead. // This means that Cache stats will be correctly updated. OkCacheContainer okCacheContainer = (OkCacheContainer) responseCache; builder.cache(okCacheContainer.getCache()); } else { builder.setInternalCache(responseCache != null ? new CacheAdapter(responseCache) : null); } okUrlFactory.setClient(builder.build()); } }