我有一个涉及httprequest的错误,有时会发生这种错误,因此我想在发生这种情况时记录HttpGet和HttpPost请求的内容。
所以,让我们说,我像这样创建HttpGet:
HttpGet g = new HttpGet();
g.setURI(new URI("http://www.google.com"));
g.setHeader("test", "hell yeah");
这是我想要的字符串表示:
GET / HTTP/1.1
Host: www.google.com
test: hell yeah
根据帖子请求,我还想获取内容字符串。
在java for android中最简单的方法是什么?
答案 0 :(得分:70)
您可以使用以下方式打印请求类型:
request.getMethod();
您可以打印所有标题:
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
System.out.println("Header Name - " + headerName + ", Value - " + request.getHeader(headerName));
}
要打印所有请求参数,请使用:
Enumeration<String> params = request.getParameterNames();
while(params.hasMoreElements()){
String paramName = params.nextElement();
System.out.println("Parameter Name - "+paramName+", Value - "+request.getParameter(paramName));
}
request
是HttpServletRequest
您可以根据需要美化输出。
答案 1 :(得分:1)
有助于记录的更多细节
String client = request.getRemoteAddr();
logger.info("###### requested client: {} , Session ID : {} , URI :" + request.getMethod() + ":" + request.getRequestURI() + "", client, request.getSession().getId());
Map params = request.getParameterMap();
Iterator i = params.keySet().iterator();
while (i.hasNext()) {
String key = (String) i.next();
String value = ((String[]) params.get(key))[0];
logger.info("###### Request Param Name : {} , Value : {} ", key, value);
}
答案 2 :(得分:1)
这应该对调试更有帮助。 @Juned Ahsan的回答不会指定完整的URL,也不会打印多个标题/参数。
private String httpServletRequestToString(HttpServletRequest request) {
StringBuilder sb = new StringBuilder();
sb.append("Request Method = [" + request.getMethod() + "], ");
sb.append("Request URL Path = [" + request.getRequestURL() + "], ");
String headers =
Collections.list(request.getHeaderNames()).stream()
.map(headerName -> headerName + " : " + Collections.list(request.getHeaders(headerName)) )
.collect(Collectors.joining(", "));
if (headers.isEmpty()) {
sb.append("Request headers: NONE,");
} else {
sb.append("Request headers: ["+headers+"],");
}
String parameters =
Collections.list(request.getParameterNames()).stream()
.map(p -> p + " : " + Arrays.asList( request.getParameterValues(p)) )
.collect(Collectors.joining(", "));
if (parameters.isEmpty()) {
sb.append("Request parameters: NONE.");
} else {
sb.append("Request parameters: [" + parameters + "].");
}
return sb.toString();
}
答案 3 :(得分:0)
如果您想要内容字符串,并且此字符串没有参数,则可以使用
String line = null;
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null){
System.out.println(line);
}
答案 4 :(得分:0)
万一有人也想像我一样甩掉回应。我避免抛弃响应主体。以下代码只是转储StatusCode和Headers。
static private String dumpResponse(HttpServletResponse resp){
StringBuilder sb = new StringBuilder();
sb.append("Response Status = [" + resp.getStatus() + "], ");
String headers = resp.getHeaderNames().stream()
.map(headerName -> headerName + " : " + resp.getHeaders(headerName) )
.collect(Collectors.joining(", "));
if (headers.isEmpty()) {
sb.append("Response headers: NONE,");
} else {
sb.append("Response headers: "+headers+",");
}
return sb.toString();
}
答案 5 :(得分:0)
通过流在一行中重写@Juned Ahsan解决方案(标头的处理方式相同):
public static String printRequest(HttpServletRequest req) {
String params = StreamSupport.stream(
((Iterable<String>) () -> req.getParameterNames().asIterator()).spliterator(), false)
.map(pName -> pName + '=' + req.getParameter(pName))
.collect(Collectors.joining("&"));
return req.getRequestURI() + '?' + params;
}