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

后端 未结 2 1670
攒了一身酷
攒了一身酷 2021-02-02 14:26

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%20ur         


        
2条回答
  •  天命终不由人
    2021-02-02 14:59

    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
    # >> #
    

    And it worked.

提交回复
热议问题