开发者

How to post files using JSoup?

开发者 https://www.devze.com 2023-04-04 02:58 出处:网络
I`m using the following code post values using JSoup: Document document = Jsoup.connect(\"http://www......com/....php\")

I`m using the following code post values using JSoup:

Document document = Jsoup.connect("http://www......com/....php")
                    .data("user","user","password","12345","email","info@tutorialswindow.com")
                    .method(Met开发者_运维问答hod.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 ?


This is only supported since Jsoup 1.8.2 (Apr 13, 2015) via the new data(String, String, InputStream) method.

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", "info@tutorialswindow.com")
    .data("file", file.getName(), new FileInputStream(file))
    .post();
// ...

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 as String so that you can feed it to Jsoup#parse() method.

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("info@tutorialswindow.com"));
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);
// ...


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:

File file1 = new File("/path/to/file");
FileInputStream fs1 = new FileInputStream(file1);

Connection.Response response = Jsoup.connect("http://www......com/....php")
    .data("user","user","password","12345","email","info@tutorialswindow.com")            
    .data("file1", "filename", fs1)
    .method(Method.POST)
    .execute();


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:

        FileInputStream fs = new FileInputStream(fileToSend);
        Connection conn = Jsoup.connect(baseUrl + authUrl)
                .data("username",username)
                .data("password",password);
        Document document = conn.post();

        System.out.println("Login successfully! Session Cookie: " + conn.response().cookies());


        System.out.println("Attempting to upload file...");
        document = Jsoup.connect(baseUrl + uploadUrl)
                .data("file",fileToSend.getName(),fs)
                .cookies(conn.response().cookies())
                .post();

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消