• Lv0
    粉丝0

积分186 / 贡献0

提问33答案被采纳3文章0

onInterceptRequest拦截URL并自定义HTML文件,页面加载失败

一只杨

沙发 发表于 2023-10-20 17:20:20

设置拦截器时,如果只设置setResponseData,内核将无法识别到这是个HTML文件,需要同时设置setResponseEncoding、setResponseMimeType、setResponseHeader等参数。

.onInterceptRequest((event) => {

    console.log('url:' + event.request.getRequestUrl())

    this.responseweb = new WebResourceResponse();

    var head1:Header = {

      headerKey:"Connection",

      headerValue:"keep-alive"

    }

    var length = this.heads.push(head1)

    this.responseweb.setResponseHeader(this.heads)

    this.responseweb.setResponseData(this.webdata)

    this.responseweb.setResponseEncoding('utf-8')

    this.responseweb.setResponseMimeType('text/html')

    this.responseweb.setResponseCode(200)

    this.responseweb.setReasonMessage('OK')

    return this.responseweb

})

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部