I have a web view of login and httpclient, which should confirm that the user is logged in. The problem is that webview and httpclient use different cookies, so httpclient cannot get webview cookies.
I read a lot of questions and tutorials, but nothing worked. Some of the things I read:
I read several other guides on Android Development and other sites, but nothing worked.
another post: https://stackoverflow.com/questions/28052461/syncing-webview-with-httpclient
The problem is that cookies will not sync.
what i tried:
WebView webview;
webview = (WebView) rootView.findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
}
});
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://www.klh-dev.com/lehava/lehava/system/mlogin.php");
and a few more:
public String IsLoggedIn() {
new Thread(new Runnable() {
@Override
public void run() {
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
response_str=client.execute(get,responseHandler);
System.out.println(response_str);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Cookie sessionInfo;
List<Cookie> cookies = client.getCookieStore().getCookies();
if (! cookies.isEmpty()){
CookieSyncManager.createInstance(getApplicationContext());
CookieManager cookieManager = CookieManager.getInstance();
for(Cookie cookie : cookies){
sessionInfo = cookie;
String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
cookieManager.setCookie(URLn, cookieString);
CookieSyncManager.getInstance().sync();
}
}
}
}).start();
return response_str;
}
* httpget return 1 or 0
I want to take cookies from webview and use them in my httpclient request
EDIT (darpan answer added):
public static BasicCookieStore getCookieStore(String cookies, String domain) {
String[] cookieValues = cookies.split(";");
BasicCookieStore cs = new BasicCookieStore();
BasicClientCookie cookie;
for (int i = 0; i < cookieValues.length; i++) {
String[] split = cookieValues[i].split("=");
if (split.length == 2)
cookie = new BasicClientCookie(split[0], split[1]);
else
cookie = new BasicClientCookie(split[0], null);
cookie.setDomain(domain);
cs.addCookie(cookie);
}
return cs;
}
public String IsLoggedIn() {
new Thread(new Runnable() {
@Override
public void run() {
String cookies = CookieManager.getInstance().getCookie("http://klh-dev.com/lehava/lehava/system/isloggedin.php");
BasicCookieStore lCS = getCookieStore(cookies, "klh-dev.com");
HttpContext localContext = new BasicHttpContext();
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCookieStore(lCS);
localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);
HttpGet get=new HttpGet("http://klh-dev.com/lehava/lehava/system/isloggedin.php");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
result=httpclient.execute(get,localContext);
response_str = EntityUtils.toString(result.getEntity());
System.out.println(response_str);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
return response_str;
}
}
EDIT2: Finally works !! This is my code:
public static String IsLoggedIn() {
new Thread(new Runnable() {
@Override
public void run() {
String cookies = CookieManager.getInstance().getCookie(getUrl);
BasicCookieStore lCS = getCookieStore(cookies, "klh-dev.com");
HttpContext localContext = new BasicHttpContext();
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCookieStore(lCS);
localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);
HttpGet get = new HttpGet("http://klh-dev.com/lehava/lehava/system/isloggedin.php");
try {
result=httpclient.execute(get,localContext);
response_str = EntityUtils.toString(result.getEntity());
System.out.println(response_str);
((MainActivity) getContext).UpdateMenu();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
return response_str;
}
and about the variable getUrl. I needed to set a global variable as follows:
private static String getUrl;
public String getUrl() {
return getUrl;
}
On each snippet I had to add onPageFinished: getUrl = view.getUrl();
Thank.