Ruby 检查 HTTP 请求的返回代码

2015-03-12 0 225
Ruby 检查 HTTP 请求的返回代码
require 'net/http'         # The library we need
host = 'www.example.com'   # The web server
path = '/index.html'       # The file we want

http = Net::HTTP.new(host)      # Create a connection
headers, body = http.get(path)  # Request the file
if headers.code == "200"        # Check the status code
                                # NOTE: code is not a number!
  print body                    # Print body if we got it
else                            # Otherwise
  puts "#{headers.code} #{headers.message}" # Display error message
end

遇见资源网 ruby Ruby 检查 HTTP 请求的返回代码 http://www.ox520.com/16269.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务