Java:在发送之前显示HttpURLConnection的请求 [英] Java: Display request of an HttpURLConnection before sending
问题描述
我想使用 HttpURLConnection
对服务器进行一些API调用。但请求不成功,返回:
I want to make some API calls to a server using HttpURLConnection
. But the requests are not successful, returning:
<error>
<http_status>400 Bad Request</http_status>
<message>Unexpected request Content-Type header ''. Expecting 'application/x-www-form-urlencoded'.</message>
</error>
所以我想查看发送到服务器的真实内容。根据实际内容,我的意思是确切的HTTP请求。
So I want to check what the "real" content is that is being sent to the server. By real content I mean the exact HTTP request.
我有什么想法可以看到这个?
Any ideas how I can see this?
编辑:
基于这里的第一个答案我应该澄清我的问题:我想避免使用像HTTP嗅探器或任何东西的外部程序,我希望有一个功能或属性或其他什么保存我正在寻找的信息。
Based on the first answers here I should clarify my problem: I want to avoid using an external program like HTTP sniffer or anything and I was hoping that there is a function or a property or whatever that holds the information I am looking for.
如果不是这样,有人知道是否可以手动重建这些信息(例如通过调用getRequestMethod等几个函数()等。)
If that is not the case, does someone know if this information can be manually rebuilt (for example by calling several functions like getRequestMethod(), etc.)
我经常遇到这个问题,所以我自己努力建立这样的功能。只需知道如何:)
I am facing this problem kinda often so that it's worth the effort to build such functionality myself. Just need to know how :)
推荐答案
您可以通过启用带有
-Djava.util.logging.config.file=logging.properties
并放入logging.properties(默认情况下在JRE_HOME \lib中)以下属性
and put in logging.properties (by default in JRE_HOME\lib) the following property
sun.net.www.protocol.http.HttpURLConnection.level = ALL
这篇关于Java:在发送之前显示HttpURLConnection的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!