Android 获得;“无同行证书”;在安卓4.3中
我想用证书连接到SSL认证服务器Android 获得;“无同行证书”;在安卓4.3中,android,ssl,Android,Ssl,我想用证书连接到SSL认证服务器 import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.params.BasicHttpParams; [...] SchemeRegistry registry = new SchemeRegistry(); SSLSocketFactory sslSocketFactory = n
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.params.BasicHttpParams;
[...]
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory sslSocketFactory = new SSLSocketFactory( keyStore, profile.getIdentityPassPhrase(), trustStore );
sslSocketFactory.setHostnameVerifier( new AllowAllHostnameVerifier() );
registry.register( new Scheme( "http", PlainSocketFactory.getSocketFactory(), 80) );
registry.register( new Scheme( "https", sslSocketFactory, 443 ) );
// Create a new HttpClient and Post Header
HttpParams httpParams = new BasicHttpParams();
httpParams.setBooleanParameter( "http.protocol.expect-continue", true );
HttpProtocolParams.setVersion( httpParams, HTTP_VERSION );
HttpProtocolParams.setContentCharset( httpParams, CONTENT_CHARSET );
HttpConnectionParams.setConnectionTimeout( httpParams, TIMEOUT_MILLISEC );
HttpConnectionParams.setSoTimeout( httpParams, TIMEOUT_MILLISEC );
httpClient = new DefaultHttpClient( new ThreadSafeClientConnManager( httpParams, registry ), httpParams );
在Android 4.2.2(及更低版本)中,它运行良好。但在安卓4.3上,它会抛出错误消息
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
错误在哪里?为什么它适用于API 17及更低版本,而不适用于API 18
EDIT在执行//execute//-函数时,在HttpClient类(org.apache.http.client)中抛出异常。在4.3中,这些功能发生了哪些变化,但在4.2.2中没有?Android GIT说没有什么重要的改变:我已经找到了解决问题的方法。我使用了
opensslsu客户端-connectmyu server.com:443-CApath/etc/ssl/certs/
来检查我的ssl证书。它返回了错误的证书!事实证明,我在同一ip上设置了两个不同的vhost,并使用不同的ssl证书。这需要客户端处理以下内容:
事实证明,Android似乎无法处理这一问题,至少在使用以下代码时:
String responseString = "";
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK || statusLine.getStatusCode() == 428){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
Log.e("err",e.getMessage());
} catch (IOException e) {
Log.e("err",e.getMessage());
}
return responseString;
我删除了第二个vHost,以便在每种情况下都只从服务器返回一个证书。现在,
javax.net.ssl.SSLPeerUnverifiedException:没有对等证书
异常消失。我也有同样的问题