如何查看 MultipartForm 请求的内容?

发布于 2024-10-12 15:44:01 字数 565 浏览 8 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(4

远昼 2024-10-19 15:44:01

使用 org.apache.http.entity.mime.MultipartEntity writeTo(java.io.OutputStream) 方法将内容写入 java.io. OutputStream,然后将该流转换为 Stringbyte[]

// import java.io.ByteArrayOutputStream;
// import org.apache.http.entity.mime.MultipartEntity;
// ...
// MultipartEntity entity = ...;
// ...

ByteArrayOutputStream out = new ByteArrayOutputStream(entity.getContentLength());

// write content to stream
entity.writeTo(out);

// either convert stream to string
String string = out.toString();

// or convert stream to bytes
byte[] bytes = out.toByteArray();

注意:这仅适用于小到足以读入内存的多部分实体,并且小于 Java 中字节数组的最大大小 2Gb。

Use the org.apache.http.entity.mime.MultipartEntity writeTo(java.io.OutputStream) method to write the content to an java.io.OutputStream, and then convert that stream to a String or byte[]:

// import java.io.ByteArrayOutputStream;
// import org.apache.http.entity.mime.MultipartEntity;
// ...
// MultipartEntity entity = ...;
// ...

ByteArrayOutputStream out = new ByteArrayOutputStream(entity.getContentLength());

// write content to stream
entity.writeTo(out);

// either convert stream to string
String string = out.toString();

// or convert stream to bytes
byte[] bytes = out.toByteArray();

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.

临风闻羌笛 2024-10-19 15:44:01

以下肯定会有帮助:

ByteArrayOutputStream content = new ByteArrayOutputStream();
httpEntity.writeTo(content);
logger.info("Calling "+url+" with data: "+content.toString());

与第一个答案相比,上面有一个修复,不需要将任何参数传递给 ByteArrayOutputStream 构造函数。

Following would help for sure:

ByteArrayOutputStream content = new ByteArrayOutputStream();
httpEntity.writeTo(content);
logger.info("Calling "+url+" with data: "+content.toString());

The above has a fix in comparison to the first answer, there is no need to pass any parameter to ByteArrayOutputStream constructor.

财迷小姐 2024-10-19 15:44:01

你不知道内容吗?不过,您是通过提供 sharedValue 来构建 StringBody 的。那么,它与 sharedValue 有何不同。

Do you not know the content? Although, you are building the StringBody by supplying sharedValue. So, how could it be different than sharedValue.

栩栩如生 2024-10-19 15:44:01

我已经通过以下代码打印了多部分请求,您可以尝试像

ByteArrayOutputStream bytes = new ByteArrayOutputStream();

entity.writeTo(bytes);

String content = bytes.toString();

Log.e("MultiPartEntityRequest:",content);

I have printed the Multipart request by following code, You can try like

ByteArrayOutputStream bytes = new ByteArrayOutputStream();

entity.writeTo(bytes);

String content = bytes.toString();

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