吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1456|回复: 1
收起左侧

[Java 转载] httpclien调用skipHttpsUtil得wrapClient方法跳过https证书验证

[复制链接]
mallorJava 发表于 2021-11-3 12:28
  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) {

    }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

1000个++ 发表于 2021-11-3 21:45
试试看看
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-2 10:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表