我将Ruby连接到下面列出的URL时出错,即使URL存在。那是为什么?
1.9.3p194 :003 > require 'uri'
=> true
1.9.3p194 :004 > require 'net/http'
=> true
1.9.3p194 :005 > url = "https://blogs.oracle.com/ksplice/entry/introducing_redpatch"
=> "https://blogs.oracle.com/ksplice/entry/introducing_redpatch"
1.9.3p194 :006 > url_parsed = URI.parse(url)
=> #<URI::HTTPS:0x00000001939288 URL:https://blogs.oracle.com/ksplice/entry/introducing_redpatch>
1.9.3p194 :007 > response = Net::HTTP.get_response(url_parsed)
Errno::ECONNRESET: Connection reset by peer
答案 0 :(得分:2)
使用此
url = "https://blogs.oracle.com/ksplice/entry/introducing_redpatch"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
response.body
从这里开始:http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
答案 1 :(得分:2)
而不是使用Net :: HTTP,简化您的生活并使用Ruby的OpenURI。除非您需要低级别控制或低级别值的可见性,否则您会发现OpenURI足够好:
require 'open-uri'
url = "https://blogs.oracle.com/ksplice/entry/introducing_redpatch"
open(url).read.size
=> 35493