精华 nodejs通过代理(proxy)发送http请求(request)
发布于 11 年前 作者 DoubleSpout 134897 次浏览 最后一次编辑是 8 年前

有可能有这样的需求,需要node作为web服务器通过另外一台http/https代理服务器发http或者https请求,废话不多说直接上代码大家都懂的:

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();

这样我们就通过了指定代理服务器发出了https的请求,注意这里我们同代理服务器是http协议的,不是https,返回的结果当然肯定会根据你的代理服务器不同有所不同。

Got response: 302
{ location: 'https://www.google.com.tw/',
  'cache-control': 'private',
  'content-type': 'text/html; charset=UTF-8',
  'set-cookie': 
   [ 'PREF=ID=b3cfcb24798a7a07:FF=0:TM=1356078097:LM=1356078097:S=v_3qEd0_gCW6-xum; expires=Sun, 21-Dec-2014 08:21:37 GMT; path=/; domain=.google.com',
     'NID=67=qoJf_z3W7KlibpNZ6xld__r0rYGyYu7l_XiDQmZ3anjBFadDzhijME3QcX651yucne_irK_2JMS8HF5FuxNl85mE0nDrtn9Iq0z2gW69n00OrB970hpHTbYe0mAogZit; expires=Sat, 22-Jun-2013 08:21:37 GMT; path=/; domain=.google.com; HttpOnly' ],
  p3p: 'CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."',
  date: 'Fri, 21 Dec 2012 08:21:37 GMT',
  server: 'gws',
  'content-length': '223',
  'x-xss-protection': '1; mode=block',
  'x-frame-options': 'SAMEORIGIN',
  via: '1.0 ***.****.com:80 (squid/2.6.STABLE21)',
  'proxy-connection': 'keep-alive' }
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://www.google.com.tw/">here</A>.
</BODY></HTML>

谷歌返回了一个302,告诉我们进行跳转,需要访问 https://www.google.com.tw/ 这个地址 博客原文:http://snoopyxdy.blog.163.com/blog/static/60117440201211214207539/

14 回复

cnode临时复活了,@snoopy 同学还在杭州么?

@suqian 没有了,周日傍晚回来的,带儿子玩了西湖、动物园、灵隐寺呵呵,不过现在进cnode要刷新N次,看RP了啊,哈哈

是不是这样就可以实现goagent的功能了?

我想知道能否实现这样的功能:

内网机器A, 外网服务器B. 第三方服务C

A先登录到B, 然后建立管道, 反向代理. C访问B,然后B把request转发给A, 处理后回复给C

就是node作为客户端,通过代理发送http请求了

可以的,这样服务器B就是一个代理服务器

@snoopy 啥时更新文章啊!

node-http-proxy , 这个包用起来更方便~~~

好像不同吧,这个node-http-proxy是让node作为代理服务器,可以正向反向代理,而本文写的是node作为客户端,通过其他的代理服务器发送http请求,是这样吧?

@hades 最近有点小忙,空了再发吧~谢谢关注啊

你好,请问下一,要是爬取youtube的话,我的返回是空的,输出内容如下:

Got response: 301
{ 'cache-control': 'no-cache',
  'content-type': 'text/html; charset=utf-8',
  date: 'Sun, 09 Jul 2017 03:55:52 GMT',
  expires: 'Tue, 27 Apr 1971 19:44:06 EST',
  'keep-alive': 'timeout=38',
  location: 'https://www.youtube.com/feed/trending',
  server: 'YouTubeFrontEnd',
  'x-content-type-options': 'nosniff',
  'x-xss-protection': '1; mode=block; report=https://www.google.com/appserve/security-bugs/log/youtube',
  'content-length': '0' }

我的代码是这样的

var http = require('http')

var opt = {
 host:'127.0.0.1',
 port:'57939',
 method:'GET',//这里是发送的方法
 path:'https://www.youtube.com/feed/trending',//这里是访问的路径
 headers:{
  //这里放期望发送出去的请求头
  'Content-Type': 'application/x-www-form-urlencoded'
 }
}
//以下是接受数据的代码
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();

七年前的帖子,mark一下

回到顶部