java httpclient跳过https证书验证

本文介绍了一种使用Apache HttpClient库跳过HTTPS证书验证的方法。通过创建一个信任所有证书的自定义X509TrustManager,并将其设置到SSLContext中,然后使用这个SSLContext创建SSLConnectionSocketFactory。最后,通过定制的HttpClients设置SSL连接工厂来实现忽略HTTPS证书验证的目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      httpclien调用skipHttpsUtil得wrapClient方法跳过https证书验证

     SkipHttpsUtil  skipHttpsUtil=new SkipHttpsUtil();
            CloseableHttpClient httpclient = null;
            CloseableHttpResponse response = null;
            try {
                httpclient =  (CloseableHttpClient)skipHttpsUtil.wrapClient();
                HttpPost post = new HttpPost(url);
                String json = "{\"image\":\""+"ddd"+
                        "\",\"Type\":\""+"ddddd"+
                        "\",\"Flag\":\""+"dddd"+"\"}";    
                StringEntity postingString = new StringEntity(json,"utf-8");// json传递  
                post.setEntity(postingString);
                post.setHeader("Content-type", "application/json");                              
                response = httpclient.execute(post);    
                String result = EntityUtils.toString(response.getEntity());
                JSONObject js=JSONObject.parseObject(result);   

        } catch (Exception e) {
                e.printStackTrace();
                mResultCode = "E";
                return false;
            } finally {
                try {
                    response.close();
                       httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    mResultCode = "E";
                    return false;
                }
                
            }

 

skipHttpsUtil类

 

package com.life.util;
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;
import org.apache.log4j.Logger;

/**
 * <p>
 * ClassName: SkipHttpsUtil
 * </p>
 * <p>
 * Description: httpclient跳过https验证
 * </p>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p>
 * Company: service
 * </p>
 * @author: maojinqiang
 * @CreateDate: 2019-08-26
 */
public class SkipHttpsUtil {
    private static Logger logger = Logger.getLogger(SkipHttpsUtil.class);
    //绕过证书
    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 static void main(String[] args) {

    }
}

转载于:https://www.cnblogs.com/jtwbdm/p/11507121.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值