Node 如何模拟客户端发送带有 cookie 的请求?
发布于 9 年前 作者 chokcoco 4859 次浏览 最后一次编辑是 8 年前 来自 问答

譬如我想请求我个人知乎的主页,但是需要带上cookie才能看到关注的人与被关注的人的页面。

  var catchFirstUrl = 'http://www.zhihu.com/people/coco-chok/followees';	//入口页面
  function onRequest(req, res){
		// 伪造cookie
		res.writeHead(200,{
		  'Set-Cookie': '__utma=51854390.1513931145.1444970178.1444970178.1444970178.1;....',
          'Content-Type': 'text/html'	
		})
		// 用 superagent 去抓取 入口页面 的内容
		superagent.get(catchFirstUrl)
			.end(function(err,sres){
		  // 常规的错误处理
	      if (err) {
	        console.log(err);
	    }

      var $ = cheerio.load(sres.text);
      console.log("sres.text is "+ sres.text);
	  
      res.end('Hello World\n<script>console.log(document.Cookie)</script>');
		})
	}
	
http.createServer(onRequest).listen(8888);

利用上面的写法,将登录态下的cookie全部加上,返回的页面 cookie 依旧是未登录态。 所以想问下可以用什么方法 伪造携带cookie去请求页面。

3 回复

是否 superagent 有方法可以携带cookie作请求?

這種問題,curl最擅長

试试request~里面有cookie相关的内容

回到顶部