转自:http://augustl.com/blog/2010/ruby_net_http_cheat_sheet
I always have to look up how to use Net::HTTP
, and I never find what I’m looking for. Behold, a cheat sheet!
A basic request
require "net/https"
require "uri"
uri = URI.parse("http://google.com")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header({"User-Agent" => "My Ruby Script"})
response = http.request(request)
puts response.code
# => 301
puts response["location"] # All headers are lowercase
# => http://www.google.com/
URI
uri = URI.parse("http://mysite.com/some_api")
uri = URI.parse("https://mysite.com/thing?foo=bar")
# URI will also guess the correct port
URI.parse("http://foo.com").port # => 80
URI.parse("https://foo.com/").port # => 443
# Full reference
uri = URI.parse("http://foo.com/this/is/everything?query=params")
# p (uri.methods - Object.methods).sort
p uri.scheme # => "http"
p uri.host # => "foo.com"
p uri.port # => 80
p uri.request_uri # => "/this/is/everything?query=params"
p uri.path # => "/this/is/everything"
p uri.query # => "query=params"
# There are setters as well
uri.port = 8080
uri.host = "google.com"
uri.scheme = "ftp"
p uri.to_s
# => "ftp://google.com:8080/this/is/everything?query=param"
Everything else
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 3 # in seconds
http.read_timeout = 3 # in seconds
# The request.
request = Net::HTTP::Get.new(uri.request_uri)
# All the HTTP 1.1 methods are available.
Net::HTTP::Get
Net::HTTP::Post
Net::HTTP::Put
Net::HTTP::Delete
Net::HTTP::Head
Net::HTTP::Options
request.body = "Request body here."
request.initialize_http_header({"Accept" => "*/*"})
request.basic_auth("username", "password")
response = http.request(request)
response.body
response.status
response["header-here"] # All headers are lowercase
SSL
# Normal ssl
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
# SSL .pem certs
pem = File.read("/path/to/my.pem")
http.use_ssl = true
http.cert = OpenSSL::X509::Certificate.new(pem)
http.key = OpenSSL::PKey::RSA.new(pem)
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
# Check for SSL dynamically. If your URI is https and you don't specify a
# port, the port will be 443, which is the correct SSL port.
http.use_ssl = (uri.port == 443)