【发布时间】:2011-03-30 11:00:13
【问题描述】:
我这里有代码,可以让我连接到 https 服务器并传输数据。它工作得很好,但我想知道我是否以正确的方式进行操作,并且我实际上正在建立安全连接。请检查我的工作。谢谢。
public class HTTPSClient extends DefaultHttpClient
{
public HTTPSClient()
{
}
@Override
protected ClientConnectionManager createClientConnectionManager()
{
SchemeRegistry registry = new SchemeRegistry();
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
final SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
//socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", socketFactory, 80));
registry.register(new Scheme("https", socketFactory, 443));
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
return new SingleClientConnManager(params, registry);
}
}
我是这样使用这段代码的:
HttpResponse response = mConnection.httpsClient.execute(new HttpHost("www.somehostname.com", 80), new HttpGet("https://someaddress")));
然后我从那里读取响应。 再次感谢。
【问题讨论】:
-
上面的代码
socketFactory是什么意思 -
通过使用此代码我得到
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate