node.js - nodejs的request库访问这个url为什么access denied?

 陈家碧玉3 发布于 2022-11-06 01:40

这样用:

var request = require('request');
request('http://graph.facebook.com/http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body)
  }
})

结果body显示出了禁止访问的html平文代码。而用curl却可以得到JSON数据:

curl http://graph.facebook.com/http://www.google.com
1 个回答
  • 你可以用curl -v 来看一下一个请求的过程和信息。

    curl并不是单纯的http请求,而是带了一些header过去。

    request如果不设置options,默认好像缺少一些信息,可能会被服务器识别为有害操作,所以屏蔽了。

    curl -v http://www.html-js.com

    * Adding handle: conn: 0x7fe323004000
    * Adding handle: send: 0
    * Adding handle: recv: 0
    * Curl_addHandleToPipeline: length: 1
    * - Conn 0 (0x7fe323004000) send_pipe: 1, recv_pipe: 0
    * About to connect() to www.html-js.com port 80 (#0)
    *   Trying 42.121.105.152...
    * Connected to www.html-js.com (42.121.105.152) port 80 (#0)
    > GET / HTTP/1.1
    > User-Agent: curl/7.30.0
    > Host: www.html-js.com
    > Accept: */*
    > 
    < HTTP/1.1 200 OK
    * Server nginx/1.0.15 is not blacklisted
    < Server: nginx/1.0.15
    < Date: Fri, 28 Feb 2014 12:20:40 GMT
    < Content-Type: text/html; charset=utf-8
    < Connection: keep-alive
    < Vary: Accept-Encoding
    < X-Powered-By: Express
    < Content-Length: 35669
    < ETag: "-2001532266"
    < Set-Cookie: connect.sess=s%3Aj%3A%7B%7D.fCXYDYnmzIsVbg6xXs7Osf5WzoOis7ziciQGrLr63cQ; Path=/; HttpOnly
    < 
    
    2022-11-10 20:38 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有