NodeJS - TCP - 发送HTTP请求

4
我试图通过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()呢? - CodeCaster
https://dev59.com/QGkw5IYBdhLWcg3w4emS#9577651 - bryanmac
1
只是想使用TCP套接字编写一些HTTP请求以学习一些新东西。 - RadiantHex
@bryanmac谢谢你提供的链接。但我真的想学习如何在更底层实现这个。 - RadiantHex
1
@RadiantHex 那么你应该开始查看 RFC 2616,特别是第5节,请求,了解如何构建有效的HTTP消息。 - CodeCaster
@CodeCaster:感谢您的回复。我已经更改了消息中的换行符,并删除了除主机之外的所有标头。仍然没有回复,甚至没有错误消息。 - RadiantHex
2个回答

2
如果您已经安装了Google Chrome浏览器,您可以查看向Google发送的确切GET请求。这是我的请求示例:
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

1
 GET /

从你的写法来看,你似乎想要得到这样的东西:http://www.google.com/http://www.google.com/

而且你需要在结尾处加上两个换行符,这样Web服务器才知道你已经完成了请求。这就是为什么你没有收到任何返回信息——它还在等待你。

在进行这些操作时,最好先用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.

非常感谢提供的信息。但是我从Google那里没有得到任何回复。至少通过client.on('data', ...)我没有收到任何回复。 - RadiantHex
你写的方式是错误的,你试图得到类似于http://www.google.com/http://www.google.com/这样的东西。HTTP GET请求支持绝对URI。此外,使用绝对URI是使HTTP GET请求与代理正常工作的唯一方法。 - Florin Petriuc
1
@RadiantHex -- 你给了它两个换行符吗? - Michael Lorton
@FlorinPetriuc - 我不知道该告诉您什么。我在谷歌上使用了绝对URI,如您所见,我得到了一个400错误,格式错误的请求。请联系谢尔盖·布林并与他沟通。同时,我建议使用HOST:头来使代理工作。 - Michael Lorton
@Malvolio 是两个换行符!谢谢! - RadiantHex
你应该加上 HTTP/1.1 - Brad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接