ruby HTTP Reponse的使用方法
require 'net/http'
h = Net::HTTP.new('www.google.com', 80)
response = h.get('/index.html', nil)
if response.message == "OK"
puts response.body.scan(/
end
get response 代码
require 'uri'
Net::HTTP.get(URI.parse("http://www.111com.net"))
response = Net::HTTP.get_response('www.111com.net', '/about/')
puts case response.code[0] # Check the first byte of the response code.
when ?1 then "Status code indicates an HTTP informational response."
when ?2 then "Status code indicates success."
when ?3 then "Status code indicates redirection."
when ?4 then "Status code indicates client error."
when ?5 then "Status code indicates server error."
else "Non-standard status code."
end
获取远程地址
require 'uri'
Net::HTTP.get(URI.parse("http://www.111com.net"))
response = Net::HTTP.get_response('www.111com.net', '/about/')
puts "Success!" if response.is_a? Net::HTTPOK
# Success!