public WebDavSocketFactory(TrustManagerFactory trustManagerFactory, String host, int port) throws NoSuchAlgorithmException, KeyManagementException { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[] { trustManagerFactory.getTrustManagerForDomain(host, port) }, null); mSocketFactory = sslContext.getSocketFactory(); mSchemeSocketFactory = org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(); mSchemeSocketFactory.setHostnameVerifier( org.apache.http.conn.ssl.SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); }
public ClientConnectionManager getSCCM() { KeyStore trustStore; try { trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new TrustAllSSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new SingleClientConnManager(null, registry); return ccm; } catch (Exception e) { return null; } }
public ClientConnectionManager getTSCCM(HttpParams params) { KeyStore trustStore; try { trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new TrustAllSSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return ccm; } catch (Exception e) { return null; } }
ctx.init(null, new TrustManager[] {tm}, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx); ssf.setHostnameVerifier(verifier); ClientConnectionManager ccm = base.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry();
/** * 默认httpclient * * @see <a * href="https://issues.apache.org/jira/browse/HTTPCLIENT-1193">HTTPCLIENT-1193</a> * @param clientConnectionManager */ public HttpComponent4_1Factory() { PoolingClientConnectionManager clientConnectionManager = new PoolingClientConnectionManager(); clientConnectionManager.setMaxTotal(30); clientConnectionManager.setDefaultMaxPerRoute(clientConnectionManager .getMaxTotal()); httpClient = new DefaultHttpClient(clientConnectionManager); httpClient.getParams().setParameter( CoreProtocolPNames.HTTP_CONTENT_CHARSET, Consts.UTF_8); httpClient.getParams().setParameter( CoreProtocolPNames.HTTP_ELEMENT_CHARSET, Consts.UTF_8.name()); httpClient.getParams().setParameter( CoreProtocolPNames.STRICT_TRANSFER_ENCODING, Consts.UTF_8); httpClient.getParams().setParameter(HttpHeaders.CONTENT_ENCODING, Consts.UTF_8); httpClient.getParams().setParameter(HttpHeaders.ACCEPT_CHARSET, Consts.UTF_8); SSLSocketFactory socketFactory = new SSLSocketFactory( HttpClientFactory.allowSSLContext()); socketFactory .setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme scheme = new Scheme("https", socketFactory, 443); httpClient.getConnectionManager().getSchemeRegistry().register(scheme); }
public static ThreadSafeClientConnManager createThreadSafeClientConnManager( ClientConfiguration config, HttpParams httpClientParams) { ConnManagerParams.setMaxConnectionsPerRoute(httpClientParams, new ConnPerRouteBean(config.getMaxConnections())); ConnManagerParams.setMaxTotalConnections(httpClientParams, config.getMaxConnections()); SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), DEFAULT_HTTP_PORT)); registry.register(new Scheme("https", sslSocketFactory, DEFAULT_HTTPS_PORT)); ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager( httpClientParams, registry); if (config.useReaper()) { IdleConnectionReaper.registerConnectionManager(connectionManager); } return connectionManager; } }
/** * Constructor. * @param config the client configuration. */ public ApacheHttpClient(ClientConfiguration config) { HttpClientFactory httpClientFactory = new HttpClientFactory(); httpClient = httpClientFactory.createHttpClient(config); // disable retry ((AbstractHttpClient) httpClient) .setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry(); Scheme https = schemeRegistry.getScheme("https"); ((SSLSocketFactory) https.getSocketFactory()) .setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); }
private void resolveHttpParams(HttpParams params) { if (params != null) { if (params.getProxy() != null) { InetSocketAddress socketAddress = (InetSocketAddress) params .getProxy().address(); HttpHost proxy = new HttpHost(socketAddress.getHostName(), socketAddress.getPort()); httpClient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy); } httpClient.getParams().setIntParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, params.getConnectTimeout()); httpClient.getParams().setIntParameter( CoreConnectionPNames.SO_TIMEOUT, params.getReadTimeout()); if (params.getSSLContext() != null) { SSLSocketFactory socketFactory = new SSLSocketFactory( params.getSSLContext()); if (params.getHostnameVerifier() != null) { socketFactory .setHostnameVerifier(new CustomHostnameVerifier( params.getHostnameVerifier())); } Scheme scheme = new Scheme("https", socketFactory, 443); httpClient.getConnectionManager().getSchemeRegistry() .register(scheme); } } }
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); final Scheme https = new Scheme("https", sslSocketFactory, HTTPS_PORT); final SchemeRegistry sr = connectionManager.getSchemeRegistry();
public static DefaultHttpClient wrapClient(HttpClient base, SSLContext ctx) { SSLSocketFactory ssf = new SSLSocketFactory(ctx); ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = base.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", ssf, 443)); return new DefaultHttpClient(ccm, base.getParams()); } }
private static HttpClient generateHttpClient() { HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, 1); HttpConnectionParams .setConnectionTimeout(params, (int) CommonConstants.DEFAULT_CLIENT_CONN_TIMEOUT); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); schemeRegistry.register(new Scheme("https", sslSocketFactory, 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schemeRegistry); return new DefaultHttpClient(conMgr, params); }
private static HttpClient generateHttpClient() { HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, 1); HttpConnectionParams .setConnectionTimeout(params, (int) CommonConstants.DEFAULT_CLIENT_CONN_TIMEOUT); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); schemeRegistry.register(new Scheme("https", sslSocketFactory, 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schemeRegistry); return new DefaultHttpClient(conMgr, params); }
private static HttpClient generateHttpClient() { HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, 1); HttpConnectionParams .setConnectionTimeout(params, (int) CommonConstants.DEFAULT_CLIENT_CONN_TIMEOUT); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); schemeRegistry.register(new Scheme("https", sslSocketFactory, 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schemeRegistry); return new DefaultHttpClient(conMgr, params); }
@Override public CloseableHttpClient newInstance() { try { final SSLSocketFactory socketFactory = new SSLSocketFactory(this.getSSLContext()); socketFactory.setHostnameVerifier(new UnsafeHostNameVerifier()); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https", 443, socketFactory)); final ClientConnectionManager clientConnectionManager = new BasicClientConnectionManager(schemeRegistry); return new DefaultHttpClient(clientConnectionManager); } catch (final KeyManagementException | NoSuchAlgorithmException ex) { throw new RuntimeException("Unable to instantiate HTTP Client", ex); } }
@Override public CloseableHttpClient newInstance() { try { final SSLSocketFactory socketFactory = new SSLSocketFactory(this.getSSLContext()); socketFactory.setHostnameVerifier(new UnsafeHostNameVerifier()); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https", 443, socketFactory)); final ClientConnectionManager clientConnectionManager = new BasicClientConnectionManager(schemeRegistry); return new DefaultHttpClient(clientConnectionManager); } catch (final KeyManagementException | NoSuchAlgorithmException ex) { throw new RuntimeException("Unable to instantiate HTTP Client", ex); } }
private SchemeRegistry createRegistry(CouchDbProperties properties) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, CertificateException, IOException, UnrecoverableKeyException { SchemeRegistry registry = new SchemeRegistry(); if("https".equals(properties.getProtocol())) { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new MySSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); registry.register(new Scheme(properties.getProtocol(), sf, properties.getPort())); } else { registry.register(new Scheme(properties.getProtocol(), PlainSocketFactory.getSocketFactory(), properties.getPort())); } return registry; }
public static SSLSocketFactory getFixedSocketFactory() { SSLSocketFactory socketFactory; try { socketFactory = new MySSLSocketFactory(getKeystore()); socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } catch (Throwable t) { t.printStackTrace(); socketFactory = SSLSocketFactory.getSocketFactory(); } return socketFactory; }
public static void setHostNameVerifier(DefaultHttpClient httpClient, X509HostnameVerifier hostNameVerifier) { Scheme httpsScheme = httpClient.getConnectionManager().getSchemeRegistry().get("https"); if (httpsScheme != null) { SSLSocketFactory sslSocketFactory = (SSLSocketFactory) httpsScheme.getSchemeSocketFactory(); sslSocketFactory.setHostnameVerifier(hostNameVerifier); } }
/** * Constructor. * @param config the client configuration. */ public ApacheHttpClient(ClientConfiguration config) { HttpClientFactory httpClientFactory = new HttpClientFactory(); httpClient = httpClientFactory.createHttpClient(config); // disable retry ((AbstractHttpClient) httpClient) .setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry(); Scheme https = schemeRegistry.getScheme("https"); ((SSLSocketFactory) https.getSocketFactory()) .setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); }
/** * Constructor. * @param config the client configuration. */ public ApacheHttpClient(ClientConfiguration config) { HttpClientFactory httpClientFactory = new HttpClientFactory(); httpClient = httpClientFactory.createHttpClient(config); // disable retry ((AbstractHttpClient) httpClient) .setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry(); Scheme https = schemeRegistry.getScheme("https"); ((SSLSocketFactory) https.getSocketFactory()) .setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); }