Mechanize 收到“Errno::ECONNRESET:连接由对等方重置 - SSL_connect”

2024-04-28

我无法让 Mechanize 加载曾经正常工作的页面——它确实失败了,并显示Errno: ECONNRESET: Connection reset by peer - SSL_connect信息。关于我应该尝试什么或我应该查看的细节有什么建议吗? (请参阅下面的“我尝试过的方法”...)

Update 1

从中得到暗示相关的 S.O.邮政 https://stackoverflow.com/questions/12443258/ssl-connect-error-when-accessing-shopify-api-with-rubygem,我尝试直接访问该网站Net::HTTP。当我设置http.ssl_version = :TLSv1,我得到一个重定向而不是一个错误(应该是这样)。所以我的问题变成:如何配置 Net::HTTP 的底层ssl_version内部参数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 Web 浏览器中尝试了相同的 URL。有用。
  • 我已经明确设置了机械化request_headers完全模仿 Firefox 浏览器。不用找了。
  • 我已经更新了我的ssl/cert.pem文件(第二次),如中所述这个S.O帖子 https://stackoverflow.com/questions/19150017/ssl-error-when-installing-rubygems-unable-to-pull-data-from-https-rubygems-o。不用找了。
  • 我尝试过不同的网站:www.pge.com and www.sdge.com。两者都有效。有一些不一样的地方www.sce.com site.

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=/"]}

Mechanize 邮件列表中的人们友好地提供了答案:

agent = Mechanize.new do |a|
  a.ssl_version = :TLSv1
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Mechanize 收到“Errno::ECONNRESET:连接由对等方重置 - SSL_connect” 的相关文章

随机推荐