开发者

Ruby on Rails Net::HTTP use_ssl throws 'undefined method'

Here's my code:

sock = Net::HTTP.new(url.host, u开发者_StackOverflowrl.port)
sock.use_ssl = true
response = sock.start {|http| http.request(req)}

here's the error:

undefined method `use_ssl=' for #<Net::HTTP www.paypal.com:443 open=false>

google is getting me nothing!

thanks.


Require 'net/https' in addition to 'net/http'. Then use_ssl= will be defined.

require 'net/http'
require 'net/https'
connection = Net::HTTP::new 'www.example.com'
connection.use_ssl = true


That is because the function is sock.use_ssl? and it returns a boolean, it is not a setter method.

It also appears to always return false. It is overridden in the Net::HTTPS package, which is probably what you should be using if you want to do any SSL stuff.

Here is the ruby doc


Have a look at the following snippet:

Net::HTTP.start(url.host, url.port) do |http|
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.request(req)
end

You need to do the set for the use_ssl on the http object not on the request object. I struggled with this and found my answer here: https://github.com/nicksieger/multipart-post/issues/18


If you get " `require': no such file to load -- net/https (LoadError)", then that's because you don't have it installed.

If you haven't already got it, you might need ssl libraries on your server. For example, on Debian/Ubuntu:

aptitude --assume-yes install libssl-dev

If you're unlucky, you might need to rebuild Ruby, with SSL support. If building manually, add --with-openssl to the configure options.

Then you need the gem:

gem install  openssl-nonblock --no-ri --no-rdoc

And now, at least in my experience, it should work...


Here is how you can do a https get request:

require "net/https"
require "uri"

uri = URI.parse("https://secure.com/")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Get.new(uri.request_uri)

response = http.request(request)

source: http://www.rubyinside.com/nethttp-cheat-sheet-2940.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜