使用 Apache HttpClient 将原始字节添加到 post 参数
有没有办法使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(您没有指定,所以我假设您使用的是 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 anHttpEntity
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 aUrlEncodedFormEntity
, and then using thewriteTo
method to extract the formatted/encoded version into aByteArrayOutputStream
. After you've hacked the content bytes, turn them into aByteArrayEntity
instance with the appropriate content type and encoding parameters.It will be messy ...