如何打印httprequest请求的内容? [英] How do I print the content of httprequest request?

查看:1487
本文介绍了如何打印httprequest请求的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个涉及httprequest的错误,有时会发生这种错误,因此我想在发生这种情况时记录HttpGet和HttpPost请求的内容。

I've got a bug involving httprequest, which happens sometimes, so I'd like to log HttpGet and HttpPost request's content when that happens.

所以,让我们来吧比方说,我这样创建HttpGet:

So, let's say, I create HttpGet like this:

HttpGet g = new HttpGet();
g.setURI(new URI("http://www.google.com"));
g.setHeader("test", "hell yeah");

这是我想要的字符串表示形式:

This is the string representation that I'd like to get:

GET / HTTP/1.1
Host: www.google.com
test: hell yeah

通过发布请求,我还想获取内容字符串。

With the post request, I'd also like to get the content string.

在java for android中最简单的方法是什么?

What is the easiest way to do it in java for android?

推荐答案

您可以使用以下方式打印请求类型:

You can print the request type using:

request.getMethod();

您可以打印此处提到的所有标题:

You can print all the headers as mentioned here:

Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
  String headerName = headerNames.nextElement();
  System.out.println("Header Name - " + headerName + ", Value - " + request.getHeader(headerName));
}

要打印所有请求参数,请使用:

To print all the request params, use this:

Enumeration<String> params = request.getParameterNames(); 
while(params.hasMoreElements()){
 String paramName = params.nextElement();
 System.out.println("Parameter Name - "+paramName+", Value - "+request.getParameter(paramName));
}

request HttpServletRequest的实例

您可以根据需要美化输出。

You can beautify the outputs as per your need.

这篇关于如何打印httprequest请求的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆