這是僅支持自Jsoup 1.8.2(2015年4月13日) 通過新的data(String, String, InputStream)
方法。
String url = "http://www......com/....php";
File file = new File("/path/to/file.ext");
Document document = Jsoup.connect(url)
.data("user", "user")
.data("password", "12345")
.data("email", "[email protected]")
.data("file", file.getName(), new FileInputStream(file))
.post();
// ...
在舊版本中,發送multipart/form-data
請求不被支持。最好的辦法是使用一個完整的HTTP客戶端,例如Apache HttpComponents Client。您最終可以將HTTP客戶端響應作爲String
,以便您可以將其提供給Jsoup#parse()
方法。
String url = "http://www......com/....php";
File file = new File("/path/to/file.ext");
MultipartEntity entity = new MultipartEntity();
entity.addPart("user", new StringBody("user"));
entity.addPart("password", new StringBody("12345"));
entity.addPart("email", new StringBody("[email protected]"));
entity.addPart("file", new InputStreamBody(new FileInputStream(file), file.getName()));
HttpPost post = new HttpPost(url);
post.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
String html = EntityUtils.toString(response.getEntity());
Document document = Jsoup.parse(html, url);
// ...