HarmonyOS 使用http请求,接口请求一直超时?

当Content-Type请求头设为application/x-www-form-urlencoded时,extraData值设为对应字符串后接口请求一直超时。

请求url:https://xyg-appapi-dev.kmcharge.com/capi/public/send\_vcode

请求options:

{
  "method":"POST", 
  "header": {
    "Content-Type":"application/x-www-form-urlencoded", 
    "Cookie":"did=;token=;cst=3;",
    "Authorization":"did=;token=;cst=3;", 
    "Host":"xyg-appapi-dev.kmcharge.com", 
    "Content-Length":147
  },
  "extraData":"reqTime=1721200276367&corpCode=999901111&params=HJt57%2B76px9HvFIIt9%2FEPGG23ZabMi%2B%2BzNVYWQ04n%2FA%3D&reqSign=0478D2E1B893D610E9A9C9D4090B8914"
}
阅读 523
1 个回答

请参考示例如下:

// 引入包名
import http from '@ohos.net.http';
import { BusinessError } from '@ohos.base';
import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import picker from '@ohos.file.picker';
import request from '@ohos.request';

// 获取应用文件路径
/*let context = getContext(this) as common.UIAbilityContext;
let cacheDir = context.cacheDir;
let fileUri = cacheDir + '/test.txt'
console.error(fileUri);
// 新建一个本地应用文件
let file = fs.openSync(fileUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(file.fd, 'upload file test');
fs.closeSync(file);*/
let httpRequest = http.createHttp();
let opt: http.HttpRequestOptions = {
  method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
  header: {
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "did=;token=;cst=3;",
    "Authorization": "did=;token=;cst=3;",
    "Host": "xyg-appapi-dev.kmcharge.com",
    "Content-Length": 147
  },
  extraData: encodeURI("reqTime=1721200276367&corpCode=999901111&params=HJt57%2B76px9HvFIIt9%2FEPGG23ZabMi%2B%2BzNVYWQ04n%2FA%3D&reqSign=0478D2E1B893D610E9A9C9D4090B8914"),
  connectTimeout: 30000,
}
httpRequest.request(// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
  "https://xyg-appapi-dev.kmcharge.com/capi/public/send_vcode", opt,
  (err: BusinessError, data: http.HttpResponse) => {
    if (!err) {
      // data.result为HTTP响应内容,可根据业务需要进行解析
      console.info('Result:' + JSON.stringify(data.result));
      console.info('code:' + JSON.stringify(data.responseCode));
      console.info('type:' + JSON.stringify(data.resultType));
      // data.header为HTTP响应头,可根据业务需要进行解析
      console.info('header:' + JSON.stringify(data.header));
      console.info('cookies:' + JSON.stringify(data.cookies)); // 自API version 8开始支持cookie

      httpRequest.destroy();
    } else {
      console.info('error:' + JSON.stringify(err));
      // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。
      httpRequest.destroy();
    }
  });

@Entry
@Component
struct UpLoadFile3 {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏