【问题标题】:Calling a https web service in node.js (Behind proxy)在 node.js 中调用 https Web 服务(代理后面)
【发布时间】:2013-05-09 10:35:27
【问题描述】:

我正在尝试从 node.js 调用 https Web 服务。我在代理后面,所以我在选项中提供代理和端口以及凭据。但我收到以下错误

[Error: 2060:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol :openssl\ssl\s23_clnt.c:683: ]

以下是我尝试调用 https 网络服务的代码 sn-p:

var https = require('https'); 
var username = 'username';
var password = 'password';
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
var data = '';
var options = { 
hostname: 'proxy', 
port: 8080, 
path: 'https://crm.zoho.com/crm/private/json/Leads/getMyRecords?authtoken=11111tsvs26677026bcba45ae3f&scope=crmapi',
headers: {
        "Proxy-Authorization" : auth
     }
};

console.log("options- " + JSON.stringify(options) + '\n');

var req = https.request(options, function(res) {

console.log("statusCode- " + res.statusCode);
console.log("headers-" + res.headers);

res.on('data', function(chunk) {
    data += chunk;
});

res.on('end', function (chunk) { 
    console.log('response-' + data);
});

});

req.end();

req.on('error', function(e) {
     console.error(e);
});

谁能帮我解决这个问题?

提前致谢, 马诺吉

【问题讨论】:

    标签: node.js https proxy


    【解决方案1】:

    我误用了同样的问题

    var https = require('https'); 
    

    但我的代理只需要 HTTP,代理将负责完成请求 HTTPS,因为您在路径参数上设置了它

    (...)
    path: 'https://crm.zoho.com/crm/private/json/Leads/getMyRecords?authtoken=11111tsvs26677026bcba45ae3f&scope=crmapi',
    (...)
    

    这对我有用:

    // baiken9: use http here
    var http = require('http');
    
    var username = 'username';
    var password = 'password';
    var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
    var data = '';
    var options = {
      hostname: 'proxy',
      // baiken9: Proxy Port
      port: 8080,  
      // baiken9: Add method type in my case works using POST
      method: "POST", 
      // baiken9: when proxy redirect you request will use https. It is correct as is
      path: 'https://crm.zoho.com/crm/private/json/Leads/getMyRecords?authtoken=11111tsvs26677026bcba45ae3f&scope=crmapi',
      headers: {
        // baiken9: I cannot test it my proxy not need authorization
        "Proxy-Authorization" : auth,
        // baiken9: required for redirection
        host: "crm.zoho.com",
      // baiken9: length of data to send
        'content-length': 0
      }
    };
    
    console.log("options- " + JSON.stringify(options) + '\n');
    
    
    var req = http.request(options, function(res) {
    
      console.log("statusCode- " + res.statusCode);
      console.log("headers-" + res.headers);
    
      res.on('data', function(chunk) {
        data += chunk;
      });
    
      res.on('end', function(chunk) {
        console.log('response-' + data);
      });
    
    });
    
    req.end();
    
    req.on('error', function(e) {
      console.error(e);
    });
    

    输出:

    statusCode- 200
    headers-[object Object]
    response-{"response":{"error":{"message":"Invalid Ticket Id","code":"4834"}}}
    

    亲切的问候

    【讨论】:

    • @BaikeN9即使我的代理也只需要http。如果是这种情况,我该如何纠正这个问题?你能帮我解决这个问题吗?
    • 我已经编辑了我的答案,并添加了一些 cmets 解释的代码
    猜你喜欢
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多