gpt4 book ai didi

ruby-on-rails - 如何在 OpenURI (Ruby) 中发送自定义 header 并检查它们

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

在以下文档中找到:https://ruby-doc.org/stdlib-2.1.0/libdoc/open-uri/rdoc/OpenURI.html

Additional header fields can be specified by an optional hash argument.

 open("http://www.ruby-lang.org/en/",
"User-Agent" => "Ruby/#{RUBY_VERSION}",
"From" => "foo@bar.invalid",
"Referer" => "http://www.ruby-lang.org/") {|f|
# ...
}

谁能告诉我如何检查它是否正常工作?

最佳答案

Can someone tell how to check whether they [the additional header fields] are properly working.

你可以使用 netcat监听特定端口,例如 2000:

$ nc -l 2000

并将主机相应地更改为 localhost:2000:

# open.rb
require 'open-uri'

open("http://localhost:2000/",
"User-Agent" => "Ruby/#{RUBY_VERSION}",
"From" => "foo@bar.invalid",
"Referer" => "http://www.ruby-lang.org/") {|f|
# ...
}

运行程序通过

$ ruby open.rb

将使 netcat 显示:

$ nc -l 2000
GET / HTTP/1.1
User-Agent: Ruby/2.3.1
From: foo@bar.invalid
Referer: http://www.ruby-lang.org/
Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept: */*
Host: localhost:2000

显然, header 工作正常。 (当然有)

关于ruby-on-rails - 如何在 OpenURI (Ruby) 中发送自定义 header 并检查它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40636423/

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