简体   繁体   中英

JSOUP + multipart/form-data response

In general, I need to send data to a site in the form of response multipart / form-data by JSOUP

As an example, take a simple form that sgeniriruet your query.

<form action=« localhost:8000 » method=«post» enctype=«multipart/form-data»
<input type=«text» name=«text» value=«text default»
<input type=«file» name=«file1»
<input type=«file» name=«file2»
Submit</button
</form

Post Response by browser:

>Request Headers Provisional headers are shown Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Content-Type:multipart/form-data;
boundary=----WebKitFormBoundaryjtkXVNw9YVG1H2P9 Origin:null
Upgrade-Insecure-Requests:1 User-Agent:Mozilla/5.0 (Windows NT 6.1;
WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106
Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id:8DCCE949-56FA-4AB0-81B7-DA2BC7960E5C
 
->Request Payload
------WebKitFormBoundaryjtkXVNw9YVG1H2P9 Content-Disposition: form-data; name=«text»

text default
------WebKitFormBoundaryjtkXVNw9YVG1H2P9 Content-Disposition: form-data; name=«file1»; filename="" Content-Type:
application/octet-stream

------WebKitFormBoundaryjtkXVNw9YVG1H2P9 Content-Disposition: form-data; name=«file2»; filename="" Content-Type:
application/octet-stream

------WebKitFormBoundaryjtkXVNw9YVG1H2P9--

I tried to create a similar request, but has not found the right way, so that the server received the request.

My code:

Map<String, String> responseMap= new HashMap<String, String>();
    String key1 = "------WebKitFormBoundary9A3GpeDAwfa0TBDK\r\n" +
         "Content-Disposition: form-data; name=\"text\"\r\n\r\n";
    String value1 = "text default";
    headersMap.put(key1, value1);

    String key2 = "\r\n------WebKitFormBoundary9A3GpeDAwfa0TBDK\r\n" +
        "Content-Disposition: form-data; name=\"doc_sma_ref_file\"; filename=\"\"" +
        "\r\nContent-Type: application/octet-stream\r\n\r\n";
    String value2 = "";
    headersMap.put(key2, value2);

    String key3 = "\r\n------WebKitFormBoundary9A3GpeDAwfa0TBDK\r\n" +
        "Content-Disposition: form-data; name=\"doc_val_ref_file\"; filename=\"\"" +
        "\r\nContent-Type: application/octet-stream\r\n\r\n";
    String value3 = "";
    headersMap.put(key3, value3);
    
    String key4 = "\r\n------WebKitFormBoundary9A3GpeDAwfa0TBDK--";
    String value4 = "";
    headersMap.put(key4, value4);

    Connection.Response resBGT = Jsoup.connect(URL)
        .header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary9A3GpeDAwfa0TBDK")
        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36")
        .followRedirects(true)
        .data(responseMap)
        .cookies(cookies)
        .ignoreHttpErrors(true)
        .timeout(15000)
        .method(Connection.Method.POST)
        .execute();

Maybe someone has experience in this matter. If you please send the right path. Perhaps there is an opportunity to see a request generated jsoup

You can do it using the third parameter of Connection.data :

File file1 = new File("C:/dir/file1.txt");
File file2 = new File("C:/dir/file2.txt");
FileInputStream fs1 = new FileInputStream(file1);
FileInputStream fs2 = new FileInputStream(file2);
Connection.Response response = Jsoup.connect("http://www.example.com")
        .data("text", "value")
        .data("file1", "filename", fs1)
        .data("file2", "filename", fs2)
        .userAgent("Mozilla")
        .method(Method.POST)
        .execute();

//Handle your response...

Before, JSoup 1.12.1, its was not possible to send form boundaries automatically unless files were involded in the form posting.

Since 1.12.1, Jsoup can generate form boundaries when the Content-Type header is set to "multipart/form-data" . It is no more necessary to add files in the form data for generating mime boundary automatically.

Document doc = Jsoup.connect(echoUrl)
            .header("Content-Type", "multipart/form-data")
            .userAgent(browserUa)
            .data("uname", "Jsoup", "uname", "Jonathan", "百", "度一下")
            .post();

As you can see in the above code, no files are used, however JSoup will handle form boundary on the fly because the content type is multipart/form-data .

Sources :

  • Issue 788 it I can send request post multipart/form-data without attachment of the file
  • Commit 56ea479 Allow multipart form submission by setting header; automatically add boundary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM