JavaRush /Java Blog /Random EN /JSOUP + multipart/form-data response
wildsinner
Level 41
Самара

JSOUP + multipart/form-data response

Published in the Random EN group
Good afternoon, In general, you need to send data to the site in the form of a response in the form multipart/form-data. It is not difficult to intercept a request generated by a browser, but executing the same one using JSOUP is difficult. As an example, you can take the simplest form that will generate the desired request.
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 in various ways to generate a similar request with different options, and settled on this one, but so far “dancing with tambourines” does not produce results. Perhaps someone has experience in this matter. If you don't mind, please direct me on the right path. It is also possible that there is a way to intercept a request generated by JSOUP, which in principle would also allow us to move forward. Thank you Map responseMap= new HashMap (); 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();
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION