机械化获取“Errno :: ECONNRESET:通过对等方重置连接 - SSL_connect”

时间:2013-10-18 00:19:22

标签: ssl openssl ruby-on-rails-4 net-http mechanize-ruby

我无法让Mechanize加载以前工作的页面 - 它可靠地失败并显示Errno: ECONNRESET: Connection reset by peer - SSL_connect消息。关于我应该尝试什么或者我应该看的细节的任何建议? (请参阅下面的“我尝试过的内容”)。

更新1

a related S.O. post获取提示,我尝试使用Net::HTTP直接访问该网站。当我设置http.ssl_version = :TLSv1时,我得到一个重定向而不是错误(应该是这样)。所以我的问题变成:如何在ssl_version内配置Net :: HTTP的基础Mechanize参数?

...谢谢

症状:

$ rails console
>> a = Mechanize.new
=> #<Mechanize:0x007fd26789b8e0 ...>
>> p = a.get("http://sce.com")
# (...after a long pause...)
Errno::ECONNRESET: Connection reset by peer - SSL_connect
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `connect'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `block in connect'
from /sandbox/usr/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `connect'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:857:in `start'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/net-http-persistent-2.9/lib/net/http/persistent.rb:691:in `start'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/net-http-persistent-2.9/lib/net/http/persistent.rb:631:in `connection_for'
    ...
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:257:in `fetch'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:974:in `response_redirect'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:298:in `fetch'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize.rb:432:in `get'
from (irb):3

环境:

$ rake about
About your application's environment
Ruby version              2.0.0 (x86_64-darwin12.4.0)
RubyGems version          2.1.9
Rack version              1.5
Rails version             4.0.0
JavaScript Runtime        JavaScriptCore
Active Record version     4.0.0
Action Pack version       4.0.0
Action Mailer version     4.0.0
Active Support version    4.0.0
Middleware                ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007ffd423c50e0>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag
Application root          /Users/me/MyProject
Environment               development
Database adapter          postgresql
Database schema version   20131017201057

$ openssl version
OpenSSL 1.0.1e 11 Feb 2013

$ system_profiler -detailLevel mini SPSoftwareDataType

System Software Overview:

  System Version: OS X 10.8.5 (12F45)
  Kernel Version: Darwin 12.5.0
  Time since boot: 8 days 7:40

我尝试了什么:

  • 我在Firefox网络浏览器中尝试了相同的网址。它有效。
  • 我已明确设置Mechanize request_headers以完全模仿Firefox浏览器。没有变化。
  • 我已按照this S.O post中的说明更新了我的ssl/cert.pem文件(第二次)。没有变化。
  • 我尝试过不同的网站:www.pge.comwww.sdge.com。两者都有效。 www.sce.com网站有不同之处。

使用Net :: HTTP

成功

这是Net :: HTTP工作的一个例子:

$ irb
>> require 'net/https'
=> true
>> require 'uri'
=> false
>> uri = URI.parse("https://www.sce.com/")
=> #<URI::HTTPS:0x007facab8f6ba0 URL:https://www.sce.com/>
>> http = Net::HTTP.new(uri.host, uri.port)
=> #<Net::HTTP www.sce.com:443 open=false>
>> http.use_ssl = true
=> true
>> http.ssl_version = :TLSv1     # <= this line makes all the difference
=> :TLSv1
>> r = http.start { |agent| p agent.get(uri.path) }
=> #<Net::HTTPFound 302 Found readbody=true>
>> r.to_hash
=> {"content-language"=>["en-US"], "date"=>["Fri, 18 Oct 2013 01:00:07 GMT"], "location"=>["https://www.sce.com/wps/portal/home/!ut/p/b1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOIt3Q1cPbz8DTzdQwKNDTyNAw38gh0djQ0MzIAKIoEKDHAARwNC-sP1o8BK8Jjg55Gfm6pfkBthoOuoqAgAgIrzaA!!/dl4/d5/L2dBISEvZ0FBIS9nQSEh/"], "p3p"=>["CP=\"NON CUR OTPi OUR NOR UNI\""], "server"=>["IBM_HTTP_Server"], "transfer-encoding"=>["chunked"], "x-powered-by"=>["Servlet/3.0"], "set-cookie"=>["PD_STATEFUL_d55ece64-8d9a-11e2-84a1-0050560010d6=%2Fwps; Path=/", "session_www=740796608.47873.0000; path=/"]}

1 个答案:

答案 0 :(得分:11)

Mechanize邮件列表上的人们提供了答案:

agent = Mechanize.new do |a|
  a.ssl_version = :TLSv1
end