use of javax.net.ssl.HostnameVerifier in project cordova-android-chromeview by thedracle.
the class HttpEngine method connect.
/** Connect to the origin server either directly or via a proxy. */
protected final void connect() throws IOException {
if (connection != null) {
return;
}
if (routeSelector == null) {
String uriHost = uri.getHost();
if (uriHost == null) {
throw new UnknownHostException(uri.toString());
}
SSLSocketFactory sslSocketFactory = null;
HostnameVerifier hostnameVerifier = null;
if (uri.getScheme().equalsIgnoreCase("https")) {
sslSocketFactory = policy.sslSocketFactory;
hostnameVerifier = policy.hostnameVerifier;
}
Address address = new Address(uriHost, getEffectivePort(uri), sslSocketFactory, hostnameVerifier, policy.requestedProxy);
routeSelector = new RouteSelector(address, uri, policy.proxySelector, policy.connectionPool, Dns.DEFAULT, policy.getFailedRoutes());
}
connection = routeSelector.next();
if (!connection.isConnected()) {
connection.connect(policy.getConnectTimeout(), policy.getReadTimeout(), getTunnelConfig());
policy.connectionPool.maybeShare(connection);
policy.getFailedRoutes().remove(connection.getRoute());
}
connected(connection);
if (connection.getRoute().getProxy() != policy.requestedProxy) {
// Update the request line if the proxy changed; it may need a host name.
requestHeaders.getHeaders().setRequestLine(getRequestLine());
}
}
use of javax.net.ssl.HostnameVerifier in project jedis by xetorthio.
the class SSLJedisTest method connectWithShardInfoAndCustomHostnameVerifier.
/**
* Tests opening an SSL/TLS connection to redis with a custom hostname
* verifier.
*/
@Test
public void connectWithShardInfoAndCustomHostnameVerifier() {
final URI uri = URI.create("rediss://localhost:6390");
final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
final SSLParameters sslParameters = new SSLParameters();
HostnameVerifier hostnameVerifier = new BasicHostnameVerifier();
JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
shardInfo.setPassword("foobared");
Jedis jedis = new Jedis(shardInfo);
jedis.get("foo");
jedis.disconnect();
jedis.close();
}
use of javax.net.ssl.HostnameVerifier in project NoHttp by yanzhenjie.
the class URLConnectionNetworkExecutor method execute.
@Override
public Network execute(IBasicRequest request) throws Exception {
URL url = new URL(request.url());
HttpURLConnection connection;
Proxy proxy = request.getProxy();
if (proxy == null)
connection = (HttpURLConnection) url.openConnection();
else
connection = (HttpURLConnection) url.openConnection(proxy);
connection.setConnectTimeout(request.getConnectTimeout());
connection.setReadTimeout(request.getReadTimeout());
connection.setInstanceFollowRedirects(false);
if (connection instanceof HttpsURLConnection) {
SSLSocketFactory sslSocketFactory = request.getSSLSocketFactory();
if (sslSocketFactory != null)
((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
HostnameVerifier hostnameVerifier = request.getHostnameVerifier();
if (hostnameVerifier != null)
((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier);
}
// Base attribute
connection.setRequestMethod(request.getRequestMethod().toString());
connection.setDoInput(true);
boolean isAllowBody = isAllowBody(request.getRequestMethod());
connection.setDoOutput(isAllowBody);
// Adds all request header to connection.
Headers headers = request.headers();
// To fix bug: accidental EOFException before API 19.
List<String> values = headers.getValues(Headers.HEAD_KEY_CONNECTION);
if (values == null || values.size() == 0)
headers.set(Headers.HEAD_KEY_CONNECTION, Build.VERSION.SDK_INT > AndroidVersion.KITKAT ? Headers.HEAD_VALUE_CONNECTION_KEEP_ALIVE : Headers.HEAD_VALUE_CONNECTION_CLOSE);
if (isAllowBody) {
long contentLength = request.getContentLength();
if (contentLength < Integer.MAX_VALUE)
connection.setFixedLengthStreamingMode((int) contentLength);
else if (Build.VERSION.SDK_INT >= AndroidVersion.KITKAT)
try {
Class<?> connectionClass = connection.getClass();
Method setFixedLengthStreamingModeMethod = connectionClass.getMethod("setFixedLengthStreamingMode", long.class);
setFixedLengthStreamingModeMethod.invoke(connection, contentLength);
} catch (Throwable e) {
connection.setChunkedStreamingMode(256 * 1024);
}
else
connection.setChunkedStreamingMode(256 * 1024);
headers.set(Headers.HEAD_KEY_CONTENT_LENGTH, Long.toString(contentLength));
}
Map<String, String> requestHeaders = headers.toRequestHeaders();
for (Map.Entry<String, String> headerEntry : requestHeaders.entrySet()) {
String headKey = headerEntry.getKey();
String headValue = headerEntry.getValue();
Logger.i(headKey + ": " + headValue);
connection.setRequestProperty(headKey, headValue);
}
// 5. Connect
connection.connect();
return new URLConnectionNetwork(connection);
}
use of javax.net.ssl.HostnameVerifier in project CloudReader by youlookwhat.
the class HttpUtils method getUnsafeOkHttpClient.
public OkHttpClient getUnsafeOkHttpClient() {
try {
final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
} };
// Install the all-trusting trust manager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new SecureRandom());
// Create an ssl socket factory with our all-trusting manager
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
okBuilder.readTimeout(20, TimeUnit.SECONDS);
okBuilder.connectTimeout(10, TimeUnit.SECONDS);
okBuilder.writeTimeout(20, TimeUnit.SECONDS);
okBuilder.addInterceptor(new HttpHeadInterceptor());
okBuilder.addInterceptor(getInterceptor());
okBuilder.sslSocketFactory(sslSocketFactory);
okBuilder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Log.d("HttpUtils", "==come");
return true;
}
});
return okBuilder.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javax.net.ssl.HostnameVerifier in project jedis by xetorthio.
the class SSLJedisTest method connectWithShardInfoAndCustomHostnameVerifierByIpAddress.
/**
* Tests opening an SSL/TLS connection to redis with a custom hostname
* verifier. This test should fail because "127.0.0.1" does not match the
* certificate subject common name and there are no subject alternative names
* in the certificate.
*/
@Test
public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() {
final URI uri = URI.create("rediss://127.0.0.1:6390");
final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
final SSLParameters sslParameters = new SSLParameters();
HostnameVerifier hostnameVerifier = new BasicHostnameVerifier();
JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
shardInfo.setPassword("foobared");
Jedis jedis = new Jedis(shardInfo);
try {
jedis.get("foo");
Assert.fail("The code did not throw the expected JedisConnectionException.");
} catch (JedisConnectionException e) {
Assert.assertEquals("The JedisConnectionException does not contain the expected message.", "The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage());
}
try {
jedis.close();
} catch (Throwable e1) {
// Expected.
}
}
Aggregations