gpt4 book ai didi

ruby HTTPClient : How to use persistent connections?

转载 作者:数据小太阳 更新时间:2023-10-29 07:11:11 24 4
gpt4 key购买 nike

如何通过 HTTPClient 使用持久 HTTP 连接?发送 HTTP 请求时是否只是设置 Keep Alive 的问题?文档指出支持持久连接,但没有告诉我们如何使用它们。

最佳答案

available in Net::HTTP

如文档中所写,

Net::HTTP.start immediately creates a connection to an HTTP server which is kept open for the duration of the block. The connection will remain open for multiple requests in the block if the server indicates it supports persistent connections.

这意味着您将在 block 中执行的所有请求都将使用相同的 HTTP 连接。

文档中的例子

require 'net/http'

uri = URI('http://google.com/')

Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri.request_uri

response = http.request request # Net::HTTPResponse object
end

关于 ruby HTTPClient : How to use persistent connections?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15976775/

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