List of usage examples for com.squareup.okhttp OkHttpClient setProxySelector
public OkHttpClient setProxySelector(ProxySelector proxySelector)
From source file:com.android.mms.service_alt.MmsHttpClient.java
License:Apache License
/** * Open an HTTP connection// w ww. ja va 2s . com * * TODO: The following code is borrowed from android.net.Network.openConnection * Once that method supports proxy, we should use that instead * Also we should remove the associated HostResolver and ConnectionPool from * MmsNetworkManager * * @param url The URL to connect to * @param proxy The proxy to use * @return The opened HttpURLConnection * @throws MalformedURLException If URL is malformed */ private HttpURLConnection openConnection(URL url, final Proxy proxy) throws MalformedURLException { final String protocol = url.getProtocol(); OkHttpClient okHttpClient; if (protocol.equals("http")) { okHttpClient = new OkHttpClient(); okHttpClient.setFollowRedirects(false); okHttpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1)); okHttpClient.setProxySelector(new ProxySelector() { @Override public List<Proxy> select(URI uri) { if (proxy != null) { return Arrays.asList(proxy); } else { return new ArrayList<Proxy>(); } } @Override public void connectFailed(URI uri, SocketAddress address, IOException failure) { } }); okHttpClient.setAuthenticator(new com.squareup.okhttp.Authenticator() { @Override public Request authenticate(Proxy proxy, Response response) throws IOException { return null; } @Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException { return null; } }); okHttpClient.setConnectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT)); okHttpClient.setConnectionPool(new ConnectionPool(3, 60000)); okHttpClient.setSocketFactory(SocketFactory.getDefault()); Internal.instance.setNetwork(okHttpClient, mHostResolver); if (proxy != null) { okHttpClient.setProxy(proxy); } return new HttpURLConnectionImpl(url, okHttpClient); } else if (protocol.equals("https")) { okHttpClient = new OkHttpClient(); okHttpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1)); HostnameVerifier verifier = HttpsURLConnection.getDefaultHostnameVerifier(); okHttpClient.setHostnameVerifier(verifier); okHttpClient.setSslSocketFactory(HttpsURLConnection.getDefaultSSLSocketFactory()); okHttpClient.setProxySelector(new ProxySelector() { @Override public List<Proxy> select(URI uri) { return Arrays.asList(proxy); } @Override public void connectFailed(URI uri, SocketAddress address, IOException failure) { } }); okHttpClient.setAuthenticator(new com.squareup.okhttp.Authenticator() { @Override public Request authenticate(Proxy proxy, Response response) throws IOException { return null; } @Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException { return null; } }); okHttpClient.setConnectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT)); okHttpClient.setConnectionPool(new ConnectionPool(3, 60000)); Internal.instance.setNetwork(okHttpClient, mHostResolver); return new HttpsURLConnectionImpl(url, okHttpClient); } else { throw new MalformedURLException("Invalid URL or unrecognized protocol " + protocol); } }
From source file:io.apiman.gateway.platforms.servlet.connectors.HttpConnectorFactory.java
License:Apache License
/** * @return a new http client//ww w .ja v a2 s .com */ private OkHttpClient createHttpClient() { OkHttpClient client = new OkHttpClient(); client.setReadTimeout(connectorOptions.getReadTimeout(), TimeUnit.SECONDS); client.setWriteTimeout(connectorOptions.getWriteTimeout(), TimeUnit.SECONDS); client.setConnectTimeout(connectorOptions.getConnectTimeout(), TimeUnit.SECONDS); client.setFollowRedirects(connectorOptions.isFollowRedirects()); client.setFollowSslRedirects(connectorOptions.isFollowRedirects()); client.setProxySelector(ProxySelector.getDefault()); client.setCookieHandler(CookieHandler.getDefault()); client.setCertificatePinner(CertificatePinner.DEFAULT); client.setAuthenticator(AuthenticatorAdapter.INSTANCE); client.setConnectionPool(ConnectionPool.getDefault()); client.setProtocols(Util.immutableList(Protocol.HTTP_1_1)); client.setConnectionSpecs(DEFAULT_CONNECTION_SPECS); client.setSocketFactory(SocketFactory.getDefault()); Internal.instance.setNetwork(client, Network.DEFAULT); return client; }