成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

httpclient訪問(wèn)https

waruqi / 734人閱讀

摘要:序本文從里頭摘出訪問(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。

newConnectionManager
protected 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());

            RegistryBuilder registryBuilder = 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

相關(guān)文章

  • 構(gòu)建https協(xié)議的webService并使用httpClient接口訪問(wèn)

    摘要:組件版本信息使用自帶的命令生成文件命令將拷貝到目錄下配置的目錄文件,在配置文件中新增配置將工程添加進(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...

    Tony_Zby 評(píng)論0 收藏0
  • httpClient ignore SSL post https

    摘要:如果服務(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...

    xfee 評(píng)論0 收藏0
  • java9系列(六)HTTP/2 Client (Incubator)

    摘要:鑒于它還處在,如果不是著急使用,建議還是使用的,它是遵循規(guī)范的,使用起來(lái)更加方便。貌似要在版本才支持。揭秘讓支持協(xié)議如何啟用命令支持 序 本文主要研究下JEP 110: HTTP/2 Client (Incubator) 基本實(shí)例 sync get /** * --add-modules jdk.incubator.httpclient * @throws ...

    tomlingtm 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<