WebView not accepting some cookies

前端 未结 1 529
时光取名叫无心
时光取名叫无心 2021-01-15 06:43

I\'m loading a website in Webview which uses some cookies to store session. I\'ve written following lines to accept cookies

CookieSyncManager.createInstance(         


        
相关标签:
1条回答
  • 2021-01-15 07:15

    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.

    0 讨论(0)
提交回复
热议问题