http练习

蔡源茂 | 创建:2018-05-15 | 最后更新:2018-05-15 | 787次阅读

require 'net/https'
uri = URI('http://nine-god.com')
# params = { :limit => 10, :page => 3 }
# uri.query = URI.encode_www_form(params)

res = Net::HTTP.get_response(uri)
puts res.body
puts res.body if res.is_a?(Net::HTTPSuccess)

if res.is_a?(Net::HTTPMovedPermanently)
	# redirect_url = ""
	##遍历所有http响应头
	# res.each {|key, val| 
	# 	puts key + ' = ' + val
	# 	redirect_url = val if key == "location"
	# } 
	redirect_uri = URI(res['location'])
	res = Net::HTTP.get_response(redirect_uri) 
end
puts res.body

  
# Net::HTTP.start(uri.host, uri.port) do |http|
#   request = Net::HTTP::Get.new uri

#   response = http.request request # Net::HTTPResponse object
#   puts response.body
#   puts response.header
# end


共收到 1 条回复


风雨彩虹: | #7 | 2019-02-17

1


评论:

忘记密码?
其他账号登录: QQ登录

回到顶部
到达底部