我提出的请求有问题,我无法弄清楚。似乎一切都符合API 规范。
这是代码:
var request = require('https').request;
var opts = {
host: 'intersango.com',
method: 'GET',
path: '/api/ticker.php',
port: 443,
accept: '*/*',
headers: { 'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0' } };
var req = request(opts, function (result) {
result.setEncoding('utf8');
var buffer = '';
result.on('data', function(data) {
buffer += data;
});
result.on('end', function() {
console.log('<- Received ' + buffer.length + ' bytes');
var data = JSON.parse(buffer);
console.log(data);
});
});
req.on('error', function(e) {
console.log('warning: problem with request: ' + e.message);
});
console.log('-> ' + opts['method'] + ' ' + opts['host'] + ':' + opts['port'] + opts['path']);
req.end();
这是输出:
$> node test.intersango.js
-> GET intersango.com:443/api/ticker.php
warning: problem with request: socket hang up
有趣的是,如果你在浏览器中访问https://intersango.com/api/ticker.php就可以了,但如果你只是访问intersango.com:443/api/ticker.php就不行了。我尝试了其他一些端口,但它只是给出了warning: problem with request: connect ECONNREFUSED
注意:尝试 POST 也无济于事