Android webview设置cookie和cookie丢失问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Te_small/article/details/83987893

Android页面嵌套了一个h5,H5页面内部有用户登陆页面,发现h5页面的登陆功能无法使用,一直登陆失败。和web那边商量一会,发现js写入的cookie丢失了。所有需要Android这边在重写写入一次。

       mWebView = view.findViewById(R.id.mall_view);
        settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setDomStorageEnabled(true);
        //不缓存
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.setWebViewClient(new MyWebViewClient());

    class MyWebViewClient extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != "") {
               //重点写入cookie
                HashMap<String, String> map = new HashMap<>();
                map.put("Referer", view.getUrl());
                view.loadUrl(url, map);
            }
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //获取登陆后的cookie,看是否写入
            CookieManager cookieManager = CookieManager.getInstance();
            String CookieStr = cookieManager.getCookie(url);
            super.onPageFinished(view, url);
        }
    }

以上就解决了登陆失败的问题!
还有就是登陆状态的同步,需要保存和设置cookie

   /**
     * 获取接口中的cookie
     * @param loginUrl
     */
    private void syncCookie(final String loginUrl) {

        new Thread(new Runnable() {


            @Override
            public void run() {
                try {
                    StringBuilder builder = new StringBuilder();
                    URL url= null;
                    byte[] data = builder.toString().getBytes("UTF-8");
                    url = new URL(loginUrl);
                    HttpURLConnection connection =
                            (HttpURLConnection) url.openConnection();

                    connection.setDoOutput(true);
                    connection.setRequestProperty("Content-Type",
                            "application/x-www-form-urlencoded");
                    connection.setRequestProperty("Content-Length",
                            Integer.toString(data.length));
                    connection.setRequestMethod("GET");
                    connection.setInstanceFollowRedirects(false);
                    OutputStream os = connection.getOutputStream();
                    os.write(data);
                    os.close();
                    int aRstCode = connection.getResponseCode();
                    if (aRstCode == HttpURLConnection.HTTP_OK) {
                        cookie = connection.getHeaderField("Set-Cookie");
                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
        //设置cookie
        if(cookie != null && cookie.length() > 0){
            android.webkit.CookieManager cookieManager =
                    android.webkit.CookieManager.getInstance();
            cookieManager.setAcceptCookie(true);
            cookieManager.setCookie(SysParam.shoppingMall, cookie);
            CookieSyncManager.getInstance().sync();
        }

猜你喜欢

转载自blog.csdn.net/Te_small/article/details/83987893
今日推荐