gpt4 book ai didi

ruby - Net::HTTP 不发送请求

转载 作者:行者123 更新时间:2023-12-04 04:36:56 25 4
gpt4 key购买 nike

我在 Ruby 1.8.7 项目中有以下方法:

def self.ping_server
request_data = get_request_data

uri = 'another_server.our_company.com'
https = Net::HTTP.new(uri)
https.use_ssl = true
path = "/our_service"
data = request_data.to_json
response = https.post(path, data, {'Content-Type' => 'application/json'})

return response
end

每当我运行此方法时,都会出现以下超时错误:

在 128936 毫秒内完成 500 个内部服务器错误
Errno::ETIMEDOUT (Connection timed out - connect(2)):
lib/my_class.rb:51:in `ping_our_server'

我与一位有权访问日志的同事核对了 another_server.our_company.com .我的请求没有到达另一台服务器。

我应该怎么做才能使我的请求生效?

编辑 :在进一步检查中,这就是我认为正在发生的事情(但我不完全确定):我们的其他服务器将只接受 HTTPS 请求,但由于某种原因,我的请求似乎是通过 HTTP 发送的。是否需要添加一些内容以确保我的请求是通过 HTTPS 发送的?

最佳答案

根据 this website ,这是您发送 HTTPS 请求的方式:

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)
response.body
response.status
response["header-here"] # All headers are lowercase

根据 this website (从我的第一个链接链接),您还应该这样做以关闭 net/https 中的漏洞。图书馆:

To get going, you need a local CA certificates bundle, the official curl site maintains an up to date cacert.pem / ca-bundle.crt file containing all of the major certificates if you need one.

Next, after a gem install always_verify_ssl_certificates, you can be up and running with a test as simply as:


require 'always_verify_ssl_certificates'
AlwaysVerifySSLCertificates.ca_file = "/path/path/path/cacert.pem"

http= Net::HTTP.new('https://some.ssl.site', 443)
http.use_ssl = true
req = Net::HTTP::Get.new('/')
response = http.request(req)

If the site has a bad certificate an error will be raised at this point. If not, a legitimate HTTP response object will be returned.

关于ruby - Net::HTTP 不发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19600389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com