[英]How to print default HTTP GET/POST REQUEST/RESPONSE Headers and add new headers in request using net/http in Ruby on console
I want to print GET/POST http headers like following on Console using net/http. 我想像在控制台上使用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
Is it possible to do so? 有可能这样做吗?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.