如何使用自动生成的SSL密钥将HTTPS请求正确发送到服务器?



我需要一些帮助来Node.js requests. 基本上,我正在尝试将HTTPS GET请求发送到具有自签名证书的服务器。

我正在尝试两种方法,unirestrequest模块。 我正在使用的以下函数:

请求方法:

function sendCommand(command){
request(IP + command, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
return body;
}
else{
console.log(command + " was not sent since an error occured!");
}
});
}

UniRest 方法:

function sendRequest(command){
unirest.get(IP + command)
.end(function(response) {
var body = response.body;
return body;
done();
});
}

在这两种情况下,我总是得到undefined返回值,但服务器处于联机状态并正在运行,可以通过转到以下位置轻松检查:https://3.16.143.68:8080

由于我在SSH中连接到服务器,因此我也可以从那里检查服务器状态:

PM2列表输出:

PM2列表 ┌────────┬────┬──────┬────────┬───┬─────┬───────────┐ │ 名称 │ id │ 模式 │ 状态 │ ↺ │ CPU │ 内存 │ ├────────┼────┼──────┼────────┼───┼─────┼───────────┤ │ 服务器 │ 0 │ 分叉 │ 在线 │ 0 │ 0% │ 50.8 MB │

当然,浏览器会警告我们证书没有签名,但我认为这应该不会在发送请求时产生问题,我错了吗?

感谢您的帮助

节点默认启用证书验证。

您可以通过配置环境变量来全局禁用它:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
request(IP + command, function (error, response, body) {
// Handle response
});

或者,您可以在发出请求时禁用验证:

var opts = {
url: IP + command,
agentOptions: {
rejectUnauthorized: false
}
};
request(opts, function (error, response, body) {
// Handle response
});

最新更新