【关键字】
API 10 / HTTP请求 / 自签名证书 / 2300077 / CA证书
【问题描述】
API 10中使用HTTP网络请求携带自签名证书,提示错误码2300077 SSL CA证书不存在或没有访问权限。
相关组件代码如下:
Button('http-capath').onClick((event: ClickEvent) => {
// getContext(this).area = 0
let context: Context = getContext(this);
const keyPemConent = context.resourceManager.getRawFileContentSync('key.pem')
let filesDir: string = context.filesDir
let filePath = filesDir + "/aaa.pem";
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(file.fd, keyPemConent.buffer);
fs.fsyncSync(file.fd);
fs.closeSync(file);
const httpRequest = http.createHttp();
httpRequest.request(// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"https://10.*.*.102:2233/?id=http-request",
{
method: http.RequestMethod.GET,
caPath: filePath,
},
(err: BusinessError, data: http.HttpResponse) => {
if (!err) {
// data.result为HTTP响应内容,可根据业务需要进行解析
console.info('Result:' + JSON.stringify(data.result));
console.info('code:' + JSON.stringify(data.responseCode));
// data.header为HTTP响应头,可根据业务需要进行解析
console.info('header:' + JSON.stringify(data.header));
console.info('cookies:' + JSON.stringify(data.cookies)); // 8+
// 当该请求使用完毕时,调用destroy方法主动销毁
httpRequest.destroy();
}
else {
console.error('error:' + JSON.stringify(err));
// 当该请求使用完毕时,调用destroy方法主动销毁
httpRequest.destroy();
}
}
);
})
【解决方案】
使用capath字段可进行自签名证书的校验,返回77证书无效报错。请检查使用证书格式或内容是否有误。