公司项目要求将客户端的Cookie传递到WebView中,代码如下,关键是对每个Cookie都需要设置url键。
1 public void synCookies(String url, Context mContext) { 2 3 URL u = null; 4 try { 5 u = new URL(url); 6 } catch (MalformedURLException e) { 7 e.printStackTrace(); 8 } 9 10 CookieSyncManager.createInstance(mContext); 11 CookieManager cookieManager = CookieManager.getInstance(); 12 cookieManager.removeAllCookie(); 13 if (PlayerUtil.getCookie() != null && !"".equals(PlayerUtil.getCookie())) { 14 String[] cookies = PlayerUtil.getCookie().split(";"); 15 for (int i = 0; i < cookies.length; i++) { 16 cookieManager.setCookie(url, cookies[i]); 17 } 18 cookieManager.setCookie("domin", "v.youku.com"); 19 cookieManager.setCookie("path", u.getPath()); 20 21 CookieSyncManager.getInstance().sync(); 22 } 23 }
客户端传递Cookie到WebView中
时间: 2024-10-09 20:23:00