2013-01-19 199 views
4

我试图通过TCP套接字发送HTTP请求。NodeJS - 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。

谢谢大家! :)

+0

,为什么要找'\ N',而不是'\ r \ N'的规格要求?您还需要通过发送两个'\ r \ n'完成请求。你想手动建立一个请求还是可以使用['http.request()'](http://nodejs.org/api/http.html#http_http_request_options_callback)? – CodeCaster

+0

http://stackoverflow.com/questions/9577611/http-get-request-in-node-js-express/9577651#9577651 – bryanmac

+1

只是想用TCP套接字来写一些HTTP请求来学习新东西。 – RadiantHex

回答

2

如果你安装了谷歌浏览器,你可以看到发送到谷歌的确切的获取请求。这是我的样子:

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. 
+0

非常感谢这些信息。但我没有从Google那里得到任何答复。至少通过'client.on('data',...)'我不是。 – RadiantHex

+0

“你写这个的方式,你试图得到像http://www.google.com/http://www.google.com/” - 这是错误的。 http get请求支持绝对URI。此外,使用绝对URI是获取http获取请求以使用代理的唯一方式。 –

+1

@RadiantHex - 你给它两条换行符吗? – Malvolio