如何在控制台上的 Ruby 中使用 net/http 打印默认 HTTP GET/POST REQUEST/RESPONSE 标头并在请求中添加新标头

发布于 2025-01-06 19:40:50 字数 708 浏览 1 评论 0原文

我想使用 net/http 在控制台上打印 GET/POST http 标头,如下所示。

GET /en HTTP/1.1
Host: www.html5rocks.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101          Firefox/10.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Access-Control-Allow-Origin: *
X-UA-Compatible: IE=Edge,chrome=1
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Sat, 18 Feb 2012 09:10:23 GMT
Server: Google Frontend
Content-Length: 8940

可以这样做吗?

I want to print GET/POST http headers like following on Console using net/http.

GET /en HTTP/1.1
Host: www.html5rocks.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101          Firefox/10.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Access-Control-Allow-Origin: *
X-UA-Compatible: IE=Edge,chrome=1
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Sat, 18 Feb 2012 09:10:23 GMT
Server: Google Frontend
Content-Length: 8940

Is it possible to do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2025-01-13 19:40:50

您可以通过以下方式打印标题:

uri = URI('http://www.html5rocks.com/en')
Net::HTTP.start(uri.host, uri.port) do |http|
  request = Net::HTTP::Get.new uri.request_uri
  request.each_header {|key,value| puts "#{key} = #{value}" }
  response = http.request request
  response.header.each_header {|key,value| puts "#{key} = #{value}" }
end

You can print headers by this way:

uri = URI('http://www.html5rocks.com/en')
Net::HTTP.start(uri.host, uri.port) do |http|
  request = Net::HTTP::Get.new uri.request_uri
  request.each_header {|key,value| puts "#{key} = #{value}" }
  response = http.request request
  response.header.each_header {|key,value| puts "#{key} = #{value}" }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文