From: Brian Candler Date: 2007-04-09T04:06:10+09:00 Subject: Re: Trying to GET google with socket....problem On Mon, Apr 09, 2007 at 12:57:09AM +0900, Hey You wrote: > >> Well I don't know why the socket can't connect to Google. Here is my > >> source code: > >> > >> require 'socket' > >> h = TCPSocket.new('www.google.ca',80) > >> h.print "GET /index.html HTTP/1.0\n\n" > >> a = h.read > >> puts a > >> > >> I tried changing the HTTP to 1.1 but it still doesn't work. > > > > Two problems: > > (1) Line terminator for HTTP is \r\n not \n > > (2) You have not supplied a Host: header > > > > h.print "GET /index.html HTTP/1.0\r\nHost: www.google.ca\r\n\r\n" > > > > I say again: you must read and understand RFC 2616. > > > > This documents HTTP/1.1, which has gained a lot of features. You could > > try > > reading the earlier RFCs for HTTP/1.0 or HTTP/0.9 for a simplified > > protocol. > > > > B. > > Have you read what I last posted? Or did you just ignore it and gave me > the answer to a already answered question? Yes I have read RFC2616 more > than once and I do understand a lot of it but not all stays on my head > in the few times I read the document. I don't know but I have read in a > lot of places that for a line terminator you can also use "\n\n" and it > seems to work fine. Read RFC 2616 section 2.2: " HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements except the entity-body (see appendix 19.3 for tolerant applications)." and appendix 19.3 says: " The line terminator for message-header fields is the sequence CRLF. However, we recommend that applications, when parsing such headers, recognize a single LF as a line terminator and ignore the leading CR." So the upshot is: you're sending a malformed request, but some servers may honour it. > Also putting the Host header or adding the full > domain to the code such as "GET www.google.ca/index.html" both specifies > which host we want so I don't see why change them. No, "GET www.google.ca/index.html" is a completely malformed request and will be rejected. In any case this is different to the GET request you actually sent, quoted at the very top of this posting. The hostname is *never* supplied as part of the GET line. Of course you supplied it to Ruby's TCPSocket.new method, but at that point the hostname is converted to an IP address before the connection is opened. The name is not passed to the far end and therefore you must provide a Host: header. I'm sorry, but I'm dropping out of this conversation now. Your response was arrogant. If you know nothing about HTTP, then I suggest you don't go around telling people who know something about HTTP that they are wrong. Regards, Brian.