Why Does Android Webview Sporadically Not Sending My Session Cookie

Cookie doesn't work properly in webview in android

I had similar issue and I added the below code and worked.

String myURL = "https://www.yourWebPage.com";

android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();

cookieManager.setAcceptCookie(true);
cookieManager.acceptCookie();
cookieManager.setAcceptFileSchemeCookies(true);
cookieManager.getInstance().setAcceptCookie(true);
cookieManager.getCookie(myURL);

Hope it helps.

WebView not accepting some cookies

If you want to pass your login users data to a cookie in Webview the do it like this
The other day I had to pass my login object to particular URL
as follow.

            WebSettings settings = webViewRoi.getSettings();
settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
settings.setAppCacheEnabled(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setAllowFileAccessFromFileURLs(true);

Gson gson = new GsonBuilder().disableHtmlEscaping().create();


FormsDTO formsDTO = new FormsDTO();
FormsDTOProfile dtoProfile = new FormsDTOProfile(LoginActivity.loginInfoDTO.getProfile());
formsDTO.setProfile(dtoProfile);
formsDTO.setAuthorized(true);
formsDTO.setToken(LoginActivity.loginInfoDTO.getToken());

String out123 = gson.toJson(formsDTO);

String auth2 = URLEncoder.encode(out123, "UTF-8");

String z = "userInfo=" + auth2; // here userinfo is the key of cookie
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);


cookieManager.setCookie(url, z);
webViewRoi.setWebChromeClient(new WebChromeClient());
webViewRoi.loadUrl(url);

So if you know the key name of cookie then you can pass your login object through cookie. hope it helps you.

Android WebView doesn't store the cookies

Seems that there is a delay indeed with the cookies so I have to use this code:

        webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
Toast.makeText(getApplicationContext(), "Page loading complete", Toast.LENGTH_LONG).show();
}
});

Now it works fine.



Related Topics



Leave a reply



Submit