Set a cookie to a webView in Android
Solution 1
It's quite simple really.
String cookieString = "cookie_name=cookie_value; path=/";
CookieManager.getInstance().setCookie(baseUrl, cookieString);
where cookieString
is formatted the same as a more traditional Set-Cookie
HTTP header, and baseUrl
is the site the cookie should belong to.
Solution 2
Couple of comments which I found out from my experience and gave me headaches:
-
http
andhttps
urls are different. Setting a cookie forhttp://www.example.com
is different than setting a cookie forhttps://www.example.com
- A slash in the end of the url can also make a difference. In my case
https://www.example.com/
works buthttps://www.example.com
does not work. -
CookieManager.getInstance().setCookie
is performing an asynchronous operation. So, if you load a url right away after you set it, it is not guaranteed that the cookies will have already been written. To prevent unexpected and unstable behaviours, use the CookieManager#setCookie(String url, String value, ValueCallback callback) (link) and start loading the url after the callback will be called.
I hope my two cents save some time from some people so you won't have to face the same problems like I did.
Solution 3
You may want to take a look of how I am setting the a webview cookie at :
Android WebView Cookie Problem
Where in my answer you could see how I'm handling this like:
val cookieManager = CookieManager.getInstance()
cookieManager.acceptCookie()
cookieManager.setCookie(domain,"$cookieKey=$cookieValue")
cookieManager.setAcceptThirdPartyCookies(view.webViewTest,true)
Solution 4
You should use it with some loop if you have few items with cookies (like in my case):
val cookies = "key1=someValue1;key2=someValue2;key3=someValue3"
val cookiesList = cookies.split(";")
cookiesList.forEach { item ->
CookieManager.getInstance().setCookie("http://someHost.com", item)
}
You can't use it like:
CookieManager.getInstance().setCookie("http://someHost.com", "key1=someValue1;key2=someValue2;key3=someValue3")
Joakim Engstrom
Gadget geek, Android and iOS developer, mobile enthusiast and an unhealthy love for film!
Updated on July 16, 2020Comments
-
Joakim Engstrom almost 4 years
I'm getting a
HttpResponse
from a server when checking if a username or password is correct. When I load theurl
in awebview
I want thewebView
to have the cookie (the answer I get withpostData()
stored in thewebView
. I want the webView to pickup the cookie and load the url with that cookie stored in the webview.I'm getting the response through.
public HttpResponse postData() { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("https://example.com/login.aspx"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("txtUsername", "user")); nameValuePairs.add(new BasicNameValuePair("txtPassword", "123")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); String responseAsText = EntityUtils.toString(response.getEntity()); Log.v(TAG , "Response from req: " + responseAsText); return responseAsText; } catch (ClientProtocolException e) { } catch (IOException e) { } return null; }
And I loadUrl with:
webView.loadUrl("http://a_page.com/getpage.aspx?p=home");
I guess I'm not really managing a cookie and I have no idea how to do so. Any suggestions or solutions?