我的一个脚本提交了一个 HTTP 请求,但我想将原始 HTTP 请求发送给一位同事以进行故障排除。有没有办法可以将原始 HTTP 请求输出到文件?
问问题
3095 次
1 回答
4
如果您正在使用Net::HTTP
,请使用#set_debug_output
:
1.9.3p194 :017 > http = Net::HTTP.new('google.com')
=> #<Net::HTTP google.com:80 open=false>
1.9.3p194 :018 > http.request_get('/')
=> #<Net::HTTPMovedPermanently 301 Moved Permanently readbody=true>
1.9.3p194 :019 > http.set_debug_output($stdout)
=> #<IO:<STDOUT>>
1.9.3p194 :020 > http.request_get('/')
opening connection to google.com...
opened
<- "GET / HTTP/1.1\r\nAccept: */*\r\nUser-Agent: Ruby\r\nConnection: close\r\nHost: google.com\r\n\r\n"
-> "HTTP/1.1 301 Moved Permanently\r\n"
-> "Location: http://www.google.com/\r\n"
-> "Content-Type: text/html; charset=UTF-8\r\n"
-> "Date: Mon, 23 Jul 2012 21:41:13 GMT\r\n"
-> "Expires: Wed, 22 Aug 2012 21:41:13 GMT\r\n"
-> "Cache-Control: public, max-age=2592000\r\n"
-> "Server: gws\r\n"
-> "Content-Length: 219\r\n"
-> "X-XSS-Protection: 1; mode=block\r\n"
-> "X-Frame-Options: SAMEORIGIN\r\n"
-> "Connection: close\r\n"
-> "\r\n"
reading 219 bytes...
-> "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.com/\">here</A>.\r\n</BODY></HTML>\r\n"
read 219 bytes
Conn close
=> #<Net::HTTPMovedPermanently 301 Moved Permanently readbody=true>
或者HTTParty
,使用以下:debug_output
选项:
1.9.3p194 :028 > HTTParty.get('http://www.google.com', :debug_output => $stdout)
opening connection to www.google.com...
opened
<- "GET / HTTP/1.1\r\nConnection: close\r\nHost: www.google.com\r\n\r\n"
-> "HTTP/1.1 200 OK\r\n"
-> "Date: Mon, 23 Jul 2012 21:42:55 GMT\r\n"
-> "Expires: -1\r\n"
-> "Cache-Control: private, max-age=0\r\n"
-> "Content-Type: text/html; charset=ISO-8859-1\r\n"
-> "Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=www.google.com\r\n"
-> "Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=www.google.com\r\n"
-> "Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=www.google.com\r\n"
-> "Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.www.google.com\r\n"
-> "Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.www.google.com\r\n"
-> "Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.www.google.com\r\n"
-> "Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=google.com\r\n"
-> "Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=google.com\r\n"
-> "Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=google.com\r\n"
-> "Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.google.com\r\n"
-> "Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.google.com\r\n"
-> "Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.google.com\r\n"
-> "Set-Cookie: PREF=ID=bf7ea711fb78b88f:FF=0:TM=1343079775:LM=1343079775:S=sitzmIr74uTXkzTt; expires=Wed, 23-Jul-2014 21:42:55 GMT; path=/; domain=.google.com\r\n"
-> "Set-Cookie: NID=62=i-gpt9kPmB80W787ganAdeSSc502sPHOT5bwpddGHTbVtAB5-oDjjaJtQAN7znbEk70RkuHDui5sJOlZrXB0MzfIKtpU2x7vRDj3vFQch6BRcqQaVmzm_J7OzAIOpB5H; expires=Tue, 22-Jan-2013 21:42:55 GMT; path=/; domain=.google.com; HttpOnly\r\n"
-> "P3P: CP=\"This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info.\"\r\n"
-> "Server: gws\r\n"
-> "X-XSS-Protection: 1; mode=block\r\n"
-> "X-Frame-Options: SAMEORIGIN\r\n"
-> "Connection: close\r\n"
-> "\r\n"
reading all...
# ... snip ...
此时,您可以复制该输出,也可以替换$stdout
为任何IO
实例,例如File
:
1.9.3p194 :034 > File.open('/tmp/http.out', 'w') {|f| http.set_debug_output(f); http.get('/')}
=> #<Net::HTTPMovedPermanently 301 Moved Permanently readbody=true>
1.9.3p194 :035 > File.read('/tmp/http.out')
=> "opening connection to google.com...\nopened\n<- "GET / 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\\nConnection: close\\r\\nHost: google.com\\r\\n\\r\\n"\n-> "HTTP/1.1 301 Moved Permanently\\r\\n"\n-> "Location: http://www.google.com/\\r\\n"\n-> "Content-Type: text/html; charset=UTF-8\\r\\n"\n-> "Date: Mon, 23 Jul 2012 21:46:02 GMT\\r\\n"\n-> "Expires: Wed, 22 Aug 2012 21:46:02 GMT\\r\\n"\n-> "Cache-Control: public, max-age=2592000\\r\\n"\n-> "Server: gws\\r\\n"\n-> "Content-Length: 219\\r\\n"\n-> "X-XSS-Protection: 1; mode=block\\r\\n"\n-> "X-Frame-Options: SAMEORIGIN\\r\\n"\n-> "Connection: close\\r\\n"\n-> "\\r\\n"\nreading 219 bytes...\n-> "<HTML><HEAD><meta http-equiv=\\"content-type\\" content=\\"text/html;charset=utf-8\\">\\n<TITLE>301 Moved</TITLE></HEAD><BODY>\\n<H1>301 Moved</H1>\\nThe document has moved\\n<A HREF=\\"http://www.google.com/\\">here</A>.\\r\\n</BODY></HTML>\\r\\n"\nread 219 bytes\nConn close\n"
于 2012-07-23T21:46:45.040 回答