Android 2.2 上带有 Cookie 的 NullPointer 异常 - 在 2.3 及更高版本上工作正常

发布于 2025-01-02 20:58:47 字数 1800 浏览 0 评论 0原文

-这已解决,请参阅帖子底部-

我正在尝试从已建立的连接中获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2025-01-09 20:58:47

这不是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文