ANDROID : Share session between Webview and httpclient
android, httpclient, session-cookies, webview
Solution
So , this is what I did and it worked for me -
HttpRequestBase request = new HttpGet(uri);
request.addHeader("Cookie", getCookieFromAppCookieManager(uri.toString()));
Now the implmentation for the getCookieFromAppCookieManager is as follows - The method gets the cookies for a given URL from the application CookieManager. The application CookieManager manages the cookies used by an application's WebView instances.
@param url the URL for which the cookies are requested
@return value the cookies as a string, using the format of the 'Cookie' HTTP request header
@throws MalformedURLException
public static String getCookieFromAppCookieManager(String url) throws MalformedURLException {
CookieManager cookieManager = CookieManager.getInstance();
if (cookieManager == null)
return null;
String rawCookieHeader = null;
URL parsedURL = new URL(url);
// Extract Set-Cookie header value from Android app CookieManager for this URL
rawCookieHeader = cookieManager.getCookie(parsedURL.getHost());
if (rawCookieHeader == null)
return null;
return rawCookieHeader;
}
Problem
I have actually a logged session in my WebView. But I use also httpclient to send and get data from the web. I saw on the internet that it's impossible to get the content of a WebView, so I needed to use my httpclient to get data from a webservice. The problem is that this webservice uses sessions... and my session is in my WebView, so the httpclient doesn't have it and I can't access the content of the webservice. I see many posts about this problem but I didn't understand the solution. Here is what i did on my onPageStarted : ``` CookieManager mgr = CookieManager.getInstance(); Log.i( "URL", url ); Log.i("Cookie",mgr.getCookie("mywebsite.com")); String cookie_string = mgr.getCookie("mywebsite.com"); if(cookie_string.length() > 1) { Data.instance().getPref().edit().putString("cookie",cookie_string).commit(); } ``` I saw that I have this kind of things, so I hope those include session too: (i remove the number) ``` __utma=......(number)......; __utmc=number; __utmz=number.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); wt3_eid=%number%number; wt3_sid=%number ``` Then i don't know what to do in order to set this cookie in my httpclient. I try that, with no success : ``` HttpClient client = new DefaultHttpClient(); BasicCookieStore cookieStore = new BasicCookieStore(); String login_cookie_string = Data.instance().getPref().getString("cookie", ""); String[] cookie_parts = null; if(login_cookie_string.length()> 0) { //debug_view.setText(login_cookie_string); Log.d("COOKIE", login_cookie_string); cookie_parts = login_cookie_string.split(";"); for(int t=0;t < cookie_parts.length;t++) { String[] cookieContent = cookie_parts[t].split("="); Cookie login_cookie = new BasicClientCookie(cookieContent[0],cookieContent[1]); ((BasicClientCookie) login_cookie).setDomain("mywebsite.com"); cookieStore.addCookie(login_cookie); } } ((AbstractHttpClient) client).setCookieStore(cookieStore); ```