HarmonyOS 网络请求自定义拦截器如何实现

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

demo如下:

import { rcp } from '@kit.RemoteCommunicationKit';
import url from '@ohos.url';

export interface Logger {
  log: (text: string) => void;
}

export class HeaderInterceptor implements rcp.Interceptor {
  private readonly logger: Logger;

  constructor(logger: Logger) {
    this.logger = logger;
  }

  async intercept(context: rcp.RequestContext, next: rcp.RequestHandler): Promise<rcp.Response> {
    // 修改请请求头内容
    let header = context.request.headers;
    this.logger.log(`[HeaderInterceptor]: curr header is : ${header}`);
    let newHeader: rcp.RequestHeaders | undefined = doSomethingForHeader(header);
    this.logger.log(`[HeaderInterceptor]: new header is : ${header}`);
    context.request.headers = newHeader;

    // 给下个拦截器处理
    return next.handle(context);
  }
}

export class DecryptInterceptor implements rcp.Interceptor {
  private readonly logger: Logger;

  constructor(logger: Logger) {
    this.logger = logger;
  }

  async intercept(context: rcp.RequestContext, next: rcp.RequestHandler): Promise<rcp.Response> {
    let url = context.request.url;
    // 地址不需要解密
    if (!needDecode(url)) {
      // 交给下个拦截器处理
      return next.handle(context);
    }

    // 拿到响应结果
    let response = await next.handle(context);
    if (!response || response.statusCode != 200 || !response.body) {
      Promise.resolve(response);
    }

    // 处理解密逻辑
    return Promise.resolve(decodeResp(response) as rcp.Response);
  }
}

function doSomethingForHeader(header: rcp.RequestHeaders | undefined): rcp.RequestHeaders | undefined {
  return header;
}

// 根据url判断是否解密
function needDecode(url: url.URL) {
  return true;
}


function decodeResp(response: rcp.Response): Response {
  return new Response(decodeBody(response.body), response);
}

class Response {
  request: rcp.Request;
  statusCode: number;
  headers: rcp.ResponseHeaders;
  effectiveUrl?: rcp.URL;
  body?: ArrayBuffer;
  downloadedTo?: rcp.DownloadedTo;
  debugInfo?: rcp.DebugInfo[];
  timeInfo?: rcp.TimeInfo;
  cookies?: rcp.ResponseCookie[];
  httpVersion?: rcp.HttpVersion;
  reasonPhrase?: string;

  constructor(body: ArrayBuffer, response: rcp.Response) {
    this.request = response.request;
    this.statusCode = response.statusCode;
    this.headers = response.headers;
    this.effectiveUrl = response.effectiveUrl;
    this.downloadedTo = response.downloadedTo;
    this.debugInfo = response.debugInfo;
    this.timeInfo = response.timeInfo;
    this.cookies = response.cookies;
    this.httpVersion = response.httpVersion;
    this.reasonPhrase = response.reasonPhrase;
    this.body = body;
  }
}

// 解密 body
function decodeBody(body: ArrayBuffer): string | ArrayBuffer {
  throw new Error('Function not implemented.');
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS rcp请求拦截器
579浏览 • 1回复 待解决
HarmonyOS 导航路由拦截器如何实现
45浏览 • 1回复 待解决
HarmonyOS rcp拦截器
79浏览 • 1回复 待解决
有没有带拦截器网络库可以封装
382浏览 • 1回复 待解决
webview如何实现网络请求拦截功能
2266浏览 • 1回复 待解决
HarmonyOS router有没有拦截器?
30浏览 • 1回复 待解决
HarmonyOS 是否有前置路由拦截器
27浏览 • 1回复 待解决
HarmonyOS ArkTS 如何实现自定义装饰
66浏览 • 1回复 待解决
HarmonyOS web拦截网络请求
63浏览 • 1回复 待解决
HarmonyOS WebView拦截网络请求
501浏览 • 1回复 待解决