HarmonyOS WebView加载本地html文件的时候如何设置Cookie?

根据文档,WebView加载网络链接的时候 可以设置Cookie,代码如下:

Web({ src: "https://www.huawei.com", controller: this.webController })
  .onPageEnd(() => {
    try {
      web_webview.WebCookieManager.configCookieSync("https://www.huawei.com", 'test=1');
    } catch (error) {
      let e: business_error.BusinessError = error as business_error.BusinessError;
      console.error(`ErrorCode: ${e.code},  Message: ${e.message}`);
    }
  })

但是如果加载本地文件:

Web({ src: $rawfile("index.html"), controller: this.webController })
  .onPageEnd(() => {
    try {
      web_webview.WebCookieManager.configCookieSync("index.html", 'test=1');
    } catch (error) {
      let e: business_error.BusinessError = error as business_error.BusinessError;
      console.error(`ErrorCode: ${e.code},  Message: ${e.message}`);
    }
  })

这样写不起作用,请问应该如何写?

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa
  1. 通过拦截主资源的方式解决 主资源请求改为http请求,拦截主资源,读取本地的文件返回给内核
  2. 设置cookie的时候指定 samesite=none;secure 但是这种需要把file协议里发起的http请求改为https,需要服务器支持。示例代码如下:
import webview from '@ohos.web.webview';

@Entry
@Component
struct MainPage {
  @State isLoading: boolean = true
  @State msg: string = ""
  controller: webview.WebviewController = new webview.WebviewController();

  aboutToAppear() {
    webview.WebviewController.setWebDebuggingAccess(true);
    webview.WebCookieManager.configCookieSync('https://xxx/index.html',' samesite=non;')
  }

  build() {
    Row() {
      Column() {
        Button().onClick(()=>{
          console.log('cookie为:'+webview.WebCookieManager.fetchCookieSync('https://xxx/index.html'))
        })
        Web({ src: "https://xxx/index.html", controller:this.controller })
          .javaScriptAccess(true)
          .domStorageAccess(true)
          .fileAccess(true)
          .onInterceptRequest((event) => {
            if (!event) {
              return;
            }
            let requestUrl:string = event.request.getRequestUrl();
            console.log('请求为:'+event.request.getRequestUrl())
            interface HtmlMimeType{
              type:string,
              mime:string
            }
            let typeArr :Array<HtmlMimeType> = [{
              type:".html",
              mime:"text/html"
            },{
              type:".js",
              mime:"text/javascript"
            },{
              type:".css",
              mime:"text/css"
            },{
              type:".wasm",
              mime:"application/wasm"
            }];
            let isSearch:boolean = false;
            let searchTypeObj : HtmlMimeType;
            for (const typeObj of typeArr) {
              let index:number = requestUrl.lastIndexOf(typeObj.type);
              if (index > 0) {
                isSearch = true;
                searchTypeObj = typeObj;
                break;
              }
            }
            if (isSearch) {
              let rawfileName :string = requestUrl.replace("https://xxx/","");
              let endIndex = rawfileName.indexOf("?");
              if (endIndex > - 1) {//如果没去除,文件名会导致错误
                rawfileName = rawfileName.substring(0,endIndex);
              }
              let response = new WebResourceResponse();
              response.setResponseData($rawfile(rawfileName));
              response.setResponseEncoding('utf-8');
              response.setResponseMimeType(searchTypeObj!.mime);
              response.setResponseCode(200);
              response.setReasonMessage('OK');
              response.setResponseIsReady(true);
              return response;
            }
            return null;
          })
          .width('100%')
          .height('100%')
      }
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS webview加载本地html问题
807浏览 • 1回复 待解决
HarmonyOS webview如何加载沙箱html
647浏览 • 1回复 待解决
HarmonyOS 如何加载本地沙盒中html
321浏览 • 1回复 待解决
HarmonyOS webview组件如何加载html代码?
665浏览 • 1回复 待解决
HarmonyOS 集群环境设置webviewcookie
253浏览 • 1回复 待解决
HarmonyOS Web组件加载html文件异常
746浏览 • 1回复 待解决
web组件对html文件加载
993浏览 • 1回复 待解决