zoukankan      html  css  js  c++  java
  • java httpclient跳过https证书验证

    感谢   java httpclient跳过https证书验证 - 九条尾巴的猫 - 博客园 (cnblogs.com)

    package com.cck.common.Utils;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;

    import org.apache.http.client.HttpClient;
    import org.apache.http.conn.ssl.NoopHostnameVerifier;
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;

    public class SkipHttpsUtil {

    //绕过证书
    public static HttpClient wrapClient() {
    try {
    SSLContext ctx = SSLContext.getInstance("TLS");
    X509TrustManager tm = new X509TrustManager() {
    public X509Certificate[] getAcceptedIssuers() {
    return null;
    }

    public void checkClientTrusted(X509Certificate[] arg0,
    String arg1) throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] arg0,
    String arg1) throws CertificateException {
    }
    };
    ctx.init(null, new TrustManager[] { tm }, null);
    SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(
    ctx, NoopHostnameVerifier.INSTANCE);
    CloseableHttpClient httpclient = HttpClients.custom()
    .setSSLSocketFactory(ssf).build();
    return httpclient;
    } catch (Exception e) {
    return HttpClients.createDefault();
    }
    }



    }










    public  String sendHttpPost(String url, String regJson) throws Exception {
    SkipHttpsUtil skipHttpsUtil=new SkipHttpsUtil();

    CloseableHttpClient httpClient =(CloseableHttpClient)skipHttpsUtil.wrapClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("Content-Type", "application/json");
    httpPost.setEntity(new StringEntity(regJson,"UTF-8")); //防止中文乱码

    CloseableHttpResponse response = httpClient.execute(httpPost);
    HttpEntity entity = response.getEntity();
    String responseContent = EntityUtils.toString(entity, "UTF-8");
    response.close();
    httpClient.close();
    return responseContent;
    }













  • 相关阅读:
    Linq to OBJECT延时标准查询操作符
    LINQ to XML
    动态Linq(结合反射)
    HDU 1242 dFS 找目标最短路
    HDu1241 DFS搜索
    hdu 1224 最长路
    BOJ 2773 第K个与m互质的数
    ZOJ 2562 反素数
    2016 ccpc 杭州赛区的总结
    bfs UESTC 381 Knight and Rook
  • 原文地址:https://www.cnblogs.com/cjb1/p/14025145.html
Copyright © 2011-2022 走看看