Java:在发送之前显示 HttpURLConnection 的请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3428341/
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
Java: Display request of an HttpURLConnection before sending
提问by Hirnhamster
I want to make some API calls to a server using HttpURLConnection
. But the requests are not successful, returning:
我想使用HttpURLConnection
. 但是请求不成功,返回:
<error>
<http_status>400 Bad Request</http_status>
<message>Unexpected request Content-Type header ''. Expecting 'application/x-www-form-urlencoded'.</message>
</error>
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.
所以我想检查发送到服务器的“真实”内容是什么。我所说的真实内容是指确切的 HTTP 请求。
Any ideas how I can see this?
任何想法我怎么能看到这个?
Edit: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.
编辑:基于这里的第一个答案,我应该澄清我的问题:我想避免使用外部程序,如 HTTP 嗅探器或任何东西,我希望有一个函数或属性或任何包含我正在寻找的信息的东西。
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.)
如果不是这种情况,是否有人知道是否可以手动重建此信息(例如通过调用 getRequestMethod() 等多个函数)
I am facing this problem kinda often so that it's worth the effort to build such functionality myself. Just need to know how :)
我经常遇到这个问题,因此值得自己努力构建这样的功能。只需要知道如何:)
采纳答案by RealHowTo
You can put the HttpURLConnection in debug mode by enabling java.logging with
您可以通过启用 java.logging 将 HttpURLConnection 置于调试模式
-Djava.util.logging.config.file=logging.properties
and put in logging.properties (by default in JRE_HOME\lib) the following property
并放入 logging.properties(默认在 JRE_HOME\lib 中)以下属性
sun.net.www.protocol.http.HttpURLConnection.level = ALL
回答by Tassos Bassoukos
Use something like tcpdump, which can dump the actual network packets that are emitted or received by your computer.
使用 tcpdump 之类的东西,它可以转储计算机发出或接收的实际网络数据包。
回答by Mike Baranczak
tcpdump will work, but it can be hard to make it do what you want. NetCat is more user-friendly (here's the project page: http://netcat.sourceforge.net/- most Unix platforms already include it).
tcpdump 可以工作,但很难让它做你想做的事。NetCat 更加用户友好(这里是项目页面:http: //netcat.sourceforge.net/- 大多数 Unix 平台已经包含它)。
nc -l 9999
This will listen on TCP port 9999, and when an HTTP client connects, it'll print out the full text of the request.
这将侦听 TCP 端口 9999,并且当 HTTP 客户端连接时,它将打印出请求的全文。