Ruby request to https - “in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)”

不羁岁月 提交于 2019-12-09 04:20:23

问题


Here is my code

domain = 'http://www.google.com'
url = URI.parse "https://graph.facebook.com/fql?q=SELECT%20url,normalized_url%20FROM%20link_stat%20WHERE%20url='#{domain}'"
req = Net::HTTP::Get.new url.path
res = Net::HTTP.start(url.host, url.port) {|http| http.request req}
puts res.body

and it gives me

/home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:153:in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:153:in `rbuf_fill'
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:134:in `readuntil'
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:144:in `readline'
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http/response.rb:39:in `read_status_line'
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http/response.rb:28:in `read_new'
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:1405:in `block in transport_request'
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:1402:in `catch'
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:1402:in `transport_request'
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:1375:in `request'
    from /home/alex/Documents/ruby_test1.rb:20:in `block in <main>'
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:851:in `start'
    from /home/alex/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:582:in `start'
    from /home/alex/Documents/ruby_test1.rb:20:in `<main>'

What did I do wrong?


回答1:


There are several oddities in your code. The main is: since you use SSL you are to aknowledge HTTP.start about with :use_ssl => url.scheme == 'https'. HTTP.Get constructor awaits for an URI, not the path. The summing up:

domain = 'http://www.google.com'
url = URI.parse("https://graph.facebook.com/fql?q=SELECT%20url,normalized_url%20FROM%20link_stat%20WHERE%20url='#{domain}'")
req = Net::HTTP::Get.new url 
res = Net::HTTP.start(url.host, url.port, 
        :use_ssl => url.scheme == 'https') {|http| http.request req}
puts res 

Gives:

#<Net::HTTPOK:0x000000027d0558>



回答2:


Accepted answer was giving me an error -

`initialize': no implicit conversion of Hash into String (TypeError)

When I was using -

Net::HTTP.start(url.host, url.port, :use_ssl => url.scheme == 'https')

My Ruby version is -

arup_ruby$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin12.0]

Then, I modified the code as below :-

require "uri"
require "net/http"

domain = 'http://www.google.com'
uri = URI.parse("https://graph.facebook.com/fql?q=SELECT%20url,normalized_url%20FROM%20link_stat%20WHERE%20url='#{domain}'")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
req = Net::HTTP::Get.new uri
res = http.start { |http| http.request req }
puts res
# >> #<Net::HTTPOK:0x000001018f8520>

And it worked.



来源:https://stackoverflow.com/questions/15726744/ruby-request-to-https-in-read-nonblock-connection-reset-by-peer-errnoec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!