Apache HttpClient 制作多部分表单帖子
我对 HttpClient 很陌生,而且我发现缺乏(和/或明显不正确)文档非常令人沮丧。我正在尝试使用 Apache Http Client 实现以下帖子(如下所列),但不知道如何实际执行。下周我将埋头于文档中,但也许更有经验的 HttpClient 编码员可以更快地给我答案。
邮政:
Content-Type: multipart/form-data; boundary=---------------------------1294919323195
Content-Length: 502
-----------------------------1294919323195
Content-Disposition: form-data; name="number"
5555555555
-----------------------------1294919323195
Content-Disposition: form-data; name="clip"
rickroll
-----------------------------1294919323195
Content-Disposition: form-data; name="upload_file"; filename=""
Content-Type: application/octet-stream
-----------------------------1294919323195
Content-Disposition: form-data; name="tos"
agree
-----------------------------1294919323195--
I'm pretty green to HttpClient and I'm finding the lack of (and or blatantly incorrect) documentation extremely frustrating. I'm trying to implement the following post (listed below) with Apache Http Client, but have no idea how to actually do it. I'm going to bury myself in documentation for the next week, but perhaps more experienced HttpClient coders could get me an answer sooner.
Post:
Content-Type: multipart/form-data; boundary=---------------------------1294919323195
Content-Length: 502
-----------------------------1294919323195
Content-Disposition: form-data; name="number"
5555555555
-----------------------------1294919323195
Content-Disposition: form-data; name="clip"
rickroll
-----------------------------1294919323195
Content-Disposition: form-data; name="upload_file"; filename=""
Content-Type: application/octet-stream
-----------------------------1294919323195
Content-Disposition: form-data; name="tos"
agree
-----------------------------1294919323195--
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 HttpMime 库 中的 MultipartEntityBuilder 来执行您想要的请求。
在我的项目中,我这样做:
希望这会有所帮助。
(更新了这篇文章以使用 MultipartEntityBuilder 而不是已弃用的 MultipartEntity,使用 @mtomy 代码作为示例)
Use MultipartEntityBuilder from the HttpMime library to perform the request you want.
In my project I do that this way:
Hope this will help.
(Updated this post to use MultipartEntityBuilder instead of deprecated MultipartEntity, using @mtomy code as the example)
中的示例代码
这是 HttpClient 4.3.x http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost。 java
的子项目
要使用 MultipartEntityBuilder 类,需要 httpmime,它是 HttpClient HttpClient 4.3.x
: http://hc.apache.org/httpcomponents-client-4.3.x/index.html
httpmime 4.3.x:
< a href="http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/dependency-info.html">http://hc.apache.org/httpcomponents-client-4.3.x/httpmime /dependency-info.html
Here is the sample code in HttpClient 4.3.x
http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java
To use the class MultipartEntityBuilder, you need httpmime, which is a sub project of HttpClient
HttpClient 4.3.x:
http://hc.apache.org/httpcomponents-client-4.3.x/index.html
httpmime 4.3.x:
http://hc.apache.org/httpcomponents-client-4.3.x/httpmime/dependency-info.html
如果使用 org.apache.commons.httpclient.HttpClient 包,也许可以帮助你!
if use org.apache.commons.httpclient.HttpClient package, maybe that can help you!