当前位置:  开发笔记 > 编程语言 > 正文

JSOUP + multipart/form-data响应

如何解决《JSOUP+multipart/form-data响应》经验,为你挑选了1个好方法。

下午好,

通常,我需要通过JSOUP以响应multipart/form-data的形式将数据发送到站点

例如,采用sgeniriruet您的查询的简单形式.

Submit

通过浏览器发布回复:

>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--

我试图创建一个类似的请求,但没有找到正确的方法,以便服务器收到请求.

我的代码:

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();

也许有人有这方面的经验.如果你请发送正确的路径.也许有机会看到生成jsoup的请求

谢谢你的帮助.



1> Davide Pasto..:

你可以使用第三个参数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...

推荐阅读
携手相约幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有