javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

出现此情况基本原因就是SSL证书的问题,有的时候访问成功有的时候访问失败

失败的代码:

/**
     * post请求
     *
     * @param url
     * @param json
     * @return
     */
    public static JSONObject doPost(String url, String json) {

     DefaultHttpClient client =  new DefaultHttpClient();
 
        HttpPost post = new HttpPost(url);
        JSONObject response = new JSONObject();

        try {
            StringEntity s = new StringEntity(json);
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = res.getEntity();
                String result = EntityUtils.toString(res.getEntity());// 返回json格式:
                //判断返回值是否是json格式,如果不是,进行处理
                if (!StringDeal.isStringJson(result)) {
                    response.put("error", result);
                    return response;
                }
                response = JSONObject.fromObject(result);
            }
        } catch (Exception e) {
            throw new ServiceException(e.toString());
        }
        return response;
    }

修复后的代码:

/**
     * post请求
     *
     * @param url
     * @param json
     * @return
     */
    public static JSONObject doPost(String url, String json) {
//      可能会出现 javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
// DefaultHttpClient client =  new DefaultHttpClient();
        org.apache.http.client.HttpClient client = wrapClient(url);

        HttpPost post = new HttpPost(url);
        JSONObject response = new JSONObject();

        try {
            StringEntity s = new StringEntity(json);
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = res.getEntity();
                String result = EntityUtils.toString(res.getEntity());// 返回json格式:
                //判断返回值是否是json格式,如果不是,进行处理
                if (!StringDeal.isStringJson(result)) {
                    response.put("error", result);
                    return response;
                }
                response = JSONObject.fromObject(result);
            }
        } catch (Exception e) {
            throw new ServiceException(e.toString());
        }
        return response;
    }


    private static org.apache.http.client.HttpClient wrapClient(String host) {
        org.apache.http.client.HttpClient httpClient = new DefaultHttpClient();
        if (host.startsWith("https://")) {
            sslClient(httpClient);
        }

        return httpClient;
    }

    private static void sslClient(org.apache.http.client.HttpClient httpClient) {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] xcs, String str) {

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

                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
            ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ClientConnectionManager ccm = httpClient.getConnectionManager();
            SchemeRegistry registry = ccm.getSchemeRegistry();
            registry.register(new Scheme("https", 443, ssf));
        } catch (KeyManagementException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException(ex);
        }
    }

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值