Получите необработанный полный HTTP-запрос внутри веб-обработчика Springs. ⇐ JAVA
-
Anonymous
Получите необработанный полный HTTP-запрос внутри веб-обработчика Springs.
I have a handler for a web-request.
public void handle (HttpServletRequest req) Now I need to log the data of the request - as near to the raw request it can be (like the output a network-logger/sniffer would produce but inside the app and just for the specific request).
This is what I build.
public class HttpReqReader { static byte [] read (HttpServletRequest req) throws Exception { String data = req.getMethod () + " " + req.getRequestURI () + " " + req.getProtocol () + "\r\n"; Enumeration hs = req.getHeaderNames (); while (hs.hasMoreElements ()) { String el = hs.nextElement (); data += el + ": " + req.getHeader (el) + "\r\n"; } data += "\r\n"; InputStream is = req.getInputStream (); byte [] bs = is.readAllBytes (); data += new String (bs); return data.getBytes (); } } Is there any class that could do the same or do it better in Spring-Boot/Java?? I do not like the way I am expecting the HTTP-header in my code.
Источник: https://stackoverflow.com/questions/781 ... webhandler
I have a handler for a web-request.
public void handle (HttpServletRequest req) Now I need to log the data of the request - as near to the raw request it can be (like the output a network-logger/sniffer would produce but inside the app and just for the specific request).
This is what I build.
public class HttpReqReader { static byte [] read (HttpServletRequest req) throws Exception { String data = req.getMethod () + " " + req.getRequestURI () + " " + req.getProtocol () + "\r\n"; Enumeration hs = req.getHeaderNames (); while (hs.hasMoreElements ()) { String el = hs.nextElement (); data += el + ": " + req.getHeader (el) + "\r\n"; } data += "\r\n"; InputStream is = req.getInputStream (); byte [] bs = is.readAllBytes (); data += new String (bs); return data.getBytes (); } } Is there any class that could do the same or do it better in Spring-Boot/Java?? I do not like the way I am expecting the HTTP-header in my code.
Источник: https://stackoverflow.com/questions/781 ... webhandler