智能客服
你问我答,随时在线为你解决问题
rcp请求过程中,如何增加query参数,以及在请求拦截场景,如何在拦截器中增加query参数?
在rcp请求时,使用uri.URI接口输入请求资源地址构建URI类,使用addQueryValue接口添加参数。在拦截场景中,新建一个自定义的拦截器,拦截器中使用addQueryValue接口添加参数,自定义拦截器传入SessionConfiguration中,在创建rcp会话时,作为入参传入,具体示例代码如下:
import uri from '@ohos.uri'; import { rcp } from '@kit.RemoteCommunicationKit'; import { common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { url } from '@kit.ArkTS'; // 模拟拦截器开关 export class InterceptorSwitch { isNeedInterceptor: boolean = true; public constructor(isNeedInterceptor: boolean) { this.isNeedInterceptor = isNeedInterceptor; } } // 定义RequestUrlChangeInterceptor拦截器 export class RequestUrlChangeInterceptor implements rcp.Interceptor { private readonly interceptorSwitch: InterceptorSwitch; constructor(interceptorSwitch: InterceptorSwitch) { this.interceptorSwitch = interceptorSwitch; } // 自定义请求处理逻辑 async intercept(context: rcp.RequestContext, next: rcp.RequestHandler): Promise<rcp.Response> { if (this.interceptorSwitch.isNeedInterceptor) { console.info('[RequestUrlChangeInterceptor]: Network need Interceptor'); console.info('[RequestUrlChangeInterceptor] href: ' + context.request.url.href); let uriBuilder = new uri.URI(context.request.url.href); let finalUrl = uriBuilder.addQueryValue('r', '0').toString(); console.log('[RequestUrlChangeInterceptor] finalUrl: ' + finalUrl); context.request.url = url.URL.parseURL(finalUrl); } else { console.info('[RequestUrlChangeInterceptor]: Network do not need Interceptor'); } return next.handle(context); } } @Entry @Component struct Index { context = this.getUIContext().getHostContext() as common.UIAbilityContext; savePath = this.context.filesDir; needInterceptor = new InterceptorSwitch(true); build() { RelativeContainer() { Button('RCP Add Query Value') .fontWeight(FontWeight.Bold) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .onClick(() => { let downloadUrl: string = ''; try { // 下载链接需根据自身业务自行配置 downloadUrl = this.context.resourceManager.getStringSync($r('app.string.download_url').id); } catch (error) { console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); } if (downloadUrl === '') { return; } let uriBuilder = new uri.URI(downloadUrl); let finalUrl = uriBuilder.addQueryValue('pid', 'ImgRaw'); let finalUrlStr = finalUrl.toString(); console.log('Hello World: ' + finalUrlStr); let downloadToFile: rcp.DownloadToFile = { kind: 'folder', path: this.savePath // 请根据自身业务选择合适的路径 } as rcp.DownloadToFile; const sessionConfig: rcp.SessionConfiguration = { interceptors: [ new RequestUrlChangeInterceptor(this.needInterceptor), ], }; const session = rcp.createSession(sessionConfig); session.downloadToFile(finalUrlStr, downloadToFile).then((response) => { if (response) { console.info(`Succeeded in getting the url ${JSON.stringify(response.request.url)}`); } }).catch((err: BusinessError) => { console.error(`DownloadToFile failed, the error message is ${JSON.stringify(err)}`); }); }); } .height('100%') .width('100%'); } }