通过HttpClient调用接口时忽略SSL证书验证

在调用HTTPS接口时遇到SSLPeerUnverifiedException异常,解决方法是在HttpClient中自定义SSL上下文和主机名验证器,跳过SSL证书验证。提供了具体的Java代码示例来展示如何实现。

在项目过程中发现调用的接口地址为https形式的,一般用httpclient调用会抛出异常:Exception in thread "main" javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated,经过查找资料发现时因为https会使用SSL数字证书验证,而服务端并没有提供相应的SSL数字证书,这就需要在调用时绕过校验,具体实现代码如下:

1.httpclient

public static void main(String[] args) throws HttpException, IOException {
HttpClient httpclient = new DefaultHttpClient(); 
httpclient = wrapClient(httpclient);
HttpPost httppost = new HttpPost(url);
 
StringEntity myEntity = new StringEntity(test(), "utf-8"); 

         httppost.addHeader("Content-Type", "text/html;charset=utf-8");
  
         httppost.setEntity(myEntity); 
        httppost.addHeader("Authorization","Bearer YWMtlZKThgG9EeSdfPG0cx2i0wAAAUcaGn61pdZR1lx5O8fYo0H8cwINV1NFExI");
         HttpResponse response = httpclient.execute(httppost); 
         HttpEntity resEntity = response.getEntity(); 
         InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "utf-8"); 
         char[] buff = new char[1024]; 
         int length = 0; 
         while ((length = reader.read(buff)) != -1) { 
                 System.out.println(new String(buff, 0, length)); 
         } 
         httpclient.getConnectionManager().shutdown();        
}

2.取消校验SSL

public static HttpClient wrapClient(HttpClient base) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] xcs,
                    String string) {
            }


            public void checkServerTrusted(X509Certificate[] xcs,
                    String string) {
            }


            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        
        X509HostnameVerifier hv = new X509HostnameVerifier(){


@Override
public boolean verify(String hostname, SSLSession session) {
// TODO Auto-generated method stub
return true;
}


@Override
public void verify(String arg0, SSLSocket arg1) throws IOException {
// TODO Auto-generated method stub

}


@Override
public void verify(String arg0, X509Certificate arg1)
throws SSLException {
// TODO Auto-generated method stub

}


@Override
public void verify(String arg0, String[] arg1, String[] arg2)
throws SSLException {
// TODO Auto-generated method stub

}
       
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx,(X509HostnameVerifier) hv);
     
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

3.需要的jar包为httpclient涉及的jar包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值