我想在控制台上使用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
是否可以这样做?
答案 0 :(得分:1)
您可以通过以下方式打印标题:
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