2013-01-19 5 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); 
}); 

희망 누군가가 나를 도울 수 : 여기


는 코드입니다.


명확히하기 위해 ... requst에는 두 개의 끝나는 줄 바꿈이없고 모든 줄 바꿈은/r/n 형식이어야합니다.

감사합니다. :)

+0

왜 보냅니 까'\ n'이 아니라'\ 연구 \ 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

Google 크롬이 설치되어 있으면 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 

첫 번째보기를 HTTPS : //www.google.com 페이지 및 HTTP에 보내는 : // www.google.com

또 다른 것은 "\ n"을 사용하고 "\ r \ n"을 사용해야하며 요청은 "\ r \ n \ r \ n"으로 끝나야한다는 것입니다.

여전히 응답을받을 수없는 경우 http://google.com 대신 http://77.214.52.152/을 사용해보세요.

1
GET/

당신이 그것을 쓴 방법, 당신은 http://www.google.com/http://www.google.com/

같은 것을 얻을 노력하고있다 그리고 당신은 마지막에 새로운 라인을 필요로하기 때문에 웹 서버는 완료 알고있다. 그래서 다시는 아무것도 얻지 못합니다. 아직 당신을 기다리고 있습니다.

항상 먼저 텔넷으로이 일을하려고 :

$ 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 get 요청이 프록시로 작동하도록하는 유일한 방법입니다. –

+1

@RadiantHex - 두 개 줄 바꿈을 했습니까? – Malvolio