Question

I'm unable to get Mechanize to load a page that used to work -- it's reliably failing with a Errno: ECONNRESET: Connection reset by peer - SSL_connect message. Any suggestions as to what I should try or details I should look at? (Please see "what I've tried" below...)

Update 1

Taking a hint from a related S.O. post, I tried accessing the site directly with Net::HTTP. When I set http.ssl_version = :TLSv1, I get a redirect rather than an error (as it should be). So my question becomes: how can I configure Net::HTTP's underlying ssl_version parameters from within Mechanize?

Thanks...

The symptoms:

$ 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

The environment:

$ 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

What I've tried:

  • I've tried the same URL from a Firefox web browser. It works.
  • I've explicitly set the Mechanize request_headers to exactly mimic the Firefox browser. No change.
  • I've updated my ssl/cert.pem file (a second time) as described in this S.O post. No change.
  • I've tried different sites: www.pge.com and www.sdge.com. Both work. There's something different about the www.sce.com site.

Success with Net::HTTP

Here's an example of Net::HTTP working:

$ 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=/"]}
Was it helpful?

Solution

The folks on the Mechanize mailing list kindly provided the answer:

agent = Mechanize.new do |a|
  a.ssl_version = :TLSv1
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top