tags:

views:

76

answers:

2

Hello! 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 then 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());

A: 

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");
Aaron Saunders
Will check this directly!
Julian Assange
Hm, cannot get this to work. Added one line in my question since I forgot to post it, if it makes any difference.
Julian Assange
+1  A: 

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.

CommonsWare
That was a good idea, will test now!
Julian Assange
Well, played around a bit, and I cannot really make this work. Any ideas?
Julian Assange
I have also tested that the input doesn't get malformed in a simple guest book... What bothers my is that I can't maybe reuse my cookie from my Request in my webview, because the cookie has expired?
Julian Assange
@Julian Assange: "I have also tested that the input doesn't get malformed in a simple guest book" -- I'm not sure what the "input" is here, but you need to make sure that the Cookie header looks the same, both when you do the request from a regular browser and when you do it through your injected cookie in the `WebView`.
CommonsWare
Well, now I have grab it. The first cookie value in my browser is: _"Cookie: SQMSESSID=0b1a366aee51ae7d36d6f174ee53a5f6; key=HkVnPXyG; OAS_SC1=1284571398389; OAX=WuWlkEyPnFwAACtr\r\n"_. The problem is maybe how I set it or how can I continue debugging this?
Julian Assange
@Julian Assange: Well, the question is -- are you getting an equivalent `Cookie` header via your injection process? If not, you need to determine where that is breaking down (e.g., not finding the cookie in the `HttpClient` response, not putting it in properly in the `WebView` `CookieStore`).
CommonsWare
Hm, okay. Then I probably need to use this (see ->) instead of getting the HttpEntity entity.getEntity() -> HttpEntity entity = response.getHeaders(?); For now I just check for the entity and the cookie is in Wireshark getting some kind of random hash. Sorry for bring such much noise, but I'm not sure how this should be implemented correctly, anyway, thanks for your help.
Julian Assange
@Julian Assange: You might consider using `getCookieStore()` from `HttpClient`. Otherwise, `getHeaders("Set-cookie")` may work.
CommonsWare
Thank you for the answers CommonsWare. Currently I'm using: "List<Cookie> cookie = httpClient.getCookieStore().getCookies();". The question is how to compare it correctly, however, I will check it out further tomorrow and really try this out. I do really appreciate your support.
Julian Assange
Accept this since the inject thing itself are working, it's now a cookie header thing.
Julian Assange