Why does accessing a SSL site with Mechanize on Windows fail, but on Mac work?

后端 未结 2 454
终归单人心
终归单人心 2020-12-13 19:45

This is the code I\'m using to connect to the SSL site.

require \'mechanize\'
a = Mechanize.new
page = a.get \'https://site.com\'

I\"m usin

相关标签:
2条回答
  • 2020-12-13 20:11

    Luis' answer looks fine but more generally:

    OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
    
    0 讨论(0)
  • 2020-12-13 20:17

    The version of OpenSSL (the library used to establish secure connections with Net::HTTPS) is not able to properly find the certificate chain in your computer.

    To our bad, OpenSSL was never able to use the Windows installed cert storage to validate remote servers so is failing because of that.

    From your example, you can do:

    a.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    

    To avoid the verification, however that is far from ideal (due clear security issues)

    I recommend you download some cert bundles (like the ones from curl):

    http://curl.haxx.se/ca

    And modify your code to something like this:

    require "rbconfig"
    require "mechanize"
    
    a = Mechanize.new
    
    # conditionally set certificate under Windows
    # http://blog.emptyway.com/2009/11/03/proper-way-to-detect-windows-platform-in-ruby/
    if RbConfig::CONFIG["host_os"] =~ /mingw|mswin/
      # http://curl.haxx.se/ca
      ca_path = File.expand_path "~/Tools/bin/curl-ca-bundle.crt"
    
      a.agent.http.ca_file = ca_path
    end
    
    page = a.get "https://github.com/"
    

    That seems to work, Ruby 1.9.3-p0 (i386-mingw32), Windows 7 x64 and mechanize 2.1.pre.1

    Hope that helps.

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