node.js - nodejs的request库访问这个url为什么access denied?
PHP中文网
PHP中文网 2017-04-17 11:07:51
[Node.js讨论组]

这样用:

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
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(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
< 
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号