国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

httpClient ignore SSL post https

xfee / 1784人閱讀

摘要:如果服務器證書這兩者不合法而我們又必須讓其校驗通過,則可以自己實現。這個屬性是新加的屬性,因為目前版本是可以共享連接池的。請求獲取數據的超時時間,單位毫秒。如果訪問一個接口,多少時間內無法返回數據,就直接放棄此次調用。

/**


    com.alibaba
    fastjson
    1.2.47



    org.apache.httpcomponents
    httpclient
    4.5.9



    commons-lang
    commons-lang
    2.6


    org.projectlombok
    lombok
    1.16.18






*/

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

public class httpClientUtil {

//采用繞過驗證的方式處理https請求
   // SSLContext sslcontext = createIgnoreVerifySSL();
public static CloseableHttpClient getIgnoeSSLClient() throws Exception {
    SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {

        public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            return true;
        }
    }).build();



    //CloseableHttpClient client = HttpClients.createDefault();*/

    //設置協議http和https對應的處理socket鏈接工廠的對象

    Registry socketFactoryRegistry = RegistryBuilder.create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", new SSLConnectionSocketFactory(sslContext))
            .build();

    //HttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(rrrr);

    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);



    //創建httpClient
    CloseableHttpClient client = HttpClients.custom()
                                            .setSSLContext(sslContext).setConnectionManager(connManager).
                                            setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
    return client;
}


public final static void main(String[] args) throws Exception {

    CloseableHttpClient httpClient =getIgnoeSSLClient();
    CloseableHttpClient httpClient1 = null;

   // try {

  /*      // 加載自定義的keystore
       // SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new File("D:/java/IdeaProjects/test/src/main/resources/certs/my.keystore"), "123456".toCharArray()).build();

        SSLContext sslContext = createIgnoreVerifySSL();
        // 默認的域名校驗類為DefaultHostnameVerifier,比對服務器證書的AlternativeName和CN兩個屬性。
        // 如果服務器證書這兩者不合法而我們又必須讓其校驗通過,則可以自己實現HostnameVerifier。
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() {
            public boolean verify(String s, SSLSession sslSession) {
                // 我們可以重寫域名校驗邏輯
                return true;
            }
        });

        // 一個httpClient對象對于https僅會選用一個SSLConnectionSocketFactory
        // 至少在4.5.3和4.5.4中,如果給HttpClient對象設置ConnectionManager,我們必須在PoolingHttpClientConnectionManager的構造方法中傳入Registry,
        // 并將https對應的工廠設置為我們自己的SSLConnectionSocketFactory對象,因為在DefaultHttpClientConnectionOperator.connect()中,邏輯是從這里找SSLConnectionSocketFactory的。
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(RegistryBuilder.create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslConnectionSocketFactory)
                .build());
        connectionManager.setMaxTotal(20);
        connectionManager.setDefaultMaxPerRoute(20);

        httpClient1 = HttpClients.custom()
                // 不在connectionManager中注冊,僅在這里設置SSLConnectionSocketFactory是無效的,詳見build()內部邏輯,在connectionManager不為null時,不會使用里的SSLConnectionSocketFactory
                .setSSLSocketFactory(sslConnectionSocketFactory)
                .setConnectionManager(connectionManager)
                .build();*/

 /*       httpClient = getIgnoeSSLClient();


        HttpGet httpGet = new HttpGet("https://www.12306.cn");
        System.out.println("Executing request " + httpGet.getRequestLine());
        CloseableHttpResponse response = httpClient.execute(httpGet);
        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8"));

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != httpClient) {
                httpClient.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/



    String body = "";

  /*  //采用繞過驗證的方式處理https請求
    SSLContext sslcontext = createIgnoreVerifySSL();
    //設置協議http和https對應的處理socket鏈接工廠的對象
    Registry socketFactoryRegistry = RegistryBuilder.create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", new SSLConnectionSocketFactory(sslcontext))
            .build();
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    HttpClients.custom().setConnectionManager(connManager);

    //創建自定義的httpclient對象
    CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
    //CloseableHttpClient client = HttpClients.createDefault();*/

    try{
        //創建post方式請求對象
        HttpPost httpPost = new HttpPost("https://www.douban.com/service/apikey/apply");//https://api.douban.com/v2/book/1220562


        //指定報文頭Content-type、User-Agent
        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");

        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");

        //setConnectTimeout:設置連接超時時間,單位毫秒。
        //setConnectionRequestTimeout:設置從connect Manager獲取Connection 超時時間,單位毫秒。這個屬性是新加的屬性,因為目前版本是可以共享連接池的。
        //setSocketTimeout:請求獲取數據的超時時間,單位毫秒。 如果訪問一個接口,多少時間內無法返回數據,就直接放棄此次調用。


        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(5000).build();


        httpPost.setConfig( requestConfig);

        //apikey: "0b2bdeda43b5688921839c8ecb20399b"

        Map params = new HashMap();
        params.put("apikey","0b2bdeda43b5688921839c8ecb20399b");

        StringEntity stringEntity = new StringEntity(JSON.toJSONString(params),"utf-8");
        httpPost.setEntity(stringEntity);


        //執行請求操作,并拿到結果(同步阻塞)
        CloseableHttpResponse response = httpClient.execute(httpPost);

        //獲取結果實體
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定編碼轉換結果實體為String類型
            body = EntityUtils.toString(entity, "UTF-8");
        }

        EntityUtils.consume(entity);
        //釋放鏈接
        response.close();
        System.out.println("body:" + body);
    }finally{
        httpClient.close();
    }




}




public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext sc = SSLContext.getInstance("SSLv3");

    // 實現一個X509TrustManager接口,用于繞過驗證,不用修改里面的方法
    X509TrustManager trustManager = new X509TrustManager() {
        public void checkClientTrusted(
                java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                String paramString) throws CertificateException {
        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                String paramString) throws CertificateException {
        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    sc.init(null, new TrustManager[] { trustManager }, null);
    return sc;
}


}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/75739.html

相關文章

  • httpclient訪問https

    摘要:序本文從里頭摘出訪問的源碼,展示一下怎么用去訪問。其中指定要不要檢驗,如果不校驗,則是使用小結使用不去驗證,但是可能存在風險構造 序 本文從spring cloud netflix zuul里頭摘出httpclient訪問https/http的源碼,展示一下怎么用httpclient去訪問https。 newConnectionManager protected PoolingHttp...

    waruqi 評論0 收藏0
  • 記一次線程掛死的排查過程(附 HttpClient 配置建議)

    摘要:此時我想到了福爾摩斯說過的一句話當你排除掉各種不可能出現的情況之后,剩下的情況無論多么難以置信,都是真相。福爾摩斯冷靜下來想一想,這個線程,有可能靜悄悄地退出了嗎,沒留下半點異常日志從理論上來說,不可能。配置建議最后,附上一份配置建議。 1、事發 我們有個視頻處理程序,基于 SpringBoot,會啟動幾個線程來跑。要退出程序時,會發送一個信號給程序,每個線程收到信號后會平滑退出,等全...

    jollywing 評論0 收藏0
  • HttpCilent 發送 POST Http以及Https請求,簡單封裝

    摘要:問題最近項目上面遇到和對接,測試庫他們使用的是,但是正式庫使用的是。之前沒有做過,在網上找過好多文章,最后還是借鑒別人的,自己重新封裝之后才在項目上面使用。以上是我在項目上面使用的,沒有添加全部的代碼,但是對應的方法都是可以使用的。 問題: 最近項目上面遇到和App對接,測試庫他們使用的是 HTTP,但是正式庫使用的是 HTTPS。之前沒有做過,在網上找過好多文章,最后還是借鑒別人的,...

    Rainie 評論0 收藏0
  • PHP回顧之IO

    摘要:命令行時返回值為,標準輸入輸出均指向終端可用進程號查看。會在腳本執行完畢后關閉三個流,無需用戶手動關閉。與遠程網址交互是一個請求和響應的過程,其中細節可參考本人之前的文章回顧之請求和回顧之響應,也可參考協議的權威文檔。 轉載請注明文章出處: https://tlanyan.me/php-review... PHP回顧系列目錄 PHP基礎 web請求 cookie web響應 ses...

    happen 評論0 收藏0
  • java9系列(六)HTTP/2 Client (Incubator)

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

    tomlingtm 評論0 收藏0

發表評論

0條評論

xfee

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<