nodejs通过代理(proxy)发送http请求(request)
代码未经测试
var http = require('http')
var opt = {
host:'这里放代理服务器的ip或者域名',
port:'这里放代理服务器的端口号',
method:'POST',//这里是发送的方法
path:' https://www.google.com', //这里是访问的路径
headers:{
//这里放期望发送出去的请求头
}
}
//以下是接受数据的代码
var body = '';
var req = http.request(opt, function(res) {
console.log("Got response: " + res.statusCode);
res.on('data',function(d){
body += d;
}).on('end', function(){
console.log(res.headers)
console.log(body)
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
})
req.end();
example:
var url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=chengdu&key=AIzaSyADem01JiuaQddzqagjdy41ep6SbhxBl8s";
var http = require('http')
var opt = {
host:'127.0.0.1',
port:'8087',
method:'get',//这里是发送的方法
path:url, //这里是访问的路径
headers:{
'Accept-Language':'zh-CN,zh;q=0.8',
'Host':'maps.googleapis.com'
}
}
//以下是接受数据的代码
var body = '';
var req = http.request(opt, function(res) {
console.log("Got response: " + res.statusCode);
res.on('data',function(d){
console.log(d);
body += d;
}).on('end', function(){
console.log(res.headers)
console.log(body)
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
})
req.end();
转载http://www.cnblogs.com/isaak/p/5347323.html
可用代码 但测试50个并发的情况下 图片损失率很大 原因还需要待一步 研究
var url = "https://media.giphy.com/media/26gsuhK3m76muilFK/giphy.gif";
var http = require('http');
var fs = require('fs');
for(var k=0; k < 50;k++){
http.get({
hostname: '213.183.252.156',
port: 8081,
path: url,
headers:{
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate, sdch, br',
'Accept-Language':'zh-CN,zh;q=0.8',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Cookie':'__qca=P0-697910219-1480697572450; _gat_false=1; _dc_gtm_UA-38174542-1=1; __asc=43135410159fa894adceaf3b0d1; __auc=ed9fa693158c073ec678cedd768; _ceg.s=okpfhz; _ceg.u=okpfhz; csrftoken=HIbNkBGJlxa44QY25LwRPiNgULsyN9xB; _ga=GA1.2.644660477.1480697575; _gat=1',
'Host':'media.giphy.com',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36'
},
agent: false // create a new agent just for this one request
}, (res) => {
console.log(res.statusCode);
res.setEncoding("binary"); //一定要设置response的编码为binary否则会下载下来的图片打不开
var imgData = '';
// Do stuff with response
res.on("data", function(chunk){
imgData+=chunk;
});
res.on("end", function(){
fs.writeFile('./a/'+Math.random()+'.gif',imgData,"binary",function(err){
if (err) throw error;
});
})
});
}