HTTPS 请求配置
本节主要介绍升级版 SDK 对于 HTTPS 请求方式的配置。
可以在 Client 中设置 OpenAPI 的请求协议,请尽量使用 HTTPS。若不设置则用 OpenAPI 默认支持的协议类型(HTTPS):
// 配置通过 HTTPS 协议发送请求
const config = new $OpenApi.Config({
protocol: 'https'
});
重要
使用 HTTPS 协议访问 OpenAPI 时,SDK 会默认开启校验 SSL/TLS 证书有效性,若您代码环境没有证书环境,则会报错证书校验失败。
为保障通信安全,建议您保持开启,若在测试环境必须忽略证书校验,可以通过运行时参数ignoreSSL
设置:
const runtime = new $Util.RuntimeOptions({
// true 忽略证书校验;false 设置证书校验
ignoreSSL: true
});
下面为完整示例。
TypeScript 版本:
import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
import * as $OpenApi from '@alicloud/openapi-client';
import Credential from '@alicloud/credentials';
export default class Client {
static async main(): Promise<void> {
// 默认 Credential
const cred = new Credential();
const config = new $OpenApi.Config({
credential: cred,
// 访问的域名
endpoint: "ecs.cn-beijing.aliyuncs.com"
protocol: 'https'
});
const client = new Ecs20140526(config);
const request = new $Ecs20140526.ModifySecurityGroupRuleRequest({
resourceOwnerAccount: "test",
resourceOwnerId: 1,
ownerAccount: "test",
regionId: "test",
});
// 创建RuntimeObject实例并设置运行参数。
const runtime = new $Util.RuntimeOptions({
// true 忽略证书校验;false 设置证书校验。默认为校验。
ignoreSSL: true
});
const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
console.log(resp.headers);
console.log(resp.body);
}
}
JavaScript 版本:
const { default: Ecs20140526, ModifySecurityGroupRuleRequest } = require('@alicloud/ecs20140526');
const { Config } = require('@alicloud/openapi-client');
const { RuntimeOptions } = require('@alicloud/tea-util');
const { default: Credential } = require('@alicloud/credentials');
async function main() {
// 默认 Credential
const cred = new Credential();
const config = new Config({
credential: cred,
// 访问的域名
endpoint: "ecs.cn-beijing.aliyuncs.com"
protocol: 'https'
});
const client = new Ecs20140526(config);
const request = new ModifySecurityGroupRuleRequest({
resourceOwnerAccount: "test",
resourceOwnerId: 1,
ownerAccount: "test",
regionId: "test",
});
// 创建RuntimeObject实例并设置运行参数。
const runtime = new RuntimeOptions({
// true 忽略证书校验;false 设置证书校验。默认为校验。
ignoreSSL: true
});
const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
console.log(resp.headers);
console.log(resp.body);
}