java http ssl_java中的httpclient的SSL出错

referenced this link

http://theskeleton.wordpress.com/2010/07/24/avoiding-the-javax-net-ssl-sslpeerunverifiedexception-peer-not-authenticated-with-httpclient/

The following class takes a HttpClient and returns a new HttpClient that accepts any SSL certificate:

01public class WebClientDevWrapper {

02

03    public static HttpClient wrapClient(HttpClient base) {

04        try {

05            SSLContext ctx = SSLContext.getInstance("TLS");

06            X509TrustManager tm = new X509TrustManager() {

07

08                public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {

09                }

10

11                public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {

12                }

13

14                public X509Certificate[] getAcceptedIssuers() {

15                    return null;

16                }

17            };

18            ctx.init(null, new TrustManager[]{tm}, null);

19            SSLSocketFactory ssf = new SSLSocketFactory(ctx);

20            ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

21            ClientConnectionManager ccm = base.getConnectionManager();

22            SchemeRegistry sr = ccm.getSchemeRegistry();

23            sr.register(new Scheme("https", ssf, 443));

24            return new DefaultHttpClient(ccm, base.getParams());

25        } catch (Exception ex) {

26            ex.printStackTrace();

27            return null;

28        }

29    }

30}

You can then do something like this in the code that creates the HttpClient:

1this.client = new DefaultHttpClient();

2if(dev) {

3    this.client = WebClientDevWrapper.wrapClient(client);

4}

referenced this link

http://theskeleton.wordpress.com/2010/07/24/avoiding-the-javax-net-ssl-sslpeerunverifiedexception-peer-not-authenticated-with-httpclient/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值