如何使用 https 使用 Typhoeus::Request 对象

How to use Typhoeus::Request object using https

我正在尝试使用 Typhoeus::Request 对象发出 https 请求,但我没有让它工作。

我 运行 的代码是这样的:

url = "https://some.server.com/"
req_opts = {
 :method => :get,
 :headers => { 
      "Content-Type"=>"application/json",
      "Accept"=>"application/json"
  },
 :params=>{},
 :params_encoding=>nil,
 :timeout=>0,
 :ssl_verifypeer=>true,
 :ssl_verifyhost=>2,
 :sslcert=>nil,
 :sslkey=>nil,
 :verbose=>true
}
request = Typhoeus::Request.new(url, req_opts)
response = request.run

我得到的回复是这样的:

HTTP/1.1 302 Found
Location: https://some.server.com:443/
Date: Sat, 27 Apr 2019 02:25:05 GMT
Content-Length: 5
Content-Type: text/plain; charset=utf-8

为什么会这样?

嗯,很难知道,因为您的示例无法访问 url。但是我看到的两件事是您没有传递 ssl 证书或密钥。但 302 也表示重定向。您可以尝试跟随重定向,但您的第一个问题可能是您不需要设置 SSL 选项,为什么呢?

看看您是否尝试以下选项:

req_opts = {
 :method => :get,
  :headers => {
    "Content-Type"=>"application/json",
    "Accept"=>"application/json"
   },
    :params=>{},
    :params_encoding=>nil,
    :timeout=>0,
    :followlocation => true,
    :ssl_verifypeer=>false,
    :ssl_verifyhost=>0,
    :verbose=>true
  }

有关详细信息,请参阅以下部分

https://github.com/typhoeus/typhoeus#following-redirections https://github.com/typhoeus/typhoeus#ssl