【发布时间】:2012-02-02 21:39:50
【问题描述】:
我希望能够通过我自己的 vpn 中的节点发出 http.get 请求,但似乎该功能是将网站传递给 dns 而不是查看我的 vpn。以下代码适用于 google 作为主机。
http = require('http');
var host = "www.google.com";
var options = {host:host, port:80, path:'/'}
http.get( options, function( res ){
res.setEncoding('utf8');
res.on( 'data', function( chunk ){
console.log( chunk );
});
});
但是当我将主机更改为 a_site_on_my_vpn.com 时,它会抛出一个未找到域名的错误,但是当我在 firefox 上键入 a_site_on_my_vpn.com 时,我能够加载该页面。我想我可以解决这个问题的一种方法是找出主机名的 ip,但有没有更简单的方法? :D
我解决了我自己的答案,但要在后续回答中回答这个问题,答案是肯定的,这里有一个 sn-p,它表明,通过进行 DNS ip 查找然后通过 IP 去谷歌。
var dns = require('dns');
var http = require('http');
dns.resolve4('www.google.com', function (err, addresses) {
if (err) throw err;
console.log('addresses: ' + JSON.stringify(addresses));
var options = {
host: addresses[0]
}
http.get(options, function(res){
console.log("Sending http request to google with options", options);
var htmlsize = 0;
res.on('data', function( chunk ){
htmlsize+=chunk.length;
});
res.on('end', function(){
console.log( "got html of lenght", htmlsize );
});
});
});
这是输出
addresses: ["74.125.224.82","74.125.224.81","74.125.224.84","74.125.224.80","74.125.224.83"]
Sending http request to google with options { host: '74.125.224.82' }
got html of lenght 33431
【问题讨论】:
-
只是好奇,如果你确实让 Node 使用 IP 而不是主机名发出请求,它真的有效吗?