最近做項(xiàng)目在手機(jī)端加載一個(gè) WebView 進(jìn)行網(wǎng)頁(yè)第三方登錄的時(shí)候,認(rèn)證成功后會(huì)跳轉(zhuǎn)到公司的網(wǎng)頁(yè)中,手機(jī)客戶端需要從網(wǎng)頁(yè)中拿到 Cookies 攜帶的 UID 進(jìn)行數(shù)據(jù)交互,這時(shí)我們可以在 WebViewClient 的 onPageFinished(WebView view, String url) 方法中使用 CookieManager 來(lái)獲取加載完成之后網(wǎng)頁(yè)中攜帶的 Cookie 值,代碼如下:
private class MyWebViewClient extends WebViewClient { public boolean shouldOverrideUrlLoading(WebView view, String url) { webview.loadUrl(url); return true; } public void onPageFinished(WebView view, String url) { CookieManager cookieManager = CookieManager.getInstance(); String CookieStr = cookieManager.getCookie(url); Log.e("sunzn", "Cookies = " + CookieStr); super.onPageFinished(view, url); } }