简体   繁体   中英

How are cookies handled in loadDataWithBaseURL in WebView?

I'm loading all pages with loadDataWithBaseURL.
Step 1: Login (page 1) sets the cookie with php.
Step 2: I check for cookie, and load logged in page. (page 2)
Step 3: Now on page 2 i check for cookie again with php, and redirect to page 1 if cookie is not set or has wrong data.

It seems between step 2 & 3, cookie data is lost because i get redirected back to login..
Here is some code:

// Load login page
String urlIn = "http://myurl.com";    
String out = new Scanner(new URL(urlIn).openStream(), "UTF-8").useDelimiter("\\A").next();
webView.loadDataWithBaseURL("file:///android_asset/", out, "text/html", "UTF-8", null);


// User logged in, check for cookie and load page 2    
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
String cookie = cookieManager.getCookie("http://myurl.com");    
if(cookie != null && cookie.length() > 0){    
    String urlIn = "http://myurl.com/user/";
    String out = new Scanner(new URL(urlIn).openStream(), "UTF-8").useDelimiter("\\A").next();
    webView.loadDataWithBaseURL("file:///android_asset/", out, "text/html", "UTF-8", null);
}

I would expect loadDataWithBaseURL() to ignore cookies, simply because there is no HTTP involved. Cookies are an HTTP construct, and loadDataWithBaseURL() gets the page directly from you, not from HTTP.

Cookies might be applied for HTTP requests for resources emanating from the content that is loaded (eg, images).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM