最近项目中用到WebView访问新浪支付页面,有个要求是必须是登录状态,否则会报Token过期,然后我简单的将我从cookie中取得的ticket,即一串数字可以代表用户登录的唯一标识作为参数拼接到url路径上,竟然成功了,但是,当在新浪页面进入第二级界面的时候,还会报Token过期,这才意识到,还是新浪网页没有获取到cookie。 可是ios也是用的webview访问的新浪支付,并且没有设置任何属性,新浪就可以获取到cookie。百度了一番,当然如何获取cookie,自己百度查查吧,我这是已经获取到cookie之后的操作。注意的是要在WebView.loadUrl()之前调用syncCookie方法: public void syncCookie(Context context, String url) { try { CookieSyncManager.createInstance(context); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); cookieManager.removeSessionCookie();// 移除 cookieManager.removeAllCookie(); //以下是我们在请求中获取到的cookie相关信息,然后存到本地,你需要做的就是 将自己的cookie 中的信息分别取出来 如:// cookie0: SFFGDFEID=0F7AE910509938FE024D6F94CB882DCA// cookie1: Domain=.dotyre.cn// cookie3: Expires=Sat, 12-Aug-2017 05:41:47 GMT// cookie4: Path=/ String cookie = SharedPreferencesUtils.getString(context, "cookie0"); String domain = SharedPreferencesUtils.getString(context, "cookie1"); String path = SharedPreferencesUtils.getString(context, "cookie4"); //然后分别拼到StringBuilder 上,然后通过cookieManager.setCookie(url, cookieValue);最后再进行同步,Cook//ieSyncManager.getInstance().sync(); StringBuilder sbCookie = new StringBuilder(); sbCookie.append(cookie); // sbCookie.append(String.format(";domain=%s", domain)); // sbCookie.append(String.format(";path=%s", path)); sbCookie.append(";"+domain); sbCookie.append(";"+path); String cookieValue = sbCookie.toString(); Log.e("==cookieValue==", cookieValue); cookieManager.setCookie(url, cookieValue); CookieSyncManager.getInstance().sync(); } catch (Exception e) { Log.e("==异常==", e.toString()); } } 另外提供一个工具类: package com.hibo.bm.utils; import android.content.Context; import android.os.Build; import android.util.Log; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; import android.webkit.WebView; import com.hibo.bm.url.AppUrl; import org.xutils.http.cookie.DbCookieStore; import java.net.HttpCookie; import java.util.List; /** * Created by 李志强 on 2016/8/12. */ public class CookieHelper { /** * clear Cookie * * @param context */ public static void clearCookie(Context context) { CookieSyncManager.createInstance(context); CookieSyncManager.getInstance().startSync(); CookieManager.getInstance().removeSessionCookie(); } /** * Sync Cookie */ public static void syncCookie(Context context, String url) { try { CookieSyncManager.createInstance(context); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); cookieManager.removeSessionCookie();// 移除 cookieManager.removeAllCookie();// String cookie = SPUtil.getParam(context, "cookie", "");// String domain = SPUtil.getParam(context, "domain", "");// String path = SPUtil.getParam(context, "path", ""); String cookie = SharedPreferencesUtils.getString(context, "cookie0");//从cookie里取出的值就是ticket String domain = SharedPreferencesUtils.getString(context, "cookie1");//domain String path = SharedPreferencesUtils.getString(context, "cookie4");//path StringBuilder sbCookie = new StringBuilder(); sbCookie.append(cookie); sbCookie.append(";"+domain); sbCookie.append(";"+path); String cookieValue = sbCookie.toString(); Log.e("==cookieValue==", cookieValue); cookieManager.setCookie(url, cookieValue); CookieSyncManager.getInstance().sync(); } catch (Exception e) { Log.e("==异常==", e.toString()); } } /** * save cookie * @param context */ public static void saveCookie(Context context){ DbCookieStore instance = DbCookieStore.INSTANCE; Listcookies = instance.getCookies(); String cookie = ""; for(int i=0;i