javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

来源:互联网 发布:高管都用什么手机 数据 编辑:程序博客网 时间:2024/05/18 18:45

今天在写代码的时候有一个接口是HTTPS的,然后用之前的代码去访问,发现报了这个错误





然后在百度上找了一下一些原因,说是SSL导致的


要可以访问有两种方法

1.忽略SSL

2.下载正式,然后用 HttpsURLConnection  去访问



这里介绍第一种方式,首先建一个类


SSLClient.java


package com.cyberway.rest.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.conn.ClientConnectionManager;import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.scheme.SchemeRegistry;import org.apache.http.conn.ssl.SSLSocketFactory;import org.apache.http.impl.client.DefaultHttpClient;/** * while true *   */public class SSLClient extends DefaultHttpClient {    public SSLClient() throws Exception{        super();        SSLContext ctx = SSLContext.getInstance("TLS");        X509TrustManager tm = new X509TrustManager() {                @Override                public void checkClientTrusted(X509Certificate[] chain,                        String authType) throws CertificateException {                }                @Override                public void checkServerTrusted(X509Certificate[] chain,                        String authType) throws CertificateException {                }                @Override                public X509Certificate[] getAcceptedIssuers() {                    return null;                }        };        ctx.init(null, new TrustManager[]{tm}, null);        SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);        ClientConnectionManager ccm = this.getConnectionManager();        SchemeRegistry sr = ccm.getSchemeRegistry();        sr.register(new Scheme("https", 443, ssf));    }}


2新增一个忽略SSL访问的post方法

public static String doPost(String url,String jsonstr,String charset){        HttpClient httpClient = null;        HttpPost httpPost = null;        String result = null;        try{            httpClient = new SSLClient();            httpPost = new HttpPost(url);            httpPost.addHeader("Content-Type", "application/json");            StringEntity se = new StringEntity(jsonstr);            se.setContentType("application/json");            //这一行代码好像会把服务器报错的消息返回,我不需要            //se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));            httpPost.setEntity(se);            HttpResponse response = httpClient.execute(httpPost);            if(response != null){                HttpEntity resEntity = response.getEntity();                if(resEntity != null){                    result = EntityUtils.toString(resEntity,charset);                }            }        }catch(Exception ex){            ex.printStackTrace();        }        return result;    }


注意,由于我们项目的 httpclient 比较久,所以没有可关闭的那个方法,如果你的是比较新版本的 httpclient,记得最好关闭一下资源


改完以后再去测试就可以了





这种是忽略SSL进行访问的,不知道有没有什么问题出现,,,,现在另一种方式我还没研究,到时有研究再更新吧...


阅读全文
0 0
原创粉丝点击