Ruby/Network/HTTP Reponse
Материал из Wiki.crossplatform.ru
Checking for Errors and Redirects
require "net/http" def get_web_document(url) uri = URI.parse(url) response = Net::HTTP.get_response(uri) case response when Net::HTTPSuccess: return response.body when Net::HTTPRedirection: return get_web_document(response["Location"]) else return nil end end puts get_web_document("http://www.rubyinside.ru/test.txt") puts get_web_document("http://www.rubyinside.ru/non-existent")
Each key in the response
require "uri" Net::HTTP.get(URI.parse("http://www.oreilly.ru")) response = Net::HTTP.get_response("www.oreilly.ru", "/about/") response.each_key { |key| puts key } # x-cache # p3p # content-type # date # server # transfer-encoding
For each segment received
require "uri" Net::HTTP.get_response("www.oreilly.ru", "/about/") do |response| response.read_body do |segment| puts "Received segment of #{segment.size} byte(s)!" end end
Get response code
require "uri" Net::HTTP.get(URI.parse("http://www.oreilly.ru")) response = Net::HTTP.get_response("www.oreilly.ru", "/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
Get response encoding and body
require "net/http" response = Net::HTTP.get_response("www.oreilly.ru", "/about/") response.code # => "200" response.body.size # => 21835 response["Content-type"] puts response.body[0,200]
get server information from the response
require "uri" Net::HTTP.get(URI.parse("http://www.oreilly.ru")) response = Net::HTTP.get_response("www.oreilly.ru", "/about/") puts response["Server"] puts response["SERVER"]
Get url and response
require "uri" Net::HTTP.get(URI.parse("http://www.oreilly.ru")) response = Net::HTTP.get_response("www.oreilly.ru", "/about/") puts "Success!" if response.is_a? Net::HTTPOK # Success!
Grabbing the Contents of a Web Page.rb
require "open-uri" puts open("http://www.oreilly.ru/").read(200)
Read a web page
require "net/http" h = Net::HTTP.new("www.google.ru", 80) response = h.get("/index.html", nil) if response.message == "OK" puts response.body.scan(/<img src="(.*?)"/m).uniq end
Read mysql command output
require "socket" client = TCPSocket.open("127.0.0.1", "finger") client.send("mysql\n", 0) # 0 means standard packet puts client.readlines client.close
Zip header
require "net/http" require "uri" module Net class HTTP def HTTP.get_with_headers(uri, headers=nil) uri = URI.parse(uri) if uri.respond_to? :to_str start(uri.host, uri.port) do |http| path_query = uri.path + (uri.query ? ("?" + uri.query) : "") return http.get(path_query, headers) end end end end gzipped = Net::HTTP.get_with_headers("http://www.cnn.ru/", {"Accept-Encoding" => "gzip"}) puts gzipped["Content-Encoding"] puts gzipped.body.size