Android 2.2 上带有 Cookie 的 NullPointer 异常 - 在 2.3 及更高版本上工作正常
-这已解决,请参阅帖子底部-
我正在尝试从已建立的连接中获取 cookie。以下代码运行良好,但在 Android 2.2 及更低版本上引发 NullPointer 异常:
URL url = new URL("https://myloginform");
trustAllHosts(); //because the certificate is not singed
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(DO_NOT_VERIFY);
conn.setInstanceFollowRedirects(false);
conn.setDoOutput(true);
//Connect to login-page and send login data
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
//get cookies THIS WORKS ONLY ON ANDROID 2.3 AND ABOVE
List<String> cookies = conn.getHeaderFields().get("Set-Cookie");
conn.disconnect();
//connect to overview page
url = new URL("https://mynextpage");
trustAllHosts();
conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(DO_NOT_VERIFY);
conn.setInstanceFollowRedirects(false);
//Send cookies for identification - THIS WILL THROW A NULLPOINTER EXCEPTION
for (String cookie : cookies) {
conn.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while (!(line2.contains("</html>"))) {
line = rd.readLine();
line2 += line;
}
// wr.close();
rd.close();
有人知道为什么吗?
找到了解决方案。 Android 2.2中没有“Set-Cookie”,而是“set-cookie”
-this is solved, see bottom of post-
I am trying to get a cookie from an established connection. The following code works well, but it throws me a NullPointer Exception on Android 2.2 and lower:
URL url = new URL("https://myloginform");
trustAllHosts(); //because the certificate is not singed
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(DO_NOT_VERIFY);
conn.setInstanceFollowRedirects(false);
conn.setDoOutput(true);
//Connect to login-page and send login data
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
//get cookies THIS WORKS ONLY ON ANDROID 2.3 AND ABOVE
List<String> cookies = conn.getHeaderFields().get("Set-Cookie");
conn.disconnect();
//connect to overview page
url = new URL("https://mynextpage");
trustAllHosts();
conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(DO_NOT_VERIFY);
conn.setInstanceFollowRedirects(false);
//Send cookies for identification - THIS WILL THROW A NULLPOINTER EXCEPTION
for (String cookie : cookies) {
conn.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while (!(line2.contains("</html>"))) {
line = rd.readLine();
line2 += line;
}
// wr.close();
rd.close();
Does anyone have an idea why?
Found the solution.
There is no "Set-Cookie" in Android 2.2, it is "set-cookie"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是 Android 的事情,而是 HTTP 的事情。 Cookie 的 HTTP 标头是
Set-Cookie
。也许 Android 2.3+ 的行为很奇怪,但 HTTP 请求/响应中不应修改标头。您应该将此作为错误归档。This isn't an Android thing, it's an HTTP thing. The HTTP header for cookies is
Set-Cookie
. Perhaps Android 2.3+ is acting strangely, but there should be no modification of headers in an HTTP request/response. You should file this as a bug.