简体   繁体   English

Rails http GET请求,没有SSL验证和基本身份验证

[英]Rails http GET request with no ssl verification and basic auth

I am trying to make a http get request as specified in the title. 我正在尝试发出标题中指定的http get请求。 What I've written: 我写的是:

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

The request is sent where I want it to be sent and I receive an Unauthorized response (as expected, because I did not specify the basic auth). 该请求被发送到我希望发送的位置,并且收到未授权的响应(如预期的那样,因为我未指定基本身份验证)。

I've tried 我试过了

http.basic_auth 'user', 'pass'

But there's no such method for the type of my http variable. 但是我的http变量的类型没有这种方法。 How can I add the auth details? 如何添加身份验证详细信息?

Update: I tried to use something like here RUBY - SSL, Basic Auth, and POST , but after replacing Post with Get, I cannot use the 'use_ssl', nor 'verify_mode' attributes (I get no such attribute error). 更新:我试图使用类似RUBY的方法-SSL,基本身份验证和POST ,但是用Get替换Post以后,我不能使用'use_ssl'或'verify_mode'属性(我没有得到这样的属性错误)。

Update 2: I figured out that I can set the 'use_ssl' attribute on a Net::HTTP object and the 'basic_auth' on a Net::HTTP::Get object. 更新2:我发现可以在Net :: HTTP对象上设置'use_ssl'属性,而在Net :: HTTP :: Get对象上设置'basic_auth'。 Now the question is how can I make them work together? 现在的问题是,如何使它们一起工作?

Well, I ended up finding an answer. 好吧,我最终找到了答案。 Maybe this will help others: 也许这会帮助其他人:

url = URI.parse('https://myaddress.com')
req = Net::HTTP::Get.new(url.path)
req.basic_auth 'user', 'pass'

sock = Net::HTTP.new(url.host, url.port)
sock.use_ssl = true
sock.verify_mode = OpenSSL::SSL::VERIFY_NONE

resp = sock.start {|http| http.request(req) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM