如何查看 MultipartForm 请求的内容?
我正在使用 Apache HTTPClient 4。我正在做非常正常的多部分工作,如下所示:
val entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("filename", new FileBody(new File(fileName), "application/zip").asInstanceOf[ContentBody])
entity.addPart("shared", new StringBody(sharedValue, "text/plain", Charset.forName("UTF-8")));
val post = new HttpPost(uploadUrl);
post.setEntity(entity);
我想在发送实体(或帖子,无论什么)之前查看它的内容。但是,该特定方法并未实现:
entity.getContent() // not defined for MultipartEntity
我怎样才能看到我发布的内容?
I am using Apache HTTPClient 4. I am doing very normal multipart stuff like this:
val entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("filename", new FileBody(new File(fileName), "application/zip").asInstanceOf[ContentBody])
entity.addPart("shared", new StringBody(sharedValue, "text/plain", Charset.forName("UTF-8")));
val post = new HttpPost(uploadUrl);
post.setEntity(entity);
I want to see the contents of the entity (or post, whatever) before I send it. However, that specific method is not implemented:
entity.getContent() // not defined for MultipartEntity
How can I see what I am posting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 org.apache.http.entity.mime.MultipartEntity writeTo(java.io.OutputStream) 方法将内容写入
java.io. OutputStream
,然后将该流转换为String
或byte[]
:注意:这仅适用于小到足以读入内存的多部分实体,并且小于 Java 中字节数组的最大大小 2Gb。
Use the
org.apache.http.entity.mime.MultipartEntity
writeTo(java.io.OutputStream) method to write the content to anjava.io.OutputStream
, and then convert that stream to aString
orbyte[]
:Note: this only works for multipart entities small enough to be read into memory, and smaller than 2Gb which is the maximum size of a byte array in Java.
以下肯定会有帮助:
与第一个答案相比,上面有一个修复,不需要将任何参数传递给 ByteArrayOutputStream 构造函数。
Following would help for sure:
The above has a fix in comparison to the first answer, there is no need to pass any parameter to ByteArrayOutputStream constructor.
你不知道内容吗?不过,您是通过提供
sharedValue
来构建StringBody
的。那么,它与sharedValue
有何不同。Do you not know the content? Although, you are building the
StringBody
by supplyingsharedValue
. So, how could it be different thansharedValue
.我已经通过以下代码打印了多部分请求,您可以尝试像
I have printed the Multipart request by following code, You can try like