使用 Apache HttpClient 将原始字节添加到 post 参数

发布于 2024-10-07 11:54:05 字数 189 浏览 3 评论 0原文

有没有办法使用 Apache 的 HTTPClient 将原始字节添加到 post 参数中?我的动机是以特定编码流式传输流的原始字节,然后测试该数据到达服务器时会发生什么情况。

编辑:我注意到 Apache 有一个已弃用的方法来使用输入流添加到请求正文,因此我可以为其提供 ByteArrayInputStream,但是有什么更好/不弃用的方法吗?

Is there a way to add raw bytes to the post parameters using Apache's HTTPClient? My motivation is to stream the raw bytes of a stream in a particular encoding and then test what happens to that data when it gets to the server.

edit: I noticed that Apache has a deprecated method to add to the request body using an inputstream, so I could feed this a ByteArrayInputStream, but is there anything better/ not deprecated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风和你 2024-10-14 11:54:05

(您没有指定,所以我假设您使用的是 Apache HttpClient 版本 4.x)

我认为您无法将原始字节“添加”到正常创建请求的 POST 参数中。

但是,应该可以使用 HttpEntity 创建一个 PostMethod,其中包含合法参数以及一些错误编码的内容。您可能需要自己对内容进行格式化和编码,但您可以采取捷径,将有效参数放入 UrlEncodedFormEntity 中,然后使用 writeTo 方法将格式化/编码版本提取到 ByteArrayOutputStream 中。破解内容字节后,将它们转换为具有适当内容类型和编码参数的 ByteArrayEntity 实例。

会很乱...

(You didn't specify, so I'm assuming you are using Apache HttpClient version 4.x)

I don't think you can "add" raw bytes to an normally create request's POST parameters.

However, it should be possible to create a PostMethod with an HttpEntity which consists of legitimate parameters, plus some incorrectly encoded stuff. You may need to do the formatting and encoding of the content yourself, but you might be able to take a short cut by putting the valid parameters into a UrlEncodedFormEntity, and then using the writeTo method to extract the formatted/encoded version into a ByteArrayOutputStream. After you've hacked the content bytes, turn them into a ByteArrayEntity instance with the appropriate content type and encoding parameters.

It will be messy ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文