Ruby on rails 在本地主机上执行HTTPS请求时遇到问题
我正在使用RubyonRails3,并尝试在localhost上使用HTTPS来实现API。我所做的一切都受到了启发 在经历了很多麻烦之后,我能够生成一个证书,设置Apache并编写一些代码。看来我快要找到解决办法了。这就是我到目前为止所做的:Ruby on rails 在本地主机上执行HTTPS请求时遇到问题,ruby-on-rails,ruby,ruby-on-rails-3,https,request,Ruby On Rails,Ruby,Ruby On Rails 3,Https,Request,我正在使用RubyonRails3,并尝试在localhost上使用HTTPS来实现API。我所做的一切都受到了启发 在经历了很多麻烦之后,我能够生成一个证书,设置Apache并编写一些代码。看来我快要找到解决办法了。这就是我到目前为止所做的: require 'uri' require 'net/https' ... host = "https://<my_site_name>.com" path = "/users/1.json" uri = URI
require 'uri'
require 'net/https'
...
host = "https://<my_site_name>.com"
path = "/users/1.json"
uri = URI.parse("#{host}#{path}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.ca_file = File.join(File.dirname("/private/etc/apache2/ssl/wildcard.certificate/ca.db.certs/"), "01.pem")
http.start do
puts http.get("#{host}#{path}")
end
@test_response = http
这是我第一次做这种事情,而且我不擅长为它实现代码,所以我有一些问题: (1)
uri=uri.parse(“{host}{path}”)
是什么意思?什么是uri
范围
(2) 为什么Paul Dix使用:
http.start do
puts http.get("#{host}#{path}")
end
http.start do
puts http.get("#{host}#{path}")
end
而不是
@test_response = http.get("#{host}#{path}")
(3) 使用Paul Dix的版本,我如何读取@test\u响应
值?这样做正确吗
(4) 为什么我使用http.verify\u mode=OpenSSL::SSL::verify\u PEER
而不是http.verify\u mode=OpenSSL::SSL::verify\u NONE
时会出现以下错误:
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
(5) 在Ruby on Rails应用程序中,我应该将.pem
证书放在哪里
(6) 上述代码正确且可取吗?如果没有,我可以改进什么?
我发现的另一件事是如果我使用
http.start do
@test_response = http.get("#{host}#{path}")
end
@test\u response
返回
--- !ruby/object:Net::HTTPOK
body: ...
body_exist: true
code: "200"
header:
...
status:
- "200"
transfer-encoding:
- chunked
content-type:
- application/json; charset=utf-8
http_version: "1.1"
message: OK
read: true
--- !ruby/object:Net::HTTPOK
body: ...
body_exist: true
code: "200"
header:
...
status:
- "200"
connection: # This is the differce from the previous result
- close
transfer-encoding:
- chunked
content-type:
- application/json; charset=utf-8
http_version: "1.1"
message: OK
read: true
如果我简单地使用
@internal_test2 = http.get("#{host}#{path}")
@test\u response
返回
--- !ruby/object:Net::HTTPOK
body: ...
body_exist: true
code: "200"
header:
...
status:
- "200"
transfer-encoding:
- chunked
content-type:
- application/json; charset=utf-8
http_version: "1.1"
message: OK
read: true
--- !ruby/object:Net::HTTPOK
body: ...
body_exist: true
code: "200"
header:
...
status:
- "200"
connection: # This is the differce from the previous result
- close
transfer-encoding:
- chunked
content-type:
- application/json; charset=utf-8
http_version: "1.1"
message: OK
read: true
(7) 这是什么意思?我不确定您在这里想要完成什么(看起来您正在尝试连接并阅读HTTPS请求),但我会尽力回答您的问题 (1)
uri=uri.parse(“{host}{path}”)
是什么意思?什么是uri
范围
根据它的语法,URI.parse
获取一个字符串(在本例中,“#{host}{path}”
并从中返回URI的一个子类。重点是获取一个URI对象而不是字符串。(URI
是一个Ruby模块。)
(2) 为什么Paul Dix使用:
http.start do
puts http.get("#{host}#{path}")
end
http.start do
puts http.get("#{host}#{path}")
end
再次,请参见。通过使用块,他打开了TCP连接和HTTP会话,这两个会话在块执行完毕后自动关闭
(3) 使用Paul Dix的版本,我如何读取@test\u response
值?这样做对吗@test\u response=http
我不知道你在这里是什么意思。如果你做了@test\u response=http
,那么@test\u response
与http
具有所有相同的值。你可以从中读取响应正文等
(4) 为什么我使用http.verify\u mode=OpenSSL::SSL::verify\u PEER
而不是http.verify\u mode=OpenSSL::SSL::verify\u NONE
时会出现以下错误:
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
这是因为Ruby找不到证书颁发机构证书。有关可能修复此问题的更多信息,请参阅
(5) 在Ruby on Rails应用程序中,我应该将.pem
证书放在哪里
我相信这是特定于您用来为Rails应用程序服务的服务器的,而不是Rails应用程序本身
(6) 这是什么意思
我相信这只是表明HTTP会话已关闭。请参见#2。(4)如果我在localhost中安装“curl ca bundle”,当我部署应用程序时,我必须使用\install,或者这仅仅是在localhost中工作的一种黑客行为?如果您想从生产应用程序的HTTPS连接中读取,并且想验证证书,您还需要在服务器上执行此操作。