简体   繁体   中英

ECONNREFUSED error https.request

I am trying to download or get a file on remote https server. I get the following error every time while other URLs are working.

{ [Error: connect ECONNREFUSED]                                                                                                                                                                                                                                                                                           
  code: 'ECONNREFUSED',                                                                                                                                                                                                                                                                                                   
  errno: 'ECONNREFUSED',                                                                                                                                                                                                                                                                                                  
  syscall: 'connect' }

I am using this code:

var https   = require('https');
var fs      = require('fs');

var url = '/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml';
//or:     '/portalfront/datafiles/miscellaneous/csv/kursliste.csv'
var options = {
  hostname  : 'dnb.no',
  port      : 443,
  path      : url,
  method    : 'GET'
};

var req = https.request(options, function(res) {
  console.log("statusCode: ", res.statusCode);
  console.log("headers: ", res.headers);
  res.on('data', function(d) {
      process.stdout.write(d);
  });
});
req.end();

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

I can fetch the url with postman.

To inspect request send by the browser you can use build in inspector. In network card you can see all the request data. The requested file should be there. If you look at the browser request headers, you will get similar results to this:

GET /portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml HTTP/1.1
Host: www.dnb.no
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,pl;q=0.6
If-None-Match: "5365-5604f43f"
If-Modified-Since: Fri, 25 Sep 2015 07:14:07 GMT

If you look closely you will see the difference in a host. Change hostname from:

hostname  : 'dnb.no',

to:

hostname  : 'www.dnb.no',

Now you shold be able to retrieve an xml file successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM