java 获取完整的 HTTP 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25963565/
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-11-02 09:03:04  来源:igfitidea点击:

Get full HTTP request

javaresthttpunirest

提问by Délisson Junio

For debugging matters, I would like to get the request executed by Unirest-Java when presented with a set of options. How can I get this:

对于调试问题,我希望在提供一组选项时让 Unirest-Java 执行请求。我怎样才能得到这个:

POST / HTTP/1.1
Host: www.some.host.tld
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
field1=FIELD1&field2=FIELD2

From this:

由此:

HttpRequest request = Unirest.post(SOMEHOST_URL)
            .field("field1" , FIELD1)
            .field("field2", FIELD2)
            .getHttpRequest();

That is, how can I get the full HTTP request from a HttpRequest? I don't really know of a proxy to use, as the only one with which I could get SSL support working was Charles, and for some reason it won't pickup the java traffic. Other tools would choke on the SSL mainly because the server I need to talk to is flawed, using self-signed certificates and invalid hostnames. So I would gladly try a proxy, but it has to work under these conditions. Better would be to extract this information from Unirest/HTTPClientitself. I already tried building the HttpClientwith .setInterceptorFirst(but I couldn't get the request body from the interceptor, only someof its headers.

也就是说,我怎样才能从HttpRequest? 我真的不知道要使用的代理,因为我可以使用 SSL 支持的唯一代理是 Charles,并且由于某种原因它不会接收 Java 流量。其他工具会阻塞 SSL,主要是因为我需要与之通信的服务器存在缺陷,使用自签名证书和无效的主机名。所以我很乐意尝试代理,但它必须在这些条件下工作。更好的是从Unirest/HTTPClient本身提取这些信息。我已经尝试构建HttpClientwith.setInterceptorFirst(但我无法从拦截器获取请求正文,只有它的一些标头。

采纳答案by volkovs

I have never used Unirest, but it uses Apache HTTP client 4, so setting debug level logger on org.apache.http should give you all the data transferred including headers and content.

我从未使用过 Unirest,但它使用 Apache HTTP 客户端 4,因此在 org.apache.http 上设置调试级别记录器应该会为您提供所有传输的数据,包括标题和内容。

Please see how to do it depending on the log system you use at https://hc.apache.org/httpcomponents-client-4.3.x/logging.html

请根据您在https://hc.apache.org/httpcomponents-client-4.3.x/logging.html使用的日志系统查看如何执行此操作

回答by fijiaaron

Unfortunately, Unirest doesn't have a good mechanism for getting the request as sent. But you can reconstruct the request by doing this:

不幸的是,Unirest 没有一个很好的机制来获取发送的请求。但是您可以通过执行以下操作来重建请求:

// build a request
HttpRequestWithBody request = Unirest.post(url);
request.headers = myHeaders;
request.body = myBody;

// send the request
HttpResponse<String> response = request.asString(); 

// print the request 
System.out.println(request.getHttpMethod() + " " + request.getUrl() + " HTTP/1.1");

Map<String, List<String>> requestHeaders = request.getHeaders();

for(String key : requestHeaders.keySet()) {
    List<String> values = requestHeaders.get(key);
    for(String value : values) {
        System.out.println(key + " : " + value);
    }
}

InputStream in = loginRequest.getBody().getEntity().getContent();
String body = IOUtils.toString(in, "UTF-8");
in.close();

System.out.println(body);

回答by Ernesto Campohermoso

You can use TCPMon: https://code.google.com/p/tcpmon/

您可以使用 TCPMon:https: //code.google.com/p/tcpmon/

From the website:

从网站:

The client will connect to the Local Port on the host where tcpmon is running. The address of the server should be specified in Server Name and Server Port. Information captured for each connection is displayed in a tabbed panel identified by the local port of the connection.

客户端将连接到运行 tcpmon 的主机上的本地端口。服务器的地址应在服务器名称和服务器端口中指定。为每个连接捕获的信息显示在由连接的本地端口标识的选项卡式面板中。

I have used this tool before, and it's light and easy to use. If you want a tutorial please check the following:

我以前用过这个工具,它轻便好用。如果您需要教程,请检查以下内容:

http://www.asjava.com/web-services/tcpmon-tutorial-web-service-debugging-tool/

http://www.asjava.com/web-services/tcpmon-tutorial-web-service-debugging-tool/

UPDATE.

更新。

TCPMon cannot monitor https traffic. Alternatively you can use SSLDump.

TCPMon 无法监控 https 流量。或者,您可以使用 SSLDump。

The main site of ssldump: http://www.rtfm.com/ssldump/Ssldump.htmlAn example of usage: http://blog.facilelogin.com/2010/11/ssl-debugging-part-ii-intercepting.html

ssldump 的主要站点:http: //www.rtfm.com/ssldump/Ssldump.html使用示例:http: //blog.facilelogin.com/2010/11/ssl-debugging-part-ii-intercepting 。 html