Search in sources :

Example 1 with HttpURLConnectionImpl

use of com.squareup.okhttp.internal.huc.HttpURLConnectionImpl in project qksms by moezbhatti.

the class MmsHttpClient method openConnection.

/**
 * Open an HTTP connection
 *
 * 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);
    }
}
Also used : ConnectionPool(com.squareup.okhttp.ConnectionPool) MalformedURLException(java.net.MalformedURLException) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) URI(java.net.URI) HttpsURLConnectionImpl(com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl) HostnameVerifier(javax.net.ssl.HostnameVerifier) ProxySelector(java.net.ProxySelector) Response(com.squareup.okhttp.Response) Proxy(java.net.Proxy) HttpURLConnectionImpl(com.squareup.okhttp.internal.huc.HttpURLConnectionImpl) ArrayList(java.util.ArrayList) List(java.util.List) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

ConnectionPool (com.squareup.okhttp.ConnectionPool)1 OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Request (com.squareup.okhttp.Request)1 Response (com.squareup.okhttp.Response)1 HttpURLConnectionImpl (com.squareup.okhttp.internal.huc.HttpURLConnectionImpl)1 HttpsURLConnectionImpl (com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 MalformedURLException (java.net.MalformedURLException)1 Proxy (java.net.Proxy)1 ProxySelector (java.net.ProxySelector)1 SocketAddress (java.net.SocketAddress)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1