Java HttpRequestBase - 如何打印请求及其所有数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18744226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:38:57  来源:igfitidea点击:

HttpRequestBase - How to print the request with all its data

javahttptostring

提问by SharonBL

I'm using HttpRequestBase and I want to log the request fully to a log file before using it.

我正在使用 HttpRequestBase 并且我想在使用它之前将请求完全记录到日志文件中。

The default toString returns only the request line and I want to print all the headers, parameters, request body etc...

默认 toString 仅返回请求行,我想打印所有标头、参数、请求正文等...

Is there a way to do so?

有没有办法这样做?

采纳答案by Sotirios Delimanolis

The HttpRequestBaseobject (HttpGet, HttpPost, etc.) contains information about the headers, parameters, and the implementation class contains the body, but it's not actually serialized into a String. That happens when the HttpClientactually sends the request.

HttpRequestBase对象(HttpGetHttpPost,等)包含了头,参数的信息,并实现类含有人体,但它实际上并不序列化为String。当HttpClient实际发送请求时会发生这种情况。

You can play with the http components logging configuration.

您可以使用 http 组件日志记录配置

Or you can call the appropriate methods and do it yourself.

或者您可以调用适当的方法并自己完成。

HttpRequestBase base = new HttpGet("www.google.com");
Header[] headers = base.getAllHeaders();
// iterate and print

For the body, you need to cast to your implementation class and get the HttpEntity, if it has one.

对于主体,您需要转换到您的实现类并获取HttpEntity, 如果它有一个。

HttpEntity entity = ((HttpPost)base).getEntity(); // example

And print it (its InputStreamcontent). Note: That might consume the entity.

并打印它(它的InputStream内容)。注意:这可能会消耗实体。

Full example

完整示例

HttpPost post = new HttpPost("www.google.com");
post.setHeader(new BasicHeader("User-Agent", "random client"));
HttpEntity entity = new StringEntity("yellaworld");
post.setEntity(entity);
Header[] headers = post.getAllHeaders();
String content = EntityUtils.toString(entity);

System.out.println(post.toString());
for (Header header : headers) {
    System.out.println(header.getName() + ": " + header.getValue());
}
System.out.println();
System.out.println(content);

prints

印刷

POST www.google.com HTTP/1.1
User-Agent: random client

yellaworld

回答by Bruno Lee

This works

这有效

private void printRequest() {
            System.out.println("receive " + httpRequest.getMethod() +" notification for "+ httpRequest.getRequestURI());


            System.out.println(" \n\n Headers");

            Enumeration headerNames = httpRequest.getHeaderNames();
            while(headerNames.hasMoreElements()) {
                String headerName = (String)headerNames.nextElement();
                System.out.println(headerName + " = " + httpRequest.getHeader(headerName));
            }

            System.out.println("\n\nParameters");

            Enumeration params = httpRequest.getParameterNames();
            while(params.hasMoreElements()){
                String paramName = (String)params.nextElement();
                System.out.println(paramName + " = " + httpRequest.getParameter(paramName));
            }

            System.out.println("\n\n Row data");
            System.out.println(extractPostRequestBody(httpRequest));
        }

        static String extractPostRequestBody(HttpServletRequest request) {
            if ("POST".equalsIgnoreCase(request.getMethod())) {
                Scanner s = null;
                try {
                    s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\A");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return s.hasNext() ? s.next() : "";
            }
            return "";
        }