你读取加载url中的cookie 他是实时的 不是存在本地的,所以你第二次打开的时候回cookie就失效了
第一步实现登录功能,然后返回cookie相关参数,在然后将cookie保存在本地中
第二步在加载时候(isNeedLoadUrl)将cookie 拼接传给后台,
//设置cookie
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
mCookieStore.setCookieEnable(true);
mCookieStore.setCookie(url, "Domain="+"1111");
mCookieStore.setCookie(url, "Path=/");
mCookieStore.setCookie(url, "Value=00000");
//获取cookie
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
String cookiestr=mCookieStore.getCookie(url);
HiLogUtils.PrintLog(cookiestr);
设置获取cookie代码如下
public class WebviewSlice extends AbilitySlice {
private static final String EXAMPLE_URL = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=android%E7%89%88%E6%9C%AC%E5%8F%B7&fenlei=256&oq=android%25E7%2589%2588%25E6%259C%25AC%25E4%25BB%25A3%25E5%258F%25B7&rsv_pq=a1072f0e00139b17&rsv_t=1790Ohd03bVT21DCnl7FeJacxdjk58eJxGpr4r41zM6bzt8pHm62TUYAMD4&rqlang=cn&rsv_dl=tb&rsv_enter=0&rsv_btype=t&rsv_sug3=110&rsv_sug1=63&rsv_sug7=100&rsv_sug2=0&inputT=283&rsv_sug4=1617";
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
setUIContent(ResourceTable.Layout_webview_slice);
WebView webView = (WebView) findComponentById(ResourceTable.Id_webview);
webView.getWebConfig().setJavaScriptPermit(true); // 如果网页需要使用JavaScript,增加此行;如何使用JavaScript下文有详细介绍
webView.setWebAgent(new WebAgent() {
@Override
public boolean isNeedLoadUrl(WebView webView, ResourceRequest request) {
if (request == null || request.getRequestUrl() == null) {
// LogUtil.info(TAG,"WebAgent isNeedLoadUrl:request is null.");
return false;
}
String url = request.getRequestUrl().toString();
//设置cookie
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
mCookieStore.setCookieEnable(true);
mCookieStore.setCookie(url, "Domain="+"1111");
mCookieStore.setCookie(url, "Path=/");
mCookieStore.setCookie(url, "Value=00000");
if (url.startsWith("http:") || url.startsWith("https:")) {
webView.load(url);
return false;
} else {
return super.isNeedLoadUrl(webView, request);
}
}
@Override
public void onPageLoaded(WebView webView, String url) {
super.onPageLoaded(webView, url);
//获取cookie
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
String cookiestr=mCookieStore.getCookie(url);
HiLogUtils.PrintLog(cookiestr);
}
});
webView.load(EXAMPLE_URL);
}
}
你读取加载url中的cookie 他是实时的 不是存在本地的,所以你第二次打开的时候回cookie就失效了
第一步实现登录功能,然后返回cookie相关参数,在然后将cookie保存在本地中
第二步在加载时候(isNeedLoadUrl)将cookie 拼接传给后台,
//设置cookie
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
mCookieStore.setCookieEnable(true);
mCookieStore.setCookie(url, "Domain="+"1111");
mCookieStore.setCookie(url, "Path=/");
mCookieStore.setCookie(url, "Value=00000");
//获取cookie
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
String cookiestr=mCookieStore.getCookie(url);
HiLogUtils.PrintLog(cookiestr);
设置获取cookie代码如下
public class WebviewSlice extends AbilitySlice {
private static final String EXAMPLE_URL = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=android%E7%89%88%E6%9C%AC%E5%8F%B7&fenlei=256&oq=android%25E7%2589%2588%25E6%259C%25AC%25E4%25BB%25A3%25E5%258F%25B7&rsv_pq=a1072f0e00139b17&rsv_t=1790Ohd03bVT21DCnl7FeJacxdjk58eJxGpr4r41zM6bzt8pHm62TUYAMD4&rqlang=cn&rsv_dl=tb&rsv_enter=0&rsv_btype=t&rsv_sug3=110&rsv_sug1=63&rsv_sug7=100&rsv_sug2=0&inputT=283&rsv_sug4=1617";
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
setUIContent(ResourceTable.Layout_webview_slice);
WebView webView = (WebView) findComponentById(ResourceTable.Id_webview);
webView.getWebConfig().setJavaScriptPermit(true); // 如果网页需要使用JavaScript,增加此行;如何使用JavaScript下文有详细介绍
webView.setWebAgent(new WebAgent() {
@Override
public boolean isNeedLoadUrl(WebView webView, ResourceRequest request) {
if (request == null || request.getRequestUrl() == null) {
// LogUtil.info(TAG,"WebAgent isNeedLoadUrl:request is null.");
return false;
}
String url = request.getRequestUrl().toString();
//设置cookie
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
mCookieStore.setCookieEnable(true);
mCookieStore.setCookie(url, "Domain="+"1111");
mCookieStore.setCookie(url, "Path=/");
mCookieStore.setCookie(url, "Value=00000");
if (url.startsWith("http:") || url.startsWith("https:")) {
webView.load(url);
return false;
} else {
return super.isNeedLoadUrl(webView, request);
}
}
@Override
public void onPageLoaded(WebView webView, String url) {
super.onPageLoaded(webView, url);
//获取cookie
ohos.agp.components.webengine.CookieStore mCookieStore = ohos.agp.components.webengine.CookieStore.getInstance();
String cookiestr=mCookieStore.getCookie(url);
HiLogUtils.PrintLog(cookiestr);
}
});
webView.load(EXAMPLE_URL);
}
}
举报