1
스크립트가 HTTP 요청을 제출하지만 원시 HTTP 요청을 문제 해결을 위해 연결 요청자에게 보내려고합니다. 원시 HTTP 요청을 파일로 출력 할 수있는 방법이 있습니까?Ruby가 원시 HTTP 요청을 출력합니다.
스크립트가 HTTP 요청을 제출하지만 원시 HTTP 요청을 문제 해결을 위해 연결 요청자에게 보내려고합니다. 원시 HTTP 요청을 파일로 출력 할 수있는 방법이 있습니까?Ruby가 원시 HTTP 요청을 출력합니다.
는 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
을 인스턴스 (예 : 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"
응답이 표시되는 인코딩을 변경하는 가장 좋은 방법은 무엇입니까? – willpots
문자열을 캡처 한 후 (파일에 대해 가장 단순한 경우) [String # encode] (http://ruby-doc.org/core-2.2.0/String.html#method-i-encode)를 사용하십시오. –