Skip to content

Instantly share code, notes, and snippets.

@kyanny
Created May 20, 2011 06:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyanny/982459 to your computer and use it in GitHub Desktop.
Save kyanny/982459 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
Net::HTTP.version_1_2
class MyHTTPClient
@@debug_output = nil
def self.get(url)
uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
http.set_debug_output(@@debug_output) if @@debug_output.kind_of?(IO)
req = Net::HTTP::Get.new(uri.path)
res = http.request(req)
end
def self.set_debug_output(io)
@@debug_output = io
end
end
MyHTTPClient.set_debug_output($stderr)
MyHTTPClient.get('http://www.ruby-lang.org/')
__END__
$ ruby $0
opening connection to www.ruby-lang.org...
opened
<- "GET / HTTP/1.1\r\nConnection: close\r\nAccept: */*\r\nHost: www.ruby-lang.org\r\n\r\n"
-> "HTTP/1.1 302 Found\r\n"
-> "Date: Fri, 20 May 2011 06:41:29 GMT\r\n"
-> "Server: Apache/2.2.3 (Debian) DAV/2 SVN/1.4.2 mod_ruby/1.2.6 Ruby/1.8.5(2006-08-25)\r\n"
-> "Vary: Accept-Language\r\n"
-> "Location: http://www.ruby-lang.org/en/\r\n"
-> "Connection: close\r\n"
-> "Transfer-Encoding: chunked\r\n"
-> "Content-Type: text/html\r\n"
-> "\r\n"
-> "23\r\n"
reading 35 bytes...
-> "<html><body>302 Found</body></html>"
read 35 bytes
reading 2 bytes...
-> "\r\n"
read 2 bytes
-> "0\r\n"
-> "\r\n"
Conn close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment