Extracting cookies from A WebView, Android
August 31st, 2010I was recently writing an application for Android which required pulling out authentication information from WebView. Basically, a user would enter his login credentials (into facebook/twitter ) in WebView and once the user is logged, the cookies need to be stored somewhere such that a HTTP Client could use them.
Cookies used by a WebView can be accessed/modified by using the CookieManager. CookieManager once instantiated can be used pretty much anywhere but its useful to snatch cookies when pages are finished/started loading. So, I wrote a WebViewClient which uses onPageStarted() and onPageFinished() to execute the cookie extracting procedure. In this example the cookies are saved as SharedPreferences strings, but once you have the cookie in hand ( a string
) you can do whatever you want to do with it.
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
Log.i( "PageStarted", url );
String success_url = "http://www.xxxxx.com/login_success";
String failure_url = "http://www.xxxxx.com/login_failure";
int fail = url.compareTo(failure_url);
int success = url.compareTo(success_url);
if(fail == 0)
{
super.onPageStarted(view, url, favicon);
}
if(success == 0)
{
CookieManager mgr = CookieManager.getInstance();
Log.i( "URL", url );
Log.i("Cookie",mgr.getCookie("xxxx.com")+"test");
String cookie_string = mgr.getCookie("xxxx.com");
if(cookie_string.length() > 1)
{
settings_editor.putBoolean("got_session_cookie",true);
settings_editor.putString("cookie",cookie_string);
settings_editor.commit();
}
super.onPageStarted(view, url, favicon);
}
if((fail != 0)&&(success != 0))
{
super.onPageStarted(view, url, favicon);
}
}
Now, inserting cookies into an HTTP Client.
DefaultHttpClient httpclient = new DefaultHttpClient();
BasicCookieStore cookieStore = new BasicCookieStore();
String login_cookie_string = settings.getString("cookie", "");
String[] cookie_parts = null;
if(login_cookie_string.length()> 0)
{
//debug_view.setText(login_cookie_string);
cookie_parts = login_cookie_string.split("=");
if(cookie_parts.length == 2)
{
for(int t=0;t cookie_parts.length;t++)
{
//debug_view.append("part "+cookie_parts[t]);
}
Cookie login_cookie = new BasicClientCookie(cookie_parts[0],cookie_parts[1]);
((BasicClientCookie) login_cookie).setDomain("pragungoyal.com");
cookieStore.addCookie(login_cookie);
}
else
{
//debug_view.setText(" couldnt split cookies ");
}
}
else
{
//debug_view.setText(" no cookie ");
}
httpclient.setCookieStore(cookieStore);
HttpGet request = new HttpGet("http://www.xxxx.com/");
int status = 0;
boolean got_url = false;
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
try
{
HttpResponse http_response = httpclient.execute(request);
if (status != HttpStatus.SC_OK)
{
http_response.getEntity().writeTo(ostream);
debug_view.setText(ostream.toString());
mainjson = new JSONObject(ostream.toString());
URLDecoder decode_url = new URLDecoder();
login_url = URLDecoder.decode(mainjson.getString("login_url"));
got_url = true;
}
else
{
InputStream content = http_response.getEntity().getContent();
}
}
catch (ClientProtocolException e)
{
debug_view.setText(e.getMessage());
}
catch (IOException e)
{
debug_view.setText(e.getMessage());
}
catch (JSONException e)
{
// TODO Auto-generated catch block e.printStackTrace();
debug_view.setText(e.getMessage());
}
Here’s how I do it. Hope It helps.