【问题标题】:node js https not working behind corporate proxy节点 js https 在公司代理后面不起作用
【发布时间】:2015-08-21 00:15:46
【问题描述】:
it always gives :
Error: getaddrinfo ENOTFOUND <hostname without http or https>
    at errnoException (dns.js:44:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:94:26)
STATUS: undefined

我的代码如下:-

var Http = require('https');
var str = "";
var options = {
    hostname: '<host name without http or https>',
    path: '<path>',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json;charset:utf-8',
        'Content-Length': payload.length,
        'User-Agent': 'Node.js/0.12.7',
        'Proxy-Connections': 'keep-alive'
    }
};

var req = Http.request(options, function(res) {
    res.setEncoding('utf-8');
    res.on('data', function(response) {
        str += response;
    });
    res.on('end', function() {
        return exits.success(str);
    });
});
req.on('error', function(e) {
    console.log('STATUS: ' + e.statusCode);
    exits.error('problem with request: ' + e.message);
});
req.write(payload);
req.end();

我已经尝试过以下操作:- - 设置代理和 https-proxy
- 从主机名中删除 http 或 https

请帮我解决这个问题。

【问题讨论】:

  • options对象中需要填写实际的hostnamepath
  • 感谢您的回复,但我已经尝试过了,但没有任何帮助。

标签: node.js https proxy request


【解决方案1】:

在您的请求选项中设置agent 参数,以便它可以处理公司代理。

var Http = require('https');
var HttpsProxyAgent = require('https-proxy-agent');

// Use the environment variables or set proxy server manually.
var proxyServer = process.env.http_proxy ||
                  process.env.HTTP_PROXY ||
                  process.env.https_proxy ||
                  process.env.HTTPS_PROXY ||
                 'https://168.63.76.32:3128';

var options = {
    agent: new HttpsProxyAgent(proxyServer ),       // <-- proxy agent
    hostname: '<host name without http or https>',
    path: '<path>',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json;charset:utf-8',
        'Content-Length': payload.length,
        'User-Agent': 'Node.js/0.12.7',
        'Proxy-Connections': 'keep-alive'
    }
};

有关更多信息,请参阅委托代理文档:

https://www.npmjs.com/package/http-proxy-agent

https://www.npmjs.com/package/https-proxy-agent

【讨论】:

    猜你喜欢
    • 2015-04-23
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多