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(/<img src="(.*?)"/m).uniq
end
get response 代碼
require 'uri'
Net::HTTP.get(URI.parse("http://www.111cn.net"))
response = Net::HTTP.get_response('www.111cn.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.111cn.net"))
response = Net::HTTP.get_response('www.111cn.net', '/about/')
puts "Success!" if response.is_a? Net::HTTPOK
# Success!