• java httpclient跳过https证书验证


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



    //POST 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; } }


    //GET

    public static String doGet(String strUrl ){  
            String strReturn="";   
            HttpGet httpGet = new HttpGet(strUrl);  
            CloseableHttpClient httpclient = null;   
            CloseableHttpResponse response1=null;  
            try {  
                httpclient = (CloseableHttpClient)wrapClient();//HttpClients.createDefault();  
                response1 = httpclient.execute(httpGet);   
                HttpEntity entity1 = response1.getEntity();   
                strReturn=EntityUtils.toString(entity1) ;  
                EntityUtils.consume(entity1);   
            }catch(Exception e){   
                e.printStackTrace();  
            }finally {    
                    try {  
                         if(response1!=null)  
                        response1.close();  
                    } catch (IOException e) {   
                        e.printStackTrace();  
                    }  
            }  
            System.out.println(strReturn);
            return strReturn;  
        }
    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; /**
    * skipHttpsUtil类
    * Description: httpclient跳过https验证 * @author: hsw * @CreateDate: 2020-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) { } }
    你所拥抱的并不总是拥抱你
  • 相关阅读:
    对websoceket进行压力测试(一)
    学习springboot的一个网站
    重装mysql数据库
    websocket扫盲:基础知识(二)
    json-lib 之jsonConfig详细使用
    hibernate的like用法(用占位符解决)
    【转载】hibernate查询参数绑定
    Struts2 Anotation action
    PLSQL怎样导出oracle表结构
    从命令行启动oracle服务
  • 原文地址:https://www.cnblogs.com/java-h/p/14365841.html
Copyright © 2020-2023  润新知