由于“不受信任的服务器证书”,httpget 无法工作但它适用于 httppost
我有一个代码需要访问下面的网站并提取文本。在项目的另一部分中,我使用 httpost 方法,它根据我发送的内容给我一个响应。然而,对于 httpget 函数,它突然给了我一个异常,说 javax.net.ssl.SSLEXCEPTION:Not trusted servercertificate。
有办法解决这个问题吗?我不明白为什么一种方法有效而另一种方法无效。谢谢
//some code
getText("https://iphone-radar.com/accounts/4e3f2c6659f25a0f8400000b");
}
public static void getText(String uri) {
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(uri);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
// Get hold of the response entity
HttpEntity entity = response.getEntity();
} catch (Exception e) {
// TODO Auto-generated catch block
android.util.Log.v("EXCEPTION","["+e.getMessage()+"]", e);
}
}
I have a code that needs to go to the website below and extract the text. In another part of the project I use the httpost method and it gives me a response based on what I've sent. However for the httpget function suddenly it gives me an exception saying javax.net.ssl.SSLEXCEPTION:Not trusted server certificate.
Is there a way to fix this? I don't understand why one method works while the other doesn't. Thanks
//some code
getText("https://iphone-radar.com/accounts/4e3f2c6659f25a0f8400000b");
}
public static void getText(String uri) {
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(uri);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
// Get hold of the response entity
HttpEntity entity = response.getEntity();
} catch (Exception e) {
// TODO Auto-generated catch block
android.util.Log.v("EXCEPTION","["+e.getMessage()+"]", e);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用如下所示的内容来覆盖验证程序。使用 Android 2.2 测试的代码
Use something like below to override the verifier. Code tested using Android 2.2
如果您想简单地忽略所有 SSL 证书检查。以下链接的评论对我有帮助!
http://code.google.com/p/android/issues /detail?id=1946#c10
If you want to simply ignore all the SSL cert check. The comment on the following link helped me!
http://code.google.com/p/android/issues/detail?id=1946#c10
我在为学校开发应用程序时遇到了类似的问题。诀窍是创建两个覆盖证书验证的类。
一种扩展 HostnameVerifier 并在每次调用 verify() 时返回 true,例如 这个。
另一个类扩展 X509TrustManager 并重写 getAcceptedIssuers(),如 这个.
然后,您可以使用以下代码将 HttpsURLConnection 设置为接受所有证书:
这应该可以解决问题。您可以看到我如何使用此代码 此处,在 run() 方法中。
I had a similar problem while developing an app for my school. The trick is to create two classes that override certificate verification.
One extends HostnameVerifier and returns true every time verify() is called, like this.
The other class extends X509TrustManager and overrides getAcceptedIssuers() like this.
Then you can set the HttpsURLConnection to accept all certificates using this code:
That should do the trick. You can see how I used this code here, in the run() method.