Java:在发送之前显示 HttpURLConnection 的请求 [英] Java: Display request of an HttpURLConnection before sending

查看:32
本文介绍了Java:在发送之前显示 HttpURLConnection 的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 HttpURLConnection 对服务器进行一些 API 调用.但是请求不成功,返回:

<http_status>400 错误请求</http_status><message>意外的请求内容类型标头''.期待application/x-www-form-urlencoded".</message></错误>

所以我想检查发送到服务器的真实"内容是什么.我所说的真实内容是指确切的 HTTP 请求.

任何想法我怎么能看到这个?

基于这里的第一个答案,我应该澄清我的问题:我想避免使用外部程序,如 HTTP 嗅探器或任何东西,我希望有一个函数或属性或任何包含我正在寻找的信息的东西.

如果不是这种情况,有人知道是否可以手动重建此信息(例如通过调用 getRequestMethod() 等多个函数)

我经常遇到这个问题,因此值得自己努力构建这样的功能.只需要知道如何:)

解决方案

您可以通过使用

启用 java.logging 将 HttpURLConnection 置于调试模式

-Djava.util.logging.config.file=logging.properties

并放入 logging.properties(默认在 JRE_HOMElib 中)以下属性

sun.net.www.protocol.http.HttpURLConnection.level = ALL

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>

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?

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.

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 :)

解决方案

You can put the HttpURLConnection in debug mode by enabling java.logging with

-Djava.util.logging.config.file=logging.properties

and put in logging.properties (by default in JRE_HOMElib) the following property

sun.net.www.protocol.http.HttpURLConnection.level = ALL

这篇关于Java:在发送之前显示 HttpURLConnection 的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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