关于此问题有数十篇文章(javax.net.ssl.SSLPeerUnverifiedException:无对等证书),但我还没有找到适合我的方法。

许多帖子(例如thisthis)通过接受所有证书来“解决”此问题,但是,除了测试以外,这当然不是一个很好的解决方案。

其他人似乎相当本地化,对我不起作用。我真的希望有人能提供一些我缺乏的见识。

所以,我的问题是:我正在通过HTTPS连接的只能通过本地网络访问的服务器上进行测试。通过浏览器拨打我需要的电话,效果很好。不用提示证书,如果您检查证书,一切看起来都不错。

当我在Android设备上尝试时,我得到javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
这是调用它的代码:

StringBuilder builder = new StringBuilder();
builder.append( /* stuff goes here*/ );

httpGet.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();

// Execute HTTP Post Request. Response body returned as a string
HttpClient httpClient = MyActivity.getHttpClient();
HttpGet httpGet = new HttpGet(builder.toString());

String jsonResponse = httpClient.execute(httpGet, responseHandler); //Line causing the Exception

我的MyActivity.getHttpClient()代码:
protected synchronized static HttpClient getHttpClient(){
    if (httpClient != null)
        return httpClient;

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION);
    HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOCKET);
    HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);

    //Thread safe in case various AsyncTasks try to access it concurrently
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(httpParameters, schemeRegistry);

    httpClient = new DefaultHttpClient(cm, httpParameters);

    CookieStore cookieStore = httpClient.getCookieStore();
    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    return httpClient;
}

任何帮助将非常感激。

编辑

还要提一下,我在另一个应用程序中遇到了其他SSL问题,但之前添加了SchemeRegistry部分为我解决了该问题。

编辑2

到目前为止,我只在Android 3.1上进行了测试,但是无论如何我都需要在Android 2.2+上进行测试。我刚刚在Android选项卡(Android 3.1)的浏览器上进行了测试,它提示证书。在我的PC浏览器上很好,但在Android浏览器或我的应用程序上没问题。

编辑3

事实证明,iOS浏览器也对此有所提示。我开始认为这是此处描述的证书链问题(SSL certificate is not trusted - on mobile only)

最佳答案

事实证明,我的代码很好,问题在于服务器未返回完整的证书链。有关更多信息,请参见此SO帖子和此 super 用户帖子:

SSL certificate is not trusted - on mobile only

https://superuser.com/questions/347588/how-do-ssl-chains-work

关于android - 安全修复: javax. net.ssl.SSLPeerUnverifiedException:没有对等证书,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18126372/

10-11 22:38
查看更多