使用Mechanize对象获取`证书验证失败(OpenSSL :: SSL :: SSLError)错误

时间:2013-01-23 08:02:55

标签: ruby ssl windows-xp ssl-certificate mechanize

以下代码获得certificate verify failed (OpenSSL::SSL::SSLError)

CODE

require 'mechanize'

agent = Mechanize.new

#{|a| a.ssl_version, a.verify_mode = 'SSLv3', OpenSSL::SSL::VERIFY_NONE}

agent.set_proxy('3.209.30.25', 80)
agent.ssl_version='SSLv3'
agent.verify_mode= OpenSSL::SSL::VERIFY_PEER

page = agent.get "https://gegsltraining.aravo.com/"

错误

C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net/http/persist
ent/ssl_reuse.rb:70:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 rea
d server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)

修改

我刚刚修改了代码如下:

require 'mechanize'
agent = Mechanize.new
#{|a| a.ssl_version, a.verify_mode = 'SSLv3', OpenSSL::SSL::VERIFY_NONE}
agent.set_proxy('3.209.30.25', 80)
agent.ssl_version='SSLv3'
cert_store = OpenSSL::X509::Store.new
cert_store.add_file 'E:\WIPData\Ruby\Scripts\cacert.pem'
agent.cert_store = cert_store
agent.verify_mode= OpenSSL::SSL::VERIFY_PEER (tried OpenSSL::SSL::VERIFY_NONE none of them going good)

新错误

E:\WIPData\Ruby\Scripts>mechanizev1.rb
C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2632:in `error!': 500 "Internal Server Err
or" (Net::HTTPFatalError)

如何成功完成认证验证?帮助我。

1 个答案:

答案 0 :(得分:0)

只需将您的mechanize agent object.http.ca_file设置为指向此文件。

agent.agent.http.ca_file = '/etc/ssl/certs/curl-ca-bundle.crt'

除此之外,还要将curl-ca-bundle.crt保存在app lib目录中 然后设置

agent.agent.http.ca_file = 'lib/curl-ca-bundle.crt'