Mechanize和HTTParty中的“主机名与服务器证书不匹配”错误

时间:2016-06-29 09:05:53

标签: ruby-on-rails ruby mechanize httparty

当我访问包含HTTPartyMechanize的网站时,我收到此错误:

hostname "www.example.com" does not match the server certificate

如果您使用open方法,我可以看到there is a workaround,但我不确定如何利用上述宝石。

Mechanize的堆栈跟踪:

agent = Mechanize.new
agent.read_timeout              = 180
agent.open_timeout              = 180
agent.user_agent_alias          = 'Mac Safari'
agent.redirect_ok               = :all
agent.follow_meta_refresh       = :anywhere
agent.follow_meta_refresh_self  = true
agent.get("https://some-domain.com")
/home/me/.rbenv/versions/2.2.2/lib/ruby/2.2.0/openssl/ssl.rb:232:in `post_connection_check'
/home/me/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/http.rb:925:in `connect'
/home/me/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
/home/me/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/http.rb:858:in `start'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:700:in `start'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:965:in `reset'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:628:in `connection_for'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:994:in `request'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mechanize-2.7.4/lib/mechanize/http/agent.rb:267:in `fetch'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mechanize-2.7.4/lib/mechanize.rb:464:in `get'

HTTParty的堆栈跟踪:

HTTParty.get("https://some-domain.com")
/home/me/.rbenv/versions/2.2.2/lib/ruby/2.2.0/openssl/ssl.rb:232:in `post_connection_check'
/home/me/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/http.rb:925:in `connect'
/home/me/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
/home/me/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/http.rb:852:in `start'
/home/me/.rbenv/versions/2.2.2/lib/ruby/2.2.0/net/http.rb:1375:in `request'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/httparty-0.13.7/lib/httparty/request.rb:117:in `perform'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/httparty-0.13.7/lib/httparty.rb:545:in `perform_request'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/httparty-0.13.7/lib/httparty.rb:476:in `get'

1 个答案:

答案 0 :(得分:4)

对于Mechanize,这应该将SSL验证为无

agent = Mechanize.new
agent.verify_mode = OpenSSL::SSL::VERIFY_NONE

对于HTTParty,有verify:选项 看到这个问题How do I make HTTParty ignore SSL?

如果你想一般地设置它,请使用这个肮脏的技巧:

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE