2012-07-23 147 views
1

我的一个脚本提交了一个HTTP请求,但我想将原始HTTP请求发送给一个关联进行故障排除。有没有一种方法可以将原始HTTP请求输出到文件?Ruby输出的原始HTTP请求

回答

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" 
+0

更改显示响应的编码的最佳方法是什么? – willpots 2013-08-12 21:39:54

+0

捕获字符串(对于文件可能最简单)后,使用[String#encode](http://ruby-doc.org/core-2.2.0/String.html#method-i- code) – 2015-05-06 00:25:43