Ruby open-uri redirect forbidden

前端 未结 3 1111
耶瑟儿~
耶瑟儿~ 2020-12-15 15:56

I have this simple html parser(for learning purposes) that I have been working on.:

require \'open-uri\'
puts \"Enter URL to parse HTML: \"
url = gets.chomp
         


        
相关标签:
3条回答
  • 2020-12-15 16:37

    Ruby 2.4 fixed upgrade redirects (from http -> https) in open-uri, so now:

    RUBY_VERSION
    => "2.4.2"
    
    require 'open-uri'
    => true
    
    open('http://twitter.com')
    => #<Tempfile:/tmp/open-uri20170926-24254-1kflwxq>
    

    Source: http://blog.bigbinary.com/2017/03/02/open-uri-in-ruby-2-4-allows-http-to-https-redirection.html

    0 讨论(0)
  • 2020-12-15 16:42

    Have a look at the open_uri_redirections gem.

    It patches Ruby's OpenURI to allow redirections from HTTP to HTTPS or the other way around.

    0 讨论(0)
  • 2020-12-15 17:00

    You can also catch the exception and then try it again with 'https' url.

    url = "http://classic.ona.io/api/v1/files/3538545?filename=gringgo/attachments/1485229166168.jpg"
    
    uri = URI.parse(url)
    tries = 3
    
    begin
      uri.open(redirect: false)
    rescue OpenURI::HTTPRedirect => redirect
      uri = redirect.uri # assigned from the "Location" response header
      retry if (tries -= 1) > 0
      raise
    end
    

    Source: https://twin.github.io/improving-open-uri/

    0 讨论(0)
提交回复
热议问题