Điều này chỉ được hỗ trợ kể từ Jsoup 1.8.2 (ngày 13 tháng 4 năm 2015) thông qua phương thức data(String, String, InputStream)
mới.
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();
// ...
Trong các phiên bản cũ hơn, yêu cầu gửi multipart/form-data
không được hỗ trợ. Đặt cược tốt nhất của bạn là sử dụng ứng dụng HTTP đủ điều kiện cho điều này, chẳng hạn như Apache HttpComponents Client. Cuối cùng, bạn có thể nhận được phản hồi của ứng dụng khách HTTP là String
để bạn có thể đưa nó đến phương thức 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);
// ...