开发者

Inject cookie into WebView doesn't work

I have some problem with the injecting of my cookie into 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 code above is correct. I got my WebView to load but the开发者_JAVA技巧n the page says I need to login.

This is the cookie injection that doesn't 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? Have been stuck on this for a several hours. Thanks in advance!

Edited code: Forgot to say I added this cookie too.

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


Use getDomain() on the Cookie, rather than assuming the domain is "http://www.thedomain.com". Also, consider using Wireshark or something to examine the HTTP request to see what may be malformed compared to a working browser request.


try this change

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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜