2013-05-09 137 views
4

我试图从node.js调用https web服务。我在代理之后,所以我提供代理和端口以及凭证。但我发现了以下错误在node.js中调用https web服务(代理服务器后台)

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

以下是在我试图调用HTTPS Web服务代码片段:

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); 
}); 

任何人都可以请帮我解决这个问题?

由于提前, 马诺

回答

4

我有错误使用

var https = require('https'); 

同样的问题,但我的代理需要HTTP只,代理会负责,因为你设置完成请求HTTPS它path参数

(...) 
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"}}} 

亲切的问候

+0

@ BaikeN9Even我的代理需要HTTP只。如果是这种情况,我该如何纠正这个问题?你能否帮我解决这个问题? – Manoj 2013-05-10 06:05:58

+0

我编辑了我的答案,并添加了一些解释代码 – BaikeN9 2013-05-10 19:55:47