如何使用 JSoup 发布文件?
我正在使用 JSoup 使用以下代码发布值:
Document document = Jsoup.connect("http://www......com/....php")
.data("user","user","password","12345","email","[email protected]")
.method(Method.POST)
.execute()
.parse();
现在我也想提交一个文件。就像带有文件字段的表单一样。 这可能吗?如果是比怎么样?
I`m using the following code post values using JSoup:
Document document = Jsoup.connect("http://www......com/....php")
.data("user","user","password","12345","email","[email protected]")
.method(Method.POST)
.execute()
.parse();
And now I want to submit a file, too. Like a form with a file field.
Is this possible ? If is than how ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅自 Jsoup 1.8.2(2015 年 4 月 13 日)起支持此功能
通过新的
data(String, String, InputStream)
方法。在旧版本中,不支持发送
multipart/form-data
请求。最好的选择是使用一个完整的 HTTP 客户端,例如 Apache HttpComponents Client。您最终可以获得String
形式的 HTTP 客户端响应,以便可以将其提供给Jsoup#parse()
方法。This is only supported since Jsoup 1.8.2 (Apr 13, 2015)
via the new
data(String, String, InputStream)
method.In older versions, sending
multipart/form-data
requests is not supported. Your best bet is using a fullworthy HTTP client for this, such as Apache HttpComponents Client. You can ultimately get the HTTP client response asString
so that you can feed it toJsoup#parse()
method.接受的答案有效并且在撰写本文时是正确的,但从那时起 JSoup 已经发展并且 自版本 1.8.2 起,可以将文件作为多部分表单的一部分发送:
The accepted answer works and was correct at the time of writing, but since then JSoup has evolved and since version 1.8.2 it is possible to send files as part of multipart forms:
这篇文章引导我走上了正确的道路,但我必须调整发布的答案才能使我的用例发挥作用。这是我的代码:
基本区别是我首先登录站点,保留响应中的 cookie (
conn
),然后将其用于后续的文件上传。希望对大家有帮助。
This post led me to the right path but I had to tweak the posted answers to make my use case work. Here's my code:
The basic difference is that I first login to the site, retain the cookie from the response (
conn
) and then use it for the subsequent upload of the file.Hope it helps guys.