android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
/** remove all cookies **/
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieManager.removeExpiredCookie();
/** Yes, we accept cookies **/
cookieManager.setAcceptCookie(true);
StringBuilder sb = null;
for (HttpCookie cookie: cookies) {
sb = new StringBuilder();
if (cookie.getDomain() == null) {
continue;
}
sb.append(cookie.getName() + "=" + cookie.getValue());
sb.append("; domain=" + cookie.getDomain());
cookieManager.setCookie(cookie.getDomain(), sb.toString());
}
cookieSyncManager.getInstance().sync();
2013/03/08
Sync cookie 到 webview
Webview 可以用來呈現網頁畫面, 其中最簡單的使用方式就是 loadUrl 這個 function.
一般網站可以不考慮到 cookie, 但如果牽扯到需要登入時, cookie 就很重要了.
如果單純使用 webview, 那 cookie 就不是問題,
但如果想要用 HttpURLConnection 這類別, 並且透過此類別做動作,
那麼要如何把所得到的 cookies 分享給 webview 使用 ?
以下就是 sync cookie 的方式.
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言