存储会话Cookie-Android Studio

时间:2020-02-25 10:01:04

标签: java android session-cookies

我正在尝试制作一个可以请求网页并存储其会话Cookie的Android应用。 我没有找到太多有关CookieManager的文档,也不知道java.net.CookieManager是否比android.webkit.CookieManager好,但是我尝试使用最后一个脚本。有没有办法将CookieManager链接到我的连接,以便它存储会话的所有cookie? 这是我的代码(它返回空cookie):

            CookieManager cookieManager = CookieManager.getInstance();
            URL url = new URL("blahblahblah");
            URLConnection urlConn = url.openConnection();
            HttpsURLConnection httpsConn = (HttpsURLConnection) urlConn;
            httpsConn.setAllowUserInteraction(false);
            httpsConn.setInstanceFollowRedirects(true);
            httpsConn.setRequestMethod("GET");
            httpsConn.connect();
            int resCode = httpsConn.getResponseCode();
            if (resCode != 200){
                return "ERROR: " + resCode + " returned";
            }
            return "Site is up. Cookie: " + cookieManager.getCookie("blahblahblah");

0 个答案:

没有答案