android2.2 上的 HttpsURLConnection 奇怪行为
我正在尝试在 android 中打开 https 连接。当我在 android 3 或 1.6 中运行下面的代码时,一切都像魅力一样工作。但在 android 2.2 中有时它会给我 -1 连接响应。
安卓2.2:
连接尝试给出 -1 响应代码
睡眠 200 毫秒
连接尝试给出 -1 响应代码
睡眠 200 毫秒
连接尝试给出 http200
又一次审判
连接尝试给出 -1 响应代码
睡眠 200 毫秒
连接尝试给出 http200
我的问题是如何克服 android 2.2 中的这种改变响应。我什至不知道去哪里看。如果有人能引导我走向正确的方向,我将不胜感激。
这是代码:
HttpsURLConnection con;
URL adress = new URL("https:\\www.url.com/service.asp?someparam");
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 {
}
} };
try {
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
sc = SSLContext.getInstance("TLS");
}
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
con = (HttpsURLConnection) adress.openConnection();
((HttpsURLConnection) con).setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
Log.d("response code", "" + con.getResponseCode());
I am trying to open a https connection in android. When i run the code below in android 3 or 1.6 everything works like a charm. But in android 2.2 sometimes it gives me -1 connection response.
Android 2.2 :
connection attemp gives -1 response code
sleep 200 miliseconds
connection attemp gives -1 response code
sleep 200 miliseconds
connection attemp gives http200
Another trial
connection attemp gives -1 response code
sleep 200 miliseconds
connection attemp gives http200
My question is how can i overcome this chaning response in android 2.2. I dont even know where to look. I will appriciate if someone can guide me in the right direction.
Here is the code:
HttpsURLConnection con;
URL adress = new URL("https:\\www.url.com/service.asp?someparam");
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 {
}
} };
try {
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
sc = SSLContext.getInstance("TLS");
}
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
con = (HttpsURLConnection) adress.openConnection();
((HttpsURLConnection) con).setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
Log.d("response code", "" + con.getResponseCode());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试禁用保持活动状态,如果这没有帮助,请尝试禁用缓存:
更多信息
Try to disable keep alive and if this doesn't help, try to disable caching:
More information is in this thread.
HttpURLConnection在2.3之前有一些bug
建议2.2及更早版本使用Apache HttpClient。以及 2.3 及更高版本的 HttlURLConnection。
有关详细信息,请参阅此开发者博客文章。
HttpURLConnection has some bugs before 2.3
It is recommended to use Apache HttpClient for 2.2 and earlier. and HttlURLConnection for 2.3 and onwards.
see this Developer Blog post for more information on it.