node js https不在公司代理后面工作

时间:2015-08-21 00:15:46

标签: node.js https proxy request

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

我的代码如下: -

&#13;
&#13;
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();
&#13;
&#13;
&#13;

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

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:-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