摘要:序本文從里頭摘出訪問(wèn)的源碼,展示一下怎么用去訪問(wèn)。其中指定要不要檢驗(yàn),如果不校驗(yàn),則是使用小結(jié)使用不去驗(yàn)證,但是可能存在風(fēng)險(xiǎn)構(gòu)造
序
本文從spring cloud netflix zuul里頭摘出httpclient訪問(wèn)https/http的源碼,展示一下怎么用httpclient去訪問(wèn)https。
newConnectionManagerprotected PoolingHttpClientConnectionManager newConnectionManager(boolean sslHostnameValidationEnabled) { try { final SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } } }, new SecureRandom()); RegistryBuilderregistryBuilder = RegistryBuilder . create() .register("http", PlainConnectionSocketFactory.INSTANCE); if (sslHostnameValidationEnabled) { registryBuilder.register("https", new SSLConnectionSocketFactory(sslContext)); } else { registryBuilder.register("https", new SSLConnectionSocketFactory( sslContext, NoopHostnameVerifier.INSTANCE)); } final Registry registry = registryBuilder.build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); connectionManager .setMaxTotal(200); connectionManager.setDefaultMaxPerRoute(20); return connectionManager; } catch (Exception ex) { throw new RuntimeException(ex); } }
其中sslHostnameValidationEnabled指定要不要檢驗(yàn)ssl,如果不校驗(yàn),則是使用NoopHostnameVerifier
@Contract(threading = ThreadingBehavior.IMMUTABLE) public class NoopHostnameVerifier implements HostnameVerifier { public static final NoopHostnameVerifier INSTANCE = new NoopHostnameVerifier(); @Override public boolean verify(final String s, final SSLSession sslSession) { return true; } @Override public final String toString() { return "NO_OP"; } }newClient
final RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(60000) .setConnectTimeout(60000) .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); HttpClient httpclient = httpClientBuilder.setConnectionManager(newConnectionManager(false)) .useSystemProperties().setDefaultRequestConfig(requestConfig) .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)) .setRedirectStrategy(new RedirectStrategy() { @Override public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return false; } @Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return null; } }).build();request
HttpRequest httpRequest = new BasicHttpRequest("GET","/api/data"); HttpHost httpHost = new HttpHost("demo.com.cn",-1,"https"); try{ return httpClient.execute(httpHost, httpRequest); // System.out.println(response.getEntity().getContent()); }catch (Exception e){ e.printStackTrace(); }小結(jié)
使用NoopHostnameVerifier不去驗(yàn)證ssl,但是可能存在風(fēng)險(xiǎn)
構(gòu)造X509TrustManager
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70185.html
摘要:組件版本信息使用自帶的命令生成文件命令將拷貝到目錄下配置的目錄文件,在配置文件中新增配置將工程添加進(jìn)并啟動(dòng),使用訪問(wèn)和鏈接。原理后續(xù)進(jìn)一步研究 1.組件版本信息apache-tomcat-7.0.75JDK 1.8.0_91 2.使用jdk自帶的keytool命令生成keystore文件test.keystore命令:keytool -genkey -alias test123 -ke...
摘要:如果服務(wù)器證書(shū)這兩者不合法而我們又必須讓其校驗(yàn)通過(guò),則可以自己實(shí)現(xiàn)。這個(gè)屬性是新加的屬性,因?yàn)槟壳鞍姹臼强梢怨蚕磉B接池的。請(qǐng)求獲取數(shù)據(jù)的超時(shí)時(shí)間,單位毫秒。如果訪問(wèn)一個(gè)接口,多少時(shí)間內(nèi)無(wú)法返回?cái)?shù)據(jù),就直接放棄此次調(diào)用。 /** com.alibaba fastjson 1.2.47 org.apache.httpcomponents ht...
摘要:鑒于它還處在,如果不是著急使用,建議還是使用的,它是遵循規(guī)范的,使用起來(lái)更加方便。貌似要在版本才支持。揭秘讓支持協(xié)議如何啟用命令支持 序 本文主要研究下JEP 110: HTTP/2 Client (Incubator) 基本實(shí)例 sync get /** * --add-modules jdk.incubator.httpclient * @throws ...
閱讀 1384·2021-11-25 09:43
閱讀 3605·2021-11-10 11:48
閱讀 5175·2021-09-23 11:21
閱讀 1609·2019-08-30 15:55
閱讀 3519·2019-08-30 13:53
閱讀 1245·2019-08-30 10:51
閱讀 880·2019-08-29 14:20
閱讀 1985·2019-08-29 13:11