First, let’s compare the two by viewing their actual HTTP requests with tcpdump so we can get an idea for what may be happening:
tcpdump -vvASs 0 port 80 and host www.readwrite.com
# Net::HTTP.get_response(uri)
GET /2013/12/04/google-compute-engine HTTP/1.1
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
User-Agent: Ruby
Host: readwrite.com
# Net::HTTP.new(uri.host).request(Net::HTTP::Get.new(url))
GET http://readwrite.com/2013/12/04/google-compute-engine HTTP/1.1
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
User-Agent: Ruby
Connection: close
Host: readwrite.com
We can see that the second request is incorrectly requesting the full URL (with hostname) as the path. This is because you’re passing url
to Net::HTTP::Get.new
which causes Net::HTTP::Get.new(url).path
to be just what we see above: the full URL with hostname. Instead pass the URI instance (uri
) to Net::HTTP::Get.new
:
Net::HTTP.new(uri.host).request(Net::HTTP::Get.new(uri))
#=> #<Net::HTTPOK 200 OK readbody=true>
And its tcpdump is now effectively the same as the first’s:
GET /2013/12/04/google-compute-engine HTTP/1.1
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
User-Agent: Ruby
Host: readwrite.com
Connection: close