2016-06-29 109 views
3

偶爾,當我訪問有HTTPartyMechanize一個網站,我得到這個錯誤:「主機名不匹配服務器證書」,在機械化的錯誤和HTTParty

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

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

堆棧跟蹤的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' 
+0

您應該創建適當的名稱服務器證書。爲此,請參閱[如何使用openssl創建自簽名證書?](http://stackoverflow.com/a/27931596/608639) – jww

回答

4

對於機械化這應該設置驗證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 
+0

非常不好的建議;請參閱[世界上最危險的代碼:在非瀏覽器軟件中驗證SSL證書](http://crypto.stanford.edu/~dabo/pubs/abstracts/ssl-client-bugs.html)。也許OP應該使用正確的名字創建一個證書。爲此,請參閱[如何使用openssl創建自簽名證書?](http://stackoverflow.com/a/27931596/608639) – jww

+0

@jww是的,當然。但OP要求解決Mechanize和HTTParty的解決方法。我提供瞭解決方法。這些解決方法不是超級清晰的額外解決方案。有時候問題出現在服務器端(而不是客戶端),你只能做解決方法。這不是downvote的原因,因爲我提供了op所要求的。我希望你明白我的觀點。 OP想要設置VERIFY_NONE。我已經回答了。所以請刪除downvote。 –

+0

@jww當您不擁有服務器時,您無法創建證書。看起來OP是颳了很多,這就是我理解OP的問題。對我而言,您對創建證書的評論與OPs問題無關。下調也是,問題是:我不擁有服務器。存在SSL問題。我想獲得頁面!即使每個瀏覽器都允許您選擇「訪問網站!」當證書有問題時。你只是得到了警告,但仍然有可能得到錯誤的證書頁面。 –

相關問題