HttpResponse 不适用于不受信任的证书
我正在尝试从链接 https://....com:8455 获取内容,其中“服务器的证书不受信任”。内容是:“测试”。
但我在 HttpResponse response = httpClient.execute(httpPost); 之后收到 NullPointerException。 我正在使用下一个代码:
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, 8455));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
final String url = "https://....com:8455";
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse response = httpClient.execute(httpPost);
Log.v("data: ", HttpHelper.requestStr(response));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
有人可以帮助我吗?
I am trying get content from the link https://....com:8455 where "Server's certificate is not trusted". The content is: "test".
But I get NullPointerException after HttpResponse response = httpClient.execute(httpPost);.
I am using the next code:
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, 8455));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
final String url = "https://....com:8455";
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse response = httpClient.execute(httpPost);
Log.v("data: ", HttpHelper.requestStr(response));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Can anybody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NullPointerException 来自以下事实:execute() 返回 null。也就是说,没有任何反应。
至于造成这种情况的原因,请搜索SO,有很多关于连接到自签名证书或根本不检查CA的问题得到解答。
The NullPointerException comes from the fact, that execute() returned null. That means, that there was no response.
As to the causes of this, please search SO, there are PLENTY of questions answered about connecting to a self-signed certificate, or not checking to a CA at all.