Is it possible to use the cookies of a WebView in a HTTP Request? If yes, how can I do that?
Thanks
Is it possible to use the cookies of a WebView in a HTTP Request? If yes, how can I do that?
Thanks
CookieManager is what you are looking for!
CookieSyncManager.createInstance(context)
Create the manager
CookieSyncManager.getInstance().startSync()
in Activity.onResume(), and call
CookieSyncManager.getInstance().stopSync()
in Activity.onPause().
To get instant sync instead of waiting for the timer to trigger, the host can call
CookieSyncManager.getInstance().sync()
Note that even sync() happens asynchronously, so don't do it just as your activity is shutting down.
Heres how you might go about using it:
// use cookies to remember a logged in status
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);
webview.loadUrl([MY URL]);
EDIT: If you wanted to do it with a HttpClient, you would need to create an HttpContext.
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet("http://www.google.com/");
System.out.println("executing request " + httpget.getURI());
// Pass local context as a parameter
HttpResponse response = httpclient.execute(httpget, localContext);