Android WebView儲存Cookie登入

來源:互聯網
上載者:User

標籤:

    因項目需要,需要在App中嵌入網頁,使用Nativie方式登入,然後將cookie儲存到WebView中,實現免登入功能。同步Cookie到WebView的方法網上有大量的參考資料,也可以參考下面的代碼:

/*** Sync Cookie*/private void syncCookie(Context context, String url){        try{            Log.d("Nat: webView.syncCookie.url", url);                       CookieSyncManager.createInstance(context);            CookieManager cookieManager = CookieManager.getInstance();            cookieManager.setAcceptCookie(true);            cookieManager.removeSessionCookie();// 移除            cookieManager.removeAllCookie();            String oldCookie = cookieManager.getCookie(url);            if(oldCookie != null){                Log.d("Nat: webView.syncCookieOutter.oldCookie", oldCookie);            }            StringBuilder sbCookie = new StringBuilder();            sbCookie.append(String.format("JSESSIONID=%s","INPUT YOUR JSESSIONID STRING"));            sbCookie.append(String.format(";domain=%s", "INPUT YOUR DOMAIN STRING"));            sbCookie.append(String.format(";path=%s","INPUT YOUR PATH STRING"));            String cookieValue = sbCookie.toString();            cookieManager.setCookie(url, cookieValue);            CookieSyncManager.getInstance().sync();             String newCookie = cookieManager.getCookie(url);            if(newCookie != null){                Log.d("Nat: webView.syncCookie.newCookie", newCookie);            }        }catch(Exception e){            Log.e("Nat: webView.syncCookie failed", e.toString());        }    }


使用上面的方法可以將Cookie同步到WebView中,這樣瀏覽網頁時即可實現免登入。

但是在實際使用過程中發現Cookie並未儲存成功,每次都會跳轉到登入頁面,糾結了很久,終於發現是在初始化WebView時漏掉了重要的東西。可以參考下面My Code設定WebView。

/*** init WebView Settings* */    private void initWebViewSettings(){//        myWebView.getSettings().setSupportZoom(true);//        myWebView.getSettings().setBuiltInZoomControls(true);//        myWebView.getSettings().setDefaultFontSize(12);//        myWebView.getSettings().setLoadWithOverviewMode(true);        // 設定可以訪問檔案        myWebView.getSettings().setAllowFileAccess(true);        //如果訪問的頁面中有Javascript,則webview必須設定支援Javascript        myWebView.getSettings().setJavaScriptEnabled(true);        myWebView.getSettings().setUserAgentString(MyApplication.getUserAgent());        myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);        myWebView.getSettings().setAllowFileAccess(true);        myWebView.getSettings().setAppCacheEnabled(true);        myWebView.getSettings().setDomStorageEnabled(true);        myWebView.getSettings().setDatabaseEnabled(true);    }


完成以上兩步操作,再次運行程式,你會發現,開啟網頁後不會再跳轉到登入頁面了。

第一使用WebView控制項,原以為很簡單,可是一不小心就掉坑裡去了,大家小心。





Android WebView儲存Cookie登入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.