android - SSL 证书 : "No peer certificate" 问题

标签 android ssl apache-commons-httpclient

我正在尝试通过 Last.fm's API 进行身份验证.

在 Android 4.3 上,它可以正常工作

HttpPost post = new HttpPost("https://ws.audioscrobbler.com/2.0/");
post.setEntity(new UrlEncodedFormEntity(params));

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);

但是在 2.3.3 上我得到了

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

然后我尝试了给出的解决方案 here :

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

DefaultHttpClient client = new DefaultHttpClient();

SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

// Set verifier     
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);        

HttpPost post = new HttpPost("https://ws.audioscrobbler.com/2.0/");
post.setEntity(new UrlEncodedFormEntity(params));

HttpResponse response = httpClient.execute(post);

但我仍然得到同样的错误。

然后我试了that :

HttpParams httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParams, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(httpParams, true);

SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(httpParams, schReg);

DefaultHttpClient httpClient = new DefaultHttpClient(conMgr, httpParams);

HttpPost post = new HttpPost("https://ws.audioscrobbler.com/2.0/");
post.setEntity(new UrlEncodedFormEntity(params));

HttpResponse response = httpClient.execute(post);

又失败了。

有人可以帮忙吗?

最佳答案

从服务器返回证书的方式似乎存在一些问题,或者可能是 android 系统 keystore 没有相关的根证书来验证和完成握手。

查看 certificate chain information对于问题中提到的网站,在我看来,链条没有正确排序。

你可以试试答案here

关于android - SSL 证书 : "No peer certificate" 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18626682/

相关文章:

java - 如何在 Java 中为 HttpClient 请求设置代理主机

java - Apache Httpclient 在目的地确定之前发布数据

android - Android应用程序的自动化测试工具

java - Android java 保存颜色

java - 通过 SSL 的 SOAP 网络服务连接

java - 通过系统属性设置 HttpClient 的套接字超时

android - 从后台截图并在保存前处理

android - 如何使用来自用户名的提要获取 youtube 视频?

ssl - 如何识别我的 SSL 公钥证书?

java - 信任 Java Playframework 2.2 中的所有 SSL 证书