【问题标题】:NodeJS - TCP - Sending an HTTP requestNodeJS - TCP - 发送 HTTP 请求
【发布时间】:2013-01-03 06:24:15
【问题描述】:

我正在尝试通过 TCP 套接字发送 HTTP 请求。

但我根本没有收到来自 www.google.com 的任何回复。不知道我做错了什么。


代码如下:

var client, net, raw_request;

net = require('net');

raw_request = "GET http://www.google.com/ HTTP/1.1\nUser-Agent: Mozilla 5.0\nhost: www.google.com\nCookie: \ncontent-length: 0\nConnection: keep-alive";

client = new net.Socket();

client.connect(80, "www.google.com", function() {
  console.log("Sending request");
  return client.write(raw_request);
});

client.on("data", function(data) {
  console.log("Data");
  return console.log(data);
});

希望有人可以帮助我。


澄清一下...请求缺少两个结尾换行符,并且所有换行符都必须采用 /r/n 的格式。

谢谢大家! :)

【问题讨论】:

  • 为什么按照规范要求发送\n 而不是\r\n?您还需要通过发送两个\r\n 来完成请求。您想手动构建请求还是可以使用http.request() 代替?
  • 只是想写一些使用 TCP 套接字的 HTTP 请求来学习新东西。
  • @bryanmac 感谢您的链接。但真的很想在较低的层次上学习如何做到这一点。
  • @RadiantHex 那么你应该开始研究 RFC 2616,尤其是 section 5, Request,如何构造有效的 HTTP 消息。

标签: javascript node.js http networking tcp


【解决方案1】:

如果您安装了 google chrome,您可以看到发送给 google 的确切获取请求。这是我的样子:

GET https://www.google.com/ HTTP/1.1
:host: www.google.com
accept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
accept-encoding: gzip,deflate,sdch
accept-language: en-US,en;q=0.8
user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17
:path: /
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
:version: HTTP/1.1
cache-control: max-age=0
cookie: <lots of chars here>
:scheme: https
x-chrome-variations: CMq1yQEIjLbJAQiYtskBCKW2yQEIp7bJAQiptskBCLa2yQEI14PKAQ==
:method: GET

第一眼看到,chrome 将请求发送到 https://www.google.com,而您发送到 http://www .google.com

另外一件事是你使用的是“\n”,你需要使用“\r\n”,并且请求必须以“\r\n\r\n”结尾。

如果您仍然无法获得任何回复,请尝试使用 http://77.214.52.152/ 而不是 http://google.com

【讨论】:

    【解决方案2】:
     GET /
    

    你写它的方式,你试图得到类似http://www.google.com/http://www.google.com/的东西

    最后你需要两个换行,这样网络服务器就知道你已经完成了。这就是为什么你什么也得不到的原因——它还在等着你。

    总是先用 telnet 试试这些东西:

    $ telnet www.google.com 80
    GET http://www.google.com/ HTTP
    
    HTTP/1.0 400 Bad Request
    Content-Type: text/html; charset=UTF-8
    Content-Length: 925
    Date: Sat, 19 Jan 2013 19:02:06 GMT
    Server: GFE/2.0
    
    <!DOCTYPE html>
    <html lang=en>
      <meta charset=utf-8>
      <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
      <title>Error 400 (Bad Request)!!1</title>
      <style>
        *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
      </style>
      <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
      <p><b>400.</b> <ins>That’s an error.</ins>
      <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>
    Connection closed by foreign host.
    

    【讨论】:

    • 非常感谢您提供的信息。但我根本没有得到谷歌的任何回复。至少通过client.on('data', ...)我不是。
    • “你写它的方式,你试图得到类似google.com/http://www.google.com” - 这是错误的。 http get 请求支持绝对 URI。此外,使用绝对 URI 是获取 http get 请求以使用代理的唯一方法。
    • @RadiantHex -- 你给它两个换行符了吗?
    • @FlorinPetriuc -- 我不知道该告诉你什么。我在 Google 中使用了绝对 URI,如您所见,我收到了 400 错误,即格式错误的请求。给谢尔盖·布林打电话,跟他一起处理。同时,我会使用 HOST: 标头来让代理工作。
    • @Malvolio 这是两条换行符!谢谢!
    猜你喜欢
    • 2018-07-07
    • 1970-01-01
    • 2017-08-02
    • 2018-02-20
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多