这样用:
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
你可以用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 <