class Net::HTTP
类 Net::HTTP 提供了一个丰富的库,实现了客户端-服务器模型中的客户端,该模型使用 HTTP 请求-响应协议。有关 HTTP 的信息,请参阅
关于示例
此处的示例假设已加载 net/http(这也将加载 uri)
require 'net/http'
这里的许多代码示例都使用了这些示例网站
一些示例还假定存在这些变量
uri = URI('https://jsonplaceholder.typicode.com/') uri.freeze # Examples may not modify. hostname = uri.hostname # => "jsonplaceholder.typicode.com" path = uri.path # => "/" port = uri.port # => 443
因此,示例请求可以这样编写
Net::HTTP.get(uri) Net::HTTP.get(hostname, '/index.html') Net::HTTP.start(hostname) do |http| http.get('/todos/1') http.get('/todos/2') end
需要修改 URI 的示例首先复制 uri,然后修改副本
_uri = uri.dup _uri.path = '/todos/1'
策略
-
如果您只需要发送少量 GET 请求,请考虑使用
OpenURI。 -
如果您只需要发送少量所有类型的请求,请考虑使用此类中的各种单例便捷方法。以下每个方法都会自动启动和完成一个 会话,该会话发送单个请求
# Return string response body. Net::HTTP.get(hostname, path) Net::HTTP.get(uri) # Write string response body to $stdout. Net::HTTP.get_print(hostname, path) Net::HTTP.get_print(uri) # Return response as Net::HTTPResponse object. Net::HTTP.get_response(hostname, path) Net::HTTP.get_response(uri) data = '{"title": "foo", "body": "bar", "userId": 1}' Net::HTTP.post(uri, data) params = {title: 'foo', body: 'bar', userId: 1} Net::HTTP.post_form(uri, params) data = '{"title": "foo", "body": "bar", "userId": 1}' Net::HTTP.put(uri, data)
-
如果性能很重要,请考虑使用会话,它们可以降低请求开销。此 会话 包含多个对 HTTP 方法 和 WebDAV 方法的请求
Net::HTTP.start(hostname) do |http| # Session started automatically before block execution. http.get(path) http.head(path) body = 'Some text' http.post(path, body) # Can also have a block. http.put(path, body) http.delete(path) http.options(path) http.trace(path) http.patch(path, body) # Can also have a block. http.copy(path) http.lock(path, body) http.mkcol(path, body) http.move(path) http.propfind(path, body) http.proppatch(path, body) http.unlock(path, body) # Session finished automatically at block exit. end
上面引用的方法是便捷方法,它们通过少数参数允许对请求进行最小的控制。为了获得更大的控制,请考虑使用 请求对象。
URI
在互联网上,URI(统一资源标识符)是标识特定资源的字符串。它由以下部分或全部组成:方案、主机名、路径、查询和片段;请参阅 URI 语法。
Ruby 的 URI::Generic 对象代表互联网 URI。它提供了方法 scheme、hostname、path、query 和 fragment 等。
方案
互联网 URI 具有一个 方案。
Net::HTTP 支持的两种方案是 'https' 和 'http'
uri.scheme # => "https" URI('http://example.com').scheme # => "http"
主机名
主机名标识可以发送请求的服务器(主机)
hostname = uri.hostname # => "jsonplaceholder.typicode.com" Net::HTTP.start(hostname) do |http| # Some HTTP stuff. end
路径
特定于主机的路径标识主机上的资源
_uri = uri.dup _uri.path = '/todos/1' hostname = _uri.hostname path = _uri.path Net::HTTP.get(hostname, path)
查询
特定于主机的查询将名称/值对添加到 URI
_uri = uri.dup params = {userId: 1, completed: false} _uri.query = URI.encode_www_form(params) _uri # => #<URI::HTTPS https://jsonplaceholder.typicode.com?userId=1&completed=false> Net::HTTP.get(_uri)
片段
Net::HTTP 中 URI 片段 没有影响;无论是否包含片段,都会返回相同的数据。
请求头
请求头可用于将额外信息传递给主机,类似于方法调用中传递的参数;每个头都是一个名称/值对。
此类的每个将请求发送到主机的 Net::HTTP 方法都有一个可选参数 headers,其中标头表示为字段名称/值对的哈希
headers = {Accept: 'application/json', Connection: 'Keep-Alive'} Net::HTTP.get(uri, headers)
请参阅 请求字段 中标准请求字段和常用请求字段的列表。主机也可能接受其他自定义字段。
HTTP 会话
会话是服务器(主机)和客户端之间的一种连接,它
-
由实例方法
Net::HTTP#start启动。 -
可以包含任意数量的请求。
-
由实例方法
Net::HTTP#finish结束。
请参阅 策略 中的示例会话。
使用 Net::HTTP.start 进行会话
如果您需要向单个主机(和端口)发送大量请求,请考虑使用带有块的单例方法 Net::HTTP.start;该方法通过以下方式自动处理会话:
在块中,您可以使用这些实例方法,每种方法都发送单个请求
-
-
get,request_get: GET。 -
head,request_head: HEAD。 -
post,request_post: POST。 -
delete: DELETE。 -
options: OPTIONS。 -
trace: TRACE。 -
patch: PATCH。
-
使用 Net::HTTP.start 和 Net::HTTP.finish 进行会话
您可以使用 start 和 finish 方法手动管理会话
http = Net::HTTP.new(hostname) http.start http.get('/todos/1') http.get('/todos/2') http.delete('/posts/1') http.finish # Needed to free resources.
单请求会话
某些便捷方法会自动处理会话,通过
-
创建 HTTP 对象
-
启动会话。
-
发送单个请求。
-
结束会话。
-
销毁对象。
发送 GET 请求的此类方法
-
::get: 返回字符串响应正文。 -
::get_print: 将字符串响应正文写入 $stdout。 -
::get_response: 返回一个Net::HTTPResponse对象。
发送 POST 请求的此类方法
-
::post: 将数据 POST 到主机。 -
::post_form: 将表单数据 POST 到主机。
HTTP 请求和响应
上面许多方法是便捷方法,每种方法都发送一个请求并返回一个字符串,而不直接使用 Net::HTTPRequest 和 Net::HTTPResponse 对象。
但是,您可以直接创建请求对象、发送请求并检索响应对象;请参阅
处理重定向
每个返回的响应都是 Net::HTTPResponse 的子类实例。请参阅 响应类层次结构。
特别是,类 Net::HTTPRedirection 是所有重定向类的父类。这允许您创建一个 case 语句来正确处理重定向
def fetch(uri, limit = 10) # You should choose a better exception. raise ArgumentError, 'Too many HTTP redirects' if limit == 0 res = Net::HTTP.get_response(URI(uri)) case res when Net::HTTPSuccess # Any success class. res when Net::HTTPRedirection # Any redirection class. location = res['Location'] warn "Redirected to #{location}" fetch(location, limit - 1) else # Any other class. res.value end end fetch(uri)
基本身份验证
基本身份验证根据 RFC2617 执行
req = Net::HTTP::Get.new(uri) req.basic_auth('user', 'pass') res = Net::HTTP.start(hostname) do |http| http.request(req) end
流式传输响应正文
默认情况下,Net::HTTP 会将整个响应读入内存。如果您正在处理大文件或希望实现进度条,则可以改将正文直接流式传输到 IO。
Net::HTTP.start(hostname) do |http| req = Net::HTTP::Get.new(uri) http.request(req) do |res| open('t.tmp', 'w') do |f| res.read_body do |chunk| f.write chunk end end end end
HTTPS
通过 Net::HTTP#use_ssl= 为 HTTP 连接启用 HTTPS
Net::HTTP.start(hostname, :use_ssl => true) do |http| req = Net::HTTP::Get.new(uri) res = http.request(req) end
或者,如果您只想发出 GET 请求,可以传入一个具有 HTTPS URL 的 URI 对象。Net::HTTP 会自动开启 TLS 验证,如果 URI 对象具有 'https' URI 方案
uri # => #<URI::HTTPS https://jsonplaceholder.typicode.com/> Net::HTTP.get(uri)
代理服务器
HTTP 对象可以有一个 代理服务器。
您可以使用方法 Net::HTTP.new 或方法 Net::HTTP.start 创建带有代理服务器的 HTTP 对象。
代理可以通过参数 p_addr 或环境变量 'http_proxy' 定义。
使用字符串参数 p_addr 的代理
当参数 p_addr 是字符串主机名时,返回的 http 将给定的主机作为其代理
http = Net::HTTP.new(hostname, nil, 'proxy.example') http.proxy? # => true http.proxy_from_env? # => false http.proxy_address # => "proxy.example" # These use default values. http.proxy_port # => 80 http.proxy_user # => nil http.proxy_pass # => nil
还可以指定代理的端口、用户名和密码
http = Net::HTTP.new(hostname, nil, 'proxy.example', 8000, 'pname', 'ppass') # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.proxy? # => true http.proxy_from_env? # => false http.proxy_address # => "proxy.example" http.proxy_port # => 8000 http.proxy_user # => "pname" http.proxy_pass # => "ppass"
使用 ‘ENV['http_proxy']’ 的代理
当环境变量 'http_proxy' 设置为 URI 字符串时,返回的 http 将在该 URI 处的服务器作为其代理;请注意,URI 字符串必须具有协议,例如 'http' 或 'https'
ENV['http_proxy'] = 'http://example.com' http = Net::HTTP.new(hostname) http.proxy? # => true http.proxy_from_env? # => true http.proxy_address # => "example.com" # These use default values. http.proxy_port # => 80 http.proxy_user # => nil http.proxy_pass # => nil
URI 字符串可以包含代理用户名、密码和端口号
ENV['http_proxy'] = 'http://pname:ppass@example.com:8000' http = Net::HTTP.new(hostname) http.proxy? # => true http.proxy_from_env? # => true http.proxy_address # => "example.com" http.proxy_port # => 8000 http.proxy_user # => "pname" http.proxy_pass # => "ppass"
过滤代理
使用方法 Net::HTTP.new(而不是 Net::HTTP.start),您可以使用参数 p_no_proxy 来过滤代理
-
拒绝特定地址
http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example') http.proxy_address # => nil
-
拒绝特定域或子域
http = Net::HTTP.new('example.com', nil, 'my.proxy.example', 8000, 'pname', 'ppass', 'proxy.example') http.proxy_address # => nil
-
拒绝特定地址和端口组合
http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:1234') http.proxy_address # => "proxy.example" http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:8000') http.proxy_address # => nil
-
拒绝以上类型列表,使用逗号分隔
http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000') http.proxy_address # => nil http = Net::HTTP.new('example.com', nil, 'my.proxy', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000') http.proxy_address # => nil
压缩和解压缩
Net::HTTP 在发送请求正文之前不会对其进行压缩。
默认情况下,Net::HTTP 会向新的 请求对象 添加 'Accept-Encoding' 头
Net::HTTP::Get.new(uri)['Accept-Encoding'] # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
这会请求服务器对响应正文进行 zip 编码(如果存在);服务器不一定需要这样做。
如果响应具有 'Content-Range' 头,Net::HTTP 不会自动解压缩响应正文。
否则,解压缩(或不解压缩)取决于 Content-Encoding 头的值
-
'deflate','gzip', 或'x-gzip': 解压缩正文并删除头。 -
'none'或'identity': 不解压缩正文,但删除头。 -
任何其他值: 保持正文和头不变。
这里有什么
首先,其他内容。类 Net::HTTP
-
继承自 class Object。
这是一个按类别分类的方法和属性的摘要。
Net::HTTP 对象
会话
-
::start: 在新的 Net::HTTP 对象中开始一个新会话。
-
#started?: 返回是否处于会话中。
-
#finish: 结束一个活动会话。
-
#start: 在现有的 Net::HTTP 对象 (
self) 中开始一个新会话。
连接
-
:continue_timeout: 返回继续超时。
-
#continue_timeout=: 设置继续超时秒数。
-
:keep_alive_timeout: 返回保持连接超时。
-
:keep_alive_timeout=: 设置保持连接超时。
-
:max_retries: 返回最大重试次数。
-
#max_retries=: 设置最大重试次数。
-
:open_timeout: 返回打开超时。
-
:open_timeout=: 设置打开超时。
-
:read_timeout: 返回打开超时。
-
:read_timeout=: 设置读取超时。
-
:ssl_timeout: 返回 SSL 超时。
-
:ssl_timeout=: 设置 SSL 超时。
-
:write_timeout: 返回写入超时。
-
write_timeout=: 设置写入超时。
请求
-
::get: 发送 GET 请求并返回字符串响应正文。
-
::get_print: 发送 GET 请求并将字符串响应正文写入 $stdout。
-
::get_response: 发送 GET 请求并返回响应对象。
-
::post_form: 发送带有表单数据的 POST 请求并返回响应对象。
-
::post: 发送带有数据的 POST 请求并返回响应对象。
-
::put: 发送带有数据的 PUT 请求并返回响应对象。
-
#copy: 发送 COPY 请求并返回响应对象。
-
#delete: 发送 DELETE 请求并返回响应对象。
-
#get: 发送 GET 请求并返回响应对象。
-
#head: 发送 HEAD 请求并返回响应对象。
-
#lock: 发送 LOCK 请求并返回响应对象。
-
#mkcol: 发送 MKCOL 请求并返回响应对象。
-
#move: 发送 MOVE 请求并返回响应对象。
-
#options: 发送 OPTIONS 请求并返回响应对象。
-
#patch: 发送 PATCH 请求并返回响应对象。
-
#post: 发送 POST 请求并返回响应对象。
-
#propfind: 发送 PROPFIND 请求并返回响应对象。
-
#proppatch: 发送 PROPPATCH 请求并返回响应对象。
-
#put: 发送 PUT 请求并返回响应对象。
-
#request: 发送请求并返回响应对象。
-
#request_get: 发送 GET 请求并形成一个响应对象;如果提供了块,则使用该对象调用块,否则返回该对象。
-
#request_head: 发送 HEAD 请求并形成一个响应对象;如果提供了块,则使用该对象调用块,否则返回该对象。
-
#request_post: 发送 POST 请求并形成一个响应对象;如果提供了块,则使用该对象调用块,否则返回该对象。
-
#send_request: 发送请求并返回响应对象。
-
#trace: 发送 TRACE 请求并返回响应对象。
-
#unlock: 发送 UNLOCK 请求并返回响应对象。
响应
-
:close_on_empty_response: 返回是否在响应为空时关闭连接。
-
:close_on_empty_response=: 设置是否在响应为空时关闭连接。
-
:ignore_eof: 返回是否在读取具有
Content-Length头信息的响应正文时忽略文件尾。 -
:ignore_eof=: 设置是否在读取具有
Content-Length头信息的响应正文时忽略文件尾。 -
:response_body_encoding: 返回用于响应正文的编码。
-
#response_body_encoding=: 设置响应正文编码。
代理
-
:proxy_address: 返回代理地址,如果没有则返回
nil。 -
:proxy_address=: 设置代理地址。
-
::proxy_class?: 返回
self是否为代理类。 -
#proxy?: 返回
self是否具有代理。 -
#proxy_address: 返回代理地址。
-
#proxy_from_env?: 返回代理是否取自环境变量。
-
:proxy_from_env=: 设置代理是否应取自环境变量。
-
:proxy_pass: 返回代理密码,如果没有则返回
nil。 -
:proxy_pass=: 设置代理密码。
-
:proxy_port: 返回代理主机端口号,如果没有则返回
nil。 -
:proxy_port=: 设置代理端口号。
-
#proxy_user: 返回代理用户名,如果没有则返回
nil。 -
:proxy_user=: 设置代理用户。
Security
-
:ca_file: 返回 CA 证书文件的路径。
-
:ca_file=: 设置 CA 证书文件的路径。
-
:ca_path: 返回包含证书文件的 CA 目录的路径。
-
:ca_path=: 设置包含证书文件的 CA 目录的路径。
-
:cert: 返回将用于客户端证书的
OpenSSL::X509::Certificate对象。 -
:cert=: 设置将用于客户端证书的
OpenSSL::X509::Certificate对象。 -
:cert_store: 返回将用于验证对等证书的 X509::Store 对象。
-
:cert_store=: 设置将用于验证对等证书的 X509::Store 对象。
-
:ciphers: 返回可用的 SSL 密码。
-
:ciphers=: 设置可用的 SSL 密码。
-
:extra_chain_cert: 返回将添加到证书链的额外 X509 证书。
-
:extra_chain_cert=: 设置将添加到证书链的额外 X509 证书。
-
:key: 返回
OpenSSL::PKey::RSA或OpenSSL::PKey::DSA对象。 -
:key=: 设置
OpenSSL::PKey::RSA或OpenSSL::PKey::DSA对象。 -
:max_version: 返回最大 SSL 版本。
-
:max_version=: 设置最大 SSL 版本。
-
:min_version: 返回最小 SSL 版本。
-
:min_version=: 设置最小 SSL 版本。
-
#peer_cert: 返回会话套接字对等方的 X509 证书链。
-
:ssl_version: 返回 SSL 版本。
-
:ssl_version=: 设置 SSL 版本。
-
#use_ssl=: 设置新会话是否使用传输层安全。
-
#use_ssl?: 返回
self是否使用 SSL。 -
:verify_callback: 返回服务器证书验证的回调。
-
:verify_callback=: 设置服务器证书验证的回调。
-
:verify_depth: 返回证书链验证的最大深度。
-
:verify_depth=: 设置证书链验证的最大深度。
-
:verify_hostname: 返回在 SSL/TLS 会话开始时用于服务器证书验证的标志。
-
:verify_hostname=: 设置在 SSL/TLS 会话开始时用于服务器证书验证的标志。
-
:verify_mode: 返回在 SSL/TLS 会话开始时用于服务器证书验证的标志。
-
:verify_mode=: 设置在 SSL/TLS 会话开始时用于服务器证书验证的标志。
地址和端口
-
:address: 返回字符串主机名或主机 IP。
-
::default_port: 返回整数 80,用于
HTTP请求的默认端口。 -
::http_default_port: 返回整数 80,用于
HTTP请求的默认端口。 -
::https_default_port: 返回整数 443,用于 HTTPS 请求的默认端口。
-
#ipaddr: 返回连接的 IP 地址。
-
#ipaddr=: 设置连接的 IP 地址。
-
:local_host: 返回用于建立连接的字符串本地主机。
-
:local_host=: 设置用于建立连接的字符串本地主机。
-
:local_port: 返回用于建立连接的整数本地端口。
-
:local_port=: 设置用于建立连接的整数本地端口。
-
:port: 返回整数端口号。
HTTP 版本
-
::version_1_2? (别名为 ::version_1_2): 返回 true;保留以兼容。
调试
-
#set_debug_output: 设置调试输出流。
属性
允许设置创建新连接时将使用的默认配置。
示例
Net::HTTP.default_configuration = { read_timeout: 1, write_timeout: 1 } http = Net::HTTP.new(hostname) http.open_timeout # => 60 http.read_timeout # => 1 http.write_timeout # => 1
返回代理主机的地址,如果没有则返回 nil;请参阅 Net::HTTP 中的代理服务器。
返回访问代理的密码,如果没有则返回 nil;请参阅 Net::HTTP 中的代理服务器。
返回代理主机端口号,如果没有则返回 nil;请参阅 Net::HTTP 中的代理服务器。
与代理通信时使用 SSL。如果 Net::HTTP 不使用代理,则为 nil。
返回访问代理的用户名,如果没有则返回 nil;请参阅 Net::HTTP 中的代理服务器。
返回在 ::new 中作为参数 address 给出的字符串主机名或主机 IP。
设置或返回 PEM 格式的 CA 证书文件的路径。
设置或返回包含 PEM 格式证书文件的 CA 目录的路径。
设置或返回将用于客户端证书的 OpenSSL::X509::Certificate 对象。
设置或返回将用于验证对等证书的 X509::Store 对象。
设置或返回可用的 SSL 密码。请参阅 :SSL::SSLContext#ciphers=。
设置或返回响应为空时是否关闭连接;初始值为 false。
返回继续超时值;请参阅 continue_timeout=。
设置或返回将添加到证书链的额外 X509 证书。请参阅 :SSL::SSLContext#add_certificate。
设置或返回在读取具有 Content-Length 头信息的响应正文时是否忽略文件尾;初始值为 true。
设置或返回将连接保持打开状态的秒数(整数或浮点数);初始值为 2。如果在给定时间间隔内发出新请求,将使用仍打开的连接;否则,连接将被关闭并打开一个新连接。
设置或返回 OpenSSL::PKey::RSA 或 OpenSSL::PKey::DSA 对象。
设置或返回用于建立连接的字符串本地主机;初始值为 nil。
设置或返回用于建立连接的整数本地端口;初始值为 nil。
返回尝试幂等请求的最大次数;请参阅 max_retries=。
设置或返回最大 SSL 版本。请参阅 :SSL::SSLContext#max_version=。
设置或返回最小 SSL 版本。请参阅 :SSL::SSLContext#min_version=。
设置或返回等待连接打开的秒数(整数或浮点数);初始值为 60。如果在给定时间间隔内未建立连接,将引发异常。
设置代理地址;请参阅 代理服务器。
设置代理密码;请参阅 代理服务器。
设置代理端口;请参阅 代理服务器。
设置代理是否使用 SSL;请参阅 代理服务器。
设置代理用户;请参阅 代理服务器。
返回等待读取一个块的秒数(整数或浮点数)(通过一次 read(2) 调用);请参阅 read_timeout=。
返回用于响应正文的编码;请参阅 response_body_encoding=。
设置或返回 SSL 超时秒数。
设置或返回 SSL 版本。请参阅 :SSL::SSLContext#ssl_version=。
设置或返回服务器证书验证的回调。
设置或返回证书链验证的最大深度。
设置或返回是否验证服务器证书对主机名有效。请参阅 :SSL::SSLContext#verify_hostname=。
设置或返回在 SSL/TLS 会话开始时用于服务器证书验证的标志。OpenSSL::SSL::VERIFY_NONE 或 OpenSSL::SSL::VERIFY_PEER 均可接受。
返回等待写入一个块的秒数(整数或浮点数)(通过一次 write(2) 调用);请参阅 write_timeout=。
Public Class Methods
Source
# File lib/net/http.rb, line 935 def HTTP.default_port http_default_port() end
返回整数 80,这是 HTTP 请求的默认端口
Net::HTTP.default_port # => 80
Source
# File lib/net/http.rb, line 804 def HTTP.get(uri_or_host, path_or_headers = nil, port = nil) get_response(uri_or_host, path_or_headers, port).body end
发送 GET 请求并以字符串形式返回 HTTP 响应正文。
使用字符串参数 hostname 和 path
hostname = 'jsonplaceholder.typicode.com' path = '/todos/1' puts Net::HTTP.get(hostname, path)
输出
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
使用 URI 对象 uri 和可选的哈希参数 headers
uri = URI('https://jsonplaceholder.typicode.com/todos/1') headers = {'Content-type' => 'application/json; charset=UTF-8'} Net::HTTP.get(uri, headers)
相关
-
Net::HTTP::Get: HTTP 方法GET的请求类。 -
Net::HTTP#get: HTTP 方法GET的便捷方法。
Source
# File lib/net/http.rb, line 763 def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil) get_response(uri_or_host, path_or_headers, port) {|res| res.read_body do |chunk| $stdout.print chunk end } nil end
类似于 Net::HTTP.get,但将返回的正文写入 $stdout;返回 nil。
Source
# File lib/net/http.rb, line 814 def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block) if path_or_headers && !path_or_headers.is_a?(Hash) host = uri_or_host path = path_or_headers new(host, port || HTTP.default_port).start {|http| return http.request_get(path, &block) } else uri = uri_or_host headers = path_or_headers start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http| return http.request_get(uri, headers, &block) } end end
类似于 Net::HTTP.get,但返回 Net::HTTPResponse 对象而不是正文字符串。
Source
# File lib/net/http.rb, line 943 def HTTP.http_default_port 80 end
返回整数 80,这是 HTTP 请求的默认端口
Net::HTTP.http_default_port # => 80
Source
# File lib/net/http.rb, line 951 def HTTP.https_default_port 443 end
返回整数 443,这是 HTTPS 请求的默认端口
Net::HTTP.https_default_port # => 443
Source
# File lib/net/http.rb, line 1100 def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = nil) http = super address, port if proxy_class? then # from Net::HTTP::Proxy() http.proxy_from_env = @proxy_from_env http.proxy_address = @proxy_address http.proxy_port = @proxy_port http.proxy_user = @proxy_user http.proxy_pass = @proxy_pass http.proxy_use_ssl = @proxy_use_ssl elsif p_addr == :ENV then http.proxy_from_env = true else if p_addr && p_no_proxy && !URI::Generic.use_proxy?(address, address, port, p_no_proxy) p_addr = nil p_port = nil end http.proxy_address = p_addr http.proxy_port = p_port || default_port http.proxy_user = p_user http.proxy_pass = p_pass http.proxy_use_ssl = p_use_ssl end http end
返回一个新的 Net::HTTP 对象 http(但不打开 TCP 连接或 HTTP 会话)。
仅给出字符串参数 address(并且 ENV['http_proxy'] 未定义或为 nil)时,返回的 http
-
具有给定的地址。
-
具有默认端口号
Net::HTTP.default_port(80)。 -
没有代理。
示例
http = Net::HTTP.new(hostname) # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.address # => "jsonplaceholder.typicode.com" http.port # => 80 http.proxy? # => false
如果还提供了整数参数 port,则返回的 http 具有给定的端口
http = Net::HTTP.new(hostname, 8000) # => #<Net::HTTP jsonplaceholder.typicode.com:8000 open=false> http.port # => 8000
有关定义代理的参数 p_addr 到 p_no_proxy,请参阅 代理服务器。
Source
# File lib/net/http.rb, line 857 def HTTP.post(url, data, header = nil) start(url.hostname, url.port, :use_ssl => url.scheme == 'https' ) {|http| http.post(url, data, header) } end
将数据 POST 到主机;返回 Net::HTTPResponse 对象。
参数 url 必须是 URL;参数 data 必须是字符串
_uri = uri.dup _uri.path = '/posts' data = '{"title": "foo", "body": "bar", "userId": 1}' headers = {'content-type': 'application/json'} res = Net::HTTP.post(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true> puts res.body
输出
{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}
相关
-
Net::HTTP::Post: HTTP 方法POST的请求类。 -
Net::HTTP#post:HTTP 方法POST的便捷方法。
Source
# File lib/net/http.rb, line 884 def HTTP.post_form(url, params) req = Post.new(url) req.form_data = params req.basic_auth url.user, url.password if url.user start(url.hostname, url.port, :use_ssl => url.scheme == 'https' ) {|http| http.request(req) } end
将数据 POST 到主机;返回 Net::HTTPResponse 对象。
参数 url 必须是一个 URI;参数 data 必须是一个哈希表。
_uri = uri.dup _uri.path = '/posts' data = {title: 'foo', body: 'bar', userId: 1} res = Net::HTTP.post_form(_uri, data) # => #<Net::HTTPCreated 201 Created readbody=true> puts res.body
输出
{
"title": "foo",
"body": "bar",
"userId": "1",
"id": 101
}
Source
# File lib/net/http.rb, line 1855 def proxy_class? defined?(@is_proxy_class) ? @is_proxy_class : false end
如果 self 是由 HTTP::Proxy 创建的类,则返回 true。
Source
# File lib/net/http.rb, line 920 def HTTP.put(url, data, header = nil) start(url.hostname, url.port, :use_ssl => url.scheme == 'https' ) {|http| http.put(url, data, header) } end
向服务器发送 PUT 请求;返回一个 Net::HTTPResponse 对象。
参数 url 必须是 URL;参数 data 必须是字符串
_uri = uri.dup _uri.path = '/posts' data = '{"title": "foo", "body": "bar", "userId": 1}' headers = {'content-type': 'application/json'} res = Net::HTTP.put(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true> puts res.body
输出
{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}
相关
-
Net::HTTP::Put:HTTP 方法PUT的请求类。 -
Net::HTTP#put:HTTP 方法PUT的便捷方法。
Source
# File lib/net/http.rb, line 1045 def HTTP.start(address, *arg, &block) # :yield: +http+ arg.pop if opt = Hash.try_convert(arg[-1]) port, p_addr, p_port, p_user, p_pass = *arg p_addr = :ENV if arg.size < 2 port = https_default_port if !port && opt && opt[:use_ssl] http = new(address, port, p_addr, p_port, p_user, p_pass) http.ipaddr = opt[:ipaddr] if opt && opt[:ipaddr] if opt if opt[:use_ssl] opt = {verify_mode: OpenSSL::SSL::VERIFY_PEER}.update(opt) end http.methods.grep(/\A(\w+)=\z/) do |meth| key = $1.to_sym opt.key?(key) or next http.__send__(meth, opt[key]) end end http.start(&block) end
通过 Net::HTTP.new 创建一个新的 Net::HTTP 对象 http。
-
对于参数
address和port,请参阅Net::HTTP.new。 -
对于代理定义参数
p_addr到p_pass,请参阅 代理服务器。 -
对于参数
opts,请参阅下文。
不带块参数时
-
调用不带块的
http.start(请参阅start),这会打开一个 TCP 连接和 HTTP 会话。 -
返回
http。 -
调用者应调用
finish来关闭会话。http = Net::HTTP.start(hostname) http.started? # => true http.finish http.started? # => false
带块参数时
-
调用带块的
http.start(请参阅start),这会:-
打开一个 TCP 连接和 HTTP 会话。
-
调用块,该块可以对主机进行任意数量的请求。
-
在块退出时关闭 HTTP 会话和 TCP 连接。
-
返回块的值
object。
-
-
返回
object。
示例
hostname = 'jsonplaceholder.typicode.com' Net::HTTP.start(hostname) do |http| puts http.get('/todos/1').body puts http.get('/todos/2').body end
输出
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
}
如果给定的最后一个参数是哈希,则它是 opts 哈希,其中每个键都是一个要调用的方法或访问器,其值是要设置的值。
键可以包括
注意:如果 port 是 nil 且 opts[:use_ssl] 是一个真值,则传递给 new 的值是 Net::HTTP.https_default_port,而不是 port。
Source
# File lib/net/http.rb, line 738 def HTTP.version_1_2 true end
返回 true;为兼容性保留。
Source
# File lib/net/http.rb, line 743 def HTTP.version_1_2? true end
返回 true;为兼容性保留。
Public Instance Methods
Source
# File lib/net/http.rb, line 1455 def continue_timeout=(sec) @socket.continue_timeout = sec if @socket @continue_timeout = sec end
设置 continue 超时值,即等待预期的 100 Continue 响应的秒数。如果 HTTP 对象在此时间内未收到响应,它将发送请求正文。
Source
# File lib/net/http.rb, line 2227 def copy(path, initheader = nil) request(Copy.new(path, initheader)) end
向服务器发送 COPY 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path 和初始头哈希 initheader 创建的 Net::HTTP::Copy 对象。
http = Net::HTTP.new(hostname) http.copy('/todos/1')
Source
# File lib/net/http.rb, line 2201 def delete(path, initheader = {'Depth' => 'Infinity'}) request(Delete.new(path, initheader)) end
向服务器发送 DELETE 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path 和初始头哈希 initheader 创建的 Net::HTTP::Delete 对象。
http = Net::HTTP.new(hostname) http.delete('/todos/1')
Source
# File lib/net/http.rb, line 1648 def finish raise IOError, 'HTTP session not yet started' unless started? do_finish end
结束 HTTP 会话。
http = Net::HTTP.new(hostname) http.start http.started? # => true http.finish # => nil http.started? # => false
如果在会话中,则引发 IOError。
Source
# File lib/net/http.rb, line 2013 def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+ res = nil request(Get.new(path, initheader)) {|r| r.read_body dest, &block res = r } res end
向服务器发送 GET 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path 和初始头哈希 initheader 创建的 Net::HTTP::Get 对象。
如果提供块,则使用响应正文调用块。
http = Net::HTTP.new(hostname) http.get('/todos/1') do |res| p res end # => #<Net::HTTPOK 200 OK readbody=true>
输出
"{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false\n}"
如果不提供块,则直接返回响应对象。
http.get('/') # => #<Net::HTTPOK 200 OK readbody=true>
相关
-
Net::HTTP::Get:HTTP 方法 GET 的请求类。 -
Net::HTTP.get:发送 GET 请求,返回响应正文。
Source
# File lib/net/http.rb, line 2037 def head(path, initheader = nil) request(Head.new(path, initheader)) end
向服务器发送 HEAD 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path 和初始头哈希 initheader 创建的 Net::HTTP::Head 对象。
res = http.head('/todos/1') # => #<Net::HTTPOK 200 OK readbody=true> res.body # => nil res.to_hash.take(3) # => [["date", ["Wed, 15 Feb 2023 15:25:42 GMT"]], ["content-type", ["application/json; charset=utf-8"]], ["connection", ["close"]]]
Source
# File lib/net/http.rb, line 1206 def inspect "#<#{self.class} #{@address}:#{@port} open=#{started?}>" end
返回 self 的字符串表示形式。
Net::HTTP.new(hostname).inspect # => "#<Net::HTTP jsonplaceholder.typicode.com:80 open=false>"
Source
# File lib/net/http.rb, line 1349 def ipaddr started? ? @socket.io.peeraddr[3] : @ipaddr end
返回连接的 IP 地址。
如果尚未开始会话,则返回由 ipaddr= 设置的值,如果未设置,则返回 nil。
http = Net::HTTP.new(hostname) http.ipaddr # => nil http.ipaddr = '172.67.155.76' http.ipaddr # => "172.67.155.76"
如果会话已开始,则返回套接字中的 IP 地址。
http = Net::HTTP.new(hostname) http.start http.ipaddr # => "172.67.155.76" http.finish
Source
# File lib/net/http.rb, line 1361 def ipaddr=(addr) raise IOError, "ipaddr value changed, but session already started" if started? @ipaddr = addr end
设置连接的 IP 地址。
http = Net::HTTP.new(hostname) http.ipaddr # => nil http.ipaddr = '172.67.155.76' http.ipaddr # => "172.67.155.76"
如果会话已开始,则 IP 地址可能无法设置。
Source
# File lib/net/http.rb, line 2147 def lock(path, body, initheader = nil) request(Lock.new(path, initheader), body) end
向服务器发送 LOCK 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path、字符串 body 和初始头哈希 initheader 创建的 Net::HTTP::Lock 对象。
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http = Net::HTTP.new(hostname) http.lock('/todos/1', data)
Source
# File lib/net/http.rb, line 1395 def max_retries=(retries) retries = retries.to_int if retries < 0 raise ArgumentError, 'max_retries should be non-negative integer number' end @max_retries = retries end
设置在出现 Net::ReadTimeout、IOError、EOFError、Errno::ECONNRESET、Errno::ECONNABORTED、Errno::EPIPE、OpenSSL::SSL::SSLError、Timeout::Error 时重试幂等请求的最大次数。初始值为 1。
参数 retries 必须是一个非负数值。
http = Net::HTTP.new(hostname) http.max_retries = 2 # => 2 http.max_retries # => 2
Source
# File lib/net/http.rb, line 2241 def mkcol(path, body = nil, initheader = nil) request(Mkcol.new(path, initheader), body) end
向服务器发送 MKCOL 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path、字符串 body 和初始头哈希 initheader 创建的 Net::HTTP::Mkcol 对象。
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http.mkcol('/todos/1', data) http = Net::HTTP.new(hostname)
Source
# File lib/net/http.rb, line 2214 def move(path, initheader = nil) request(Move.new(path, initheader)) end
向服务器发送 MOVE 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path 和初始头哈希 initheader 创建的 Net::HTTP::Move 对象。
http = Net::HTTP.new(hostname) http.move('/todos/1')
Source
# File lib/net/http.rb, line 2174 def options(path, initheader = nil) request(Options.new(path, initheader)) end
向服务器发送 Options 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path 和初始头哈希 initheader 创建的 Net::HTTP::Options 对象。
http = Net::HTTP.new(hostname) http.options('/')
Source
# File lib/net/http.rb, line 2100 def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+ send_entity(path, data, initheader, dest, Patch, &block) end
向服务器发送 PATCH 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path、字符串 data 和初始头哈希 initheader 创建的 Net::HTTP::Patch 对象。
如果提供块,则使用响应正文调用块。
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http = Net::HTTP.new(hostname) http.patch('/todos/1', data) do |res| p res end # => #<Net::HTTPOK 200 OK readbody=true>
输出
"{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false,\n \"{\\\"userId\\\": 1, \\\"id\\\": 1, \\\"title\\\": \\\"delectus aut autem\\\", \\\"completed\\\": false}\": \"\"\n}"
如果不提供块,则直接返回响应对象。
http.patch('/todos/1', data) # => #<Net::HTTPCreated 201 Created readbody=true>
Source
# File lib/net/http.rb, line 1597 def peer_cert if not use_ssl? or not @socket return nil end @socket.io.peer_cert end
返回会话套接字对端的 X509 证书链(字符串数组),如果没有则返回 nil。
Source
# File lib/net/http.rb, line 2071 def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+ send_entity(path, data, initheader, dest, Post, &block) end
向服务器发送 POST 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path、字符串 data 和初始头哈希 initheader 创建的 Net::HTTP::Post 对象。
如果提供块,则使用响应正文调用块。
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http = Net::HTTP.new(hostname) http.post('/todos', data) do |res| p res end # => #<Net::HTTPCreated 201 Created readbody=true>
输出
"{\n \"{\\\"userId\\\": 1, \\\"id\\\": 1, \\\"title\\\": \\\"delectus aut autem\\\", \\\"completed\\\": false}\": \"\",\n \"id\": 201\n}"
如果不提供块,则直接返回响应对象。
http.post('/todos', data) # => #<Net::HTTPCreated 201 Created readbody=true>
相关
-
Net::HTTP::Post:HTTP 方法 POST 的请求类。 -
Net::HTTP.post:发送 POST 请求,返回响应正文。
Source
# File lib/net/http.rb, line 2188 def propfind(path, body = nil, initheader = {'Depth' => '0'}) request(Propfind.new(path, initheader), body) end
向服务器发送 PROPFIND 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path、字符串 body 和初始头哈希 initheader 创建的 Net::HTTP::Propfind 对象。
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http = Net::HTTP.new(hostname) http.propfind('/todos/1', data)
Source
# File lib/net/http.rb, line 2133 def proppatch(path, body, initheader = nil) request(Proppatch.new(path, initheader), body) end
向服务器发送 PROPPATCH 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path、字符串 body 和初始头哈希 initheader 创建的 Net::HTTP::Proppatch 对象。
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http = Net::HTTP.new(hostname) http.proppatch('/todos/1', data)
Source
# File lib/net/http.rb, line 1881 def proxy? !!(@proxy_from_env ? proxy_uri : @proxy_address) end
如果定义了代理服务器,则返回 true,否则返回 false;请参阅 代理服务器。
Source
# File lib/net/http.rb, line 1903 def proxy_address if @proxy_from_env then proxy_uri&.hostname else @proxy_address end end
如果定义了代理服务器,则返回其地址,否则返回 nil;请参阅 代理服务器。
Source
# File lib/net/http.rb, line 1888 def proxy_from_env? @proxy_from_env end
如果代理服务器在环境变量中定义,则返回 true,否则返回 false;请参阅 代理服务器。
Source
# File lib/net/http.rb, line 1934 def proxy_pass if @proxy_from_env pass = proxy_uri&.password unescape(pass) if pass else @proxy_pass end end
如果定义了代理服务器,则返回其密码,否则返回 nil;请参阅 代理服务器。
Source
# File lib/net/http.rb, line 1913 def proxy_port if @proxy_from_env then proxy_uri&.port else @proxy_port end end
如果定义了代理服务器,则返回其端口号,否则返回 nil;请参阅 代理服务器。
Source
# File lib/net/http.rb, line 1923 def proxy_user if @proxy_from_env user = proxy_uri&.user unescape(user) if user else @proxy_user end end
如果定义了代理服务器,则返回其用户名,否则返回 nil;请参阅 代理服务器。
Source
# File lib/net/http.rb, line 2119 def put(path, data, initheader = nil) request(Put.new(path, initheader), data) end
向服务器发送 PUT 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path、字符串 data 和初始头哈希 initheader 创建的 Net::HTTP::Put 对象。
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http = Net::HTTP.new(hostname) http.put('/todos/1', data) # => #<Net::HTTPOK 200 OK readbody=true>
相关
-
Net::HTTP::Put:HTTP 方法 PUT 的请求类。 -
Net::HTTP.put:发送 PUT 请求,返回响应正文。
Source
# File lib/net/http.rb, line 1418 def read_timeout=(sec) @socket.read_timeout = sec if @socket @read_timeout = sec end
将 self 的读取超时(以秒为单位)设置为整数 sec;初始值为 60。
参数 sec 必须是一个非负数值。
http = Net::HTTP.new(hostname) http.read_timeout # => 60 http.get('/todos/1') # => #<Net::HTTPOK 200 OK readbody=true> http.read_timeout = 0 http.get('/todos/1') # Raises Net::ReadTimeout.
Source
# File lib/net/http.rb, line 2399 def request(req, body = nil, &block) # :yield: +response+ unless started? start { req['connection'] ||= 'close' return request(req, body, &block) } end if proxy_user() req.proxy_basic_auth proxy_user(), proxy_pass() unless use_ssl? end req.set_body_internal body res = transport_request(req, &block) if sspi_auth?(res) sspi_auth(req) res = transport_request(req, &block) end res end
向服务器发送给定的请求 req;将响应格式化为 Net::HTTPResponse 对象。
给定的 req 必须是 Net::HTTPRequest 的子类 的实例。如果请求需要,则应提供参数 body。
不带块参数时,返回响应对象。
http = Net::HTTP.new(hostname) req = Net::HTTP::Get.new('/todos/1') http.request(req) # => #<Net::HTTPOK 200 OK readbody=true> req = Net::HTTP::Post.new('/todos') http.request(req, 'xyzzy') # => #<Net::HTTPCreated 201 Created readbody=true>
带块参数时,使用响应调用块,并返回响应。
req = Net::HTTP::Get.new('/todos/1') http.request(req) do |res| p res end # => #<Net::HTTPOK 200 OK readbody=true>
输出
#<Net::HTTPOK 200 OK readbody=false>
Source
# File lib/net/http.rb, line 2280 def request_get(path, initheader = nil, &block) # :yield: +response+ request(Get.new(path, initheader), &block) end
向服务器发送 GET 请求;将响应格式化为 Net::HTTPResponse 对象。
该请求基于由字符串 path 和初始头哈希 initheader 创建的 Net::HTTP::Get 对象。
不带块参数时,返回响应对象。
http = Net::HTTP.new(hostname) http.request_get('/todos') # => #<Net::HTTPOK 200 OK readbody=true>
带块参数时,使用响应对象调用块,并返回响应对象。
http.request_get('/todos') do |res| p res end # => #<Net::HTTPOK 200 OK readbody=true>
输出
#<Net::HTTPOK 200 OK readbody=false>
Source
# File lib/net/http.rb, line 2293 def request_head(path, initheader = nil, &block) request(Head.new(path, initheader), &block) end
向服务器发送 HEAD 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path 和初始头哈希 initheader 创建的 Net::HTTP::Head 对象。
http = Net::HTTP.new(hostname) http.head('/todos/1') # => #<Net::HTTPOK 200 OK readbody=true>
Source
# File lib/net/http.rb, line 2320 def request_post(path, data, initheader = nil, &block) # :yield: +response+ request Post.new(path, initheader), data, &block end
向服务器发送 POST 请求;将响应格式化为 Net::HTTPResponse 对象。
该请求基于由字符串 path、字符串 data 和初始头哈希 initheader 创建的 Net::HTTP::Post 对象。
不带块参数时,返回响应对象。
http = Net::HTTP.new(hostname) http.post('/todos', 'xyzzy') # => #<Net::HTTPCreated 201 Created readbody=true>
带块参数时,使用响应正文调用块,并返回响应对象。
http.post('/todos', 'xyzzy') do |res| p res end # => #<Net::HTTPCreated 201 Created readbody=true>
输出
"{\n \"xyzzy\": \"\",\n \"id\": 201\n}"
Source
# File lib/net/http.rb, line 1300 def response_body_encoding=(value) value = Encoding.find(value) if value.is_a?(String) @response_body_encoding = value end
Source
# File lib/net/http.rb, line 2363 def send_request(name, path, data = nil, header = nil) has_response_body = name != 'HEAD' r = HTTPGenericRequest.new(name,(data ? true : false),has_response_body,path,header) request r, data end
向服务器发送 HTTP 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path、字符串 data 和初始头哈希 header 创建的 Net::HTTPRequest 对象。该对象是 Net::HTTPRequest 的子类 的实例,对应于给定的全大写字符串 name,它必须是一个 HTTP 请求方法 或 WebDAV 请求方法。
示例
http = Net::HTTP.new(hostname) http.send_request('GET', '/todos/1') # => #<Net::HTTPOK 200 OK readbody=true> http.send_request('POST', '/todos', 'xyzzy') # => #<Net::HTTPCreated 201 Created readbody=true>
Source
# File lib/net/http.rb, line 1259 def set_debug_output(output) warn 'Net::HTTP#set_debug_output called after HTTP started', uplevel: 1 if started? @debug_output = output end
警告 此方法会打开一个严重的安全漏洞。切勿在生产代码中使用此方法。
设置调试的输出流。
http = Net::HTTP.new(hostname) File.open('t.tmp', 'w') do |file| http.set_debug_output(file) http.start http.get('/nosuch/1') http.finish end puts File.read('t.tmp')
输出
opening connection to jsonplaceholder.typicode.com:80...
opened
<- "GET /nosuch/1 HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: Ruby\r\nHost: jsonplaceholder.typicode.com\r\n\r\n"
-> "HTTP/1.1 404 Not Found\r\n"
-> "Date: Mon, 12 Dec 2022 21:14:11 GMT\r\n"
-> "Content-Type: application/json; charset=utf-8\r\n"
-> "Content-Length: 2\r\n"
-> "Connection: keep-alive\r\n"
-> "X-Powered-By: Express\r\n"
-> "X-Ratelimit-Limit: 1000\r\n"
-> "X-Ratelimit-Remaining: 999\r\n"
-> "X-Ratelimit-Reset: 1670879660\r\n"
-> "Vary: Origin, Accept-Encoding\r\n"
-> "Access-Control-Allow-Credentials: true\r\n"
-> "Cache-Control: max-age=43200\r\n"
-> "Pragma: no-cache\r\n"
-> "Expires: -1\r\n"
-> "X-Content-Type-Options: nosniff\r\n"
-> "Etag: W/\"2-vyGp6PvFo4RvsFtPoIWeCReyIC8\"\r\n"
-> "Via: 1.1 vegur\r\n"
-> "CF-Cache-Status: MISS\r\n"
-> "Server-Timing: cf-q-config;dur=1.3000000762986e-05\r\n"
-> "Report-To: {\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=yOr40jo%2BwS1KHzhTlVpl54beJ5Wx2FcG4gGV0XVrh3X9OlR5q4drUn2dkt5DGO4GDcE%2BVXT7CNgJvGs%2BZleIyMu8CLieFiDIvOviOY3EhHg94m0ZNZgrEdpKD0S85S507l1vsEwEHkoTm%2Ff19SiO\"}],\"group\":\"cf-nel\",\"max_age\":604800}\r\n"
-> "NEL: {\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}\r\n"
-> "Server: cloudflare\r\n"
-> "CF-RAY: 778977dc484ce591-DFW\r\n"
-> "alt-svc: h3=\":443\"; ma=86400, h3-29=\":443\"; ma=86400\r\n"
-> "\r\n"
reading 2 bytes...
-> "{}"
read 2 bytes
Conn keep-alive
Source
# File lib/net/http.rb, line 1625 def start # :yield: http raise IOError, 'HTTP session already opened' if @started if block_given? begin do_start return yield(self) ensure do_finish end end do_start self end
启动 HTTP 会话。
不带块参数时,返回 self。
http = Net::HTTP.new(hostname) # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.start # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=true> http.started? # => true http.finish
带块参数时,使用 self 调用块,在块退出时关闭会话,并返回块的值。
http.start do |http| http end # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.started? # => false
Source
# File lib/net/http.rb, line 1488 def started? @started end
如果 HTTP 会话已启动,则返回 true。
http = Net::HTTP.new(hostname) http.started? # => false http.start http.started? # => true http.finish # => nil http.started? # => false Net::HTTP.start(hostname) do |http| http.started? end # => true http.started? # => false
Source
# File lib/net/http.rb, line 2254 def trace(path, initheader = nil) request(Trace.new(path, initheader)) end
向服务器发送 TRACE 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path 和初始头哈希 initheader 创建的 Net::HTTP::Trace 对象。
http = Net::HTTP.new(hostname) http.trace('/todos/1')
Source
# File lib/net/http.rb, line 2161 def unlock(path, body, initheader = nil) request(Unlock.new(path, initheader), body) end
向服务器发送 UNLOCK 请求;返回 Net::HTTPResponse 的子类的实例。
该请求基于由字符串 path、字符串 body 和初始头哈希 initheader 创建的 Net::HTTP::Unlock 对象。
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http = Net::HTTP.new(hostname) http.unlock('/todos/1', data)
Source
# File lib/net/http.rb, line 1510 def use_ssl=(flag) flag = flag ? true : false if started? and @use_ssl != flag raise IOError, "use_ssl value changed, but session already started" end @use_ssl = flag end
Source
# File lib/net/http.rb, line 1500 def use_ssl? @use_ssl end
如果 self 使用 SSL,则返回 true,否则返回 false。请参阅 Net::HTTP#use_ssl=。
Source
# File lib/net/http.rb, line 1442 def write_timeout=(sec) @socket.write_timeout = sec if @socket @write_timeout = sec end
将 self 的写入超时(以秒为单位)设置为整数 sec;初始值为 60。
参数 sec 必须是一个非负数值。
_uri = uri.dup _uri.path = '/posts' body = 'bar' * 200000 data = <<EOF {"title": "foo", "body": "#{body}", "userId": "1"} EOF headers = {'content-type': 'application/json'} http = Net::HTTP.new(hostname) http.write_timeout # => 60 http.post(_uri.path, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true> http.write_timeout = 0 http.post(_uri.path, data, headers) # Raises Net::WriteTimeout.
私有实例方法
Source
# File lib/net/http.rb, line 2422 def send_entity(path, data, initheader, dest, type, &block) res = nil request(type.new(path, initheader), data) {|r| r.read_body dest, &block res = r } res end
执行使用表示形式的请求,并返回其正文。