0

我可以很高兴地在 CURL 或浏览器下面获得以下 url,但是当我使用节点尝试它时,我得到一个 ECONNREFUSED 错误。

试试这个代码...

var request = require('request')

var url = "https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-ash3/t39.2365-6/851565_602269956474188_918638970_n.png"

var r = request({url:url}, function callback(error, response, body) {
    console.log("Error:", error)
    console.dir(r.headers)
})
4

1 回答 1

0

我认为你的代码有效。我已更改代码以测试某些 url 并记录显示请求正常。

所以,我认为您可以粘贴详细的错误日志以进行更深入的分析!

这是我的测试代码:

var request = require('request')

var urls = ["https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-ash3/t39.2365-6/851565_602269956474188_918638970_n.png",
            "https://www.google.com",
            "http://nodejs.org/"]

urls.forEach(function(url){
  request({url:url}, function callback(error, response) {
    console.log("Response for "+url);
    if(error){
      console.error(error);
    }else{
      console.log("statusCode: "+response.statusCode);
      console.log("body.length: ", response.body.length + " bytes \n");
    }
  })
})

控制台日志:

Response for https://www.google.com
statusCode: 200
body.length:  44898 bytes 

Response for http://nodejs.org/
statusCode: 200
body.length:  6318 bytes 

Response for https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-ash3/t39.2365-6/851565_602269956474188_918638970_n.png
statusCode: 200
body.length:  722 bytes 
于 2014-03-14T03:00:14.123 回答