java httpclient跳过https证书验证

2022-10-16,,,,

      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) {

    }
}

《java httpclient跳过https证书验证.doc》

下载本文的Word格式文档,以方便收藏与打印。