2

I just started with TCPSockets. I am simply trying to get the google home page. This is my code:

require 'socket'

host = 'http://www.google.com'
port = 80

s = TCPSocket.open host, port
s.puts "GET / HTTP/1.1\r\n"
s.puts "Host: Firefox"
s.puts "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
s.puts "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
s.puts "\r\n"

while line = s.gets
  puts line.chop
end


s.close

This returns:

HTTP/1.1 302 Document has moved
Location: http://92.242.140.29/?nxdomain=http%3A%2F%2Ffirefox&AddInType=2&PlatformInfo=pbrgen

Why? My goal is to get the contents of google home page. Thanks

0xSina
  • 19,923
  • 31
  • 130
  • 250

2 Answers2

6
require 'socket'

host = 'www.google.com'
port = 80

s = TCPSocket.open host, port
s.puts "GET / HTTP/1.1\r\n"
s.puts "\r\n"

while line = s.gets
  puts line.chop
end

s.close

Also, using a real HTTP client will make your life much, much easier. I like Typhoeus.

yanot
  • 325
  • 4
  • 15
ezkl
  • 3,819
  • 21
  • 37
  • 1
    Hi, now I get 400 Bad Request and html body with "Your browser sent a request that this server could not understand" – 0xSina Dec 27 '11 at 22:36
  • The `puts` method adds a trailing `\n`, which messes up the format of the request. Using `s.send(msg, 0)` instead should fix the problem. – Benji XVI Sep 18 '14 at 15:39
0

A 302 status is a type of HTTP redirect, but here you're working with TCP, a network layer below HTTP, which doesn't understand redirects (or anything else HTTP). As this SO post shows, howerver, there are other ways to request a web page, namely using the OpenURI library instead of sockets.

Community
  • 1
  • 1
sczizzo
  • 3,156
  • 19
  • 28