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);
}
}
Aggregations