像许多在这里,我试图与具有自签名证书站点进行通信。 有很多很多的类和代码片段展示了如何做到这一点,如Android和自签名服务器证书的HTTPS GET(SSL) ,但我找不到任何说明如何实际使用的类。 我已经尝试了很多,我敢肯定,我只是失去了一些东西简单。
具体而言,我试图使用由“莫斯”提供的课程; 答案得票最多。 我还总是得到“不信任的证书”的消息。 另外一人在该主题还要求实施的提示,但一直没有得到答复。 谢谢你的帮助。
如果我能这个问题,追加到该话题,我会觉得很开心,但我想新手和我一样只能张贴新的问题(虽然我一直这么一个风扇一年多了)。
我们使用了类似SocketFactory
和X509TrustManager
在我们的一些项目的实施。 为了将这些类绑定到你的实现,你基本上应该做的是勾起来的HttpClient
(假设这是你使用的是什么),用于客户端-服务器通信。
通常,我们必须创建一个方法HttpClient
与工厂提及和的TrustManager。 它看起来有点像这一点,是松散的基础在内部注释显示的链接。
protected HttpClient constructClient() {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
// use the debug proxy to view internet traffic on the host computer
if (ABCApplication.isDebuggingProxy()) params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(ABCConstants.DEBUG_PROXY_HOST, ABCConstants.DEBUG_PROXY_PORT, "http"));
// ignore ssl certification (due to signed authority not appearing on android list of permitted authorities)
// see: http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", new PlainSocketFactory(), 80));
registry.register(new Scheme("https", new FakeSocketFactory(), 443));
ClientConnectionManager cm = new SingleClientConnManager(params, registry);
return new DefaultHttpClient(cm, params);
}
希望与您的实现有所帮助。
我们也可以使用
HttpURLConnection http = null;
URL url;
try {
url = new URL("https:your domian");
if (url.getProtocol().toLowerCase().equals("https")) {
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
http = https;
System.out.println("TEST:::"+convertStreamToString(http.getInputStream()));
} else {
http = (HttpURLConnection) url.openConnection();
System.out.println("TEST:::"+convertStreamToString(http.getInputStream()));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*******
* Trust every server - don't check for any certificate
*/
private static void trustAllHosts() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};