Cookie attachment in WebView not working

I have some problem entering my cookie on the login.php page. This is the code:

LinearLayout lLayout = (LinearLayout)findViewById(R.id.linearlayoutIdLogin);
lLayout.setVisibility(View.GONE); //make my standard layout inivisible
LinearLayout lWeb = (LinearLayout)findViewById(R.id.webviewId); 
lWeb.setVisibility(View.VISIBLE); //make my webview visible
WebView browse = (WebView)findViewById(R.id.webViewBrowse);

The correct code. I loaded my WebView, but then the page says that I need to log in.

This is an injection of cookies that does not work for me.

Cookie setcookie = cookie.get(1);
Cookie othercookie = cookie.get(0);
/* I assign my two cookies from a List<Cookie>. cookie.get() 
brings the element I need. The cookies overall contains two fields. Therefore 
the get 1 and get 0. All this works, I have tested to make a Toast to
print out the cookies */

//Here it must be anything I'm doing wrong?
CookieManager cookieManager = CookieManager.getInstance(); 
cookieManager.setAcceptCookie(true);
cookieManager.setCookie("http://www.thedomain.com", setcookie.getValue());

browse.setWebViewClient(new WebViewClient(){ });
browse.loadUrl("mypagewhenloggedin.php");

Any ideas? Stuck on this for several hours. Thanks in advance!

Edited Code: Forgot to say that I added this cookie too.

cookieManager.setCookie("http://www.thedomain.com", othercookie.getValue());

+3
source share
2 answers

getDomain() Cookie, , "http://www.thedomain.com". , Wireshark -, HTTP-, , .

+3

http://developer.android.com/reference/android/webkit/CookieSyncManager.html

CookieManager cookieManager = CookieManager.getInstance(); 

// new line
CookieSyncManager.createInstance(this);

cookieManager.setAcceptCookie(true);
cookieManager.setCookie("http://www.thedomain.com", setcookie.getValue());

// new line
CookieSyncManager.getInstance().sync();

browse.setWebViewClient(new WebViewClient(){ });
browse.loadUrl("mypagewhenloggedin.php");
+1

Source: https://habr.com/ru/post/1764924/


All Articles