JSOUP + multipart / form-data响应

时间:2016-01-02 21:17:48

标签: java post jsoup multipartform-data

下午好,

一般情况下,我需要通过JSOUP以响应multipart / form-data的形式向站点发送数据

举一个例子,采用一个简单的形式来sgeniriruet您的查询。

< 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»
提交< /按钮
< /形式

浏览器发布回复:

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

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

感谢您的帮助。

2 个答案:

答案 0 :(得分:5)

您可以使用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...

答案 1 :(得分:1)

在JSoup 1.12.1之前,除非文件被包含在表单发布中,否则无法自动发送表单边界。

从1.12.1开始,当Content-Type标头设置为"multipart/form-data"时,Jsoup可以生成表单边界。不再需要在表单数据中添加文件以自动生成mime边界。

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

您可以在上面的代码中看到,没有使用任何文件,但是JSoup会动态处理表单边界,因为内容类型为multipart/form-data

来源:

  • Issue 788,我可以发送请求后的多部分/表单数据而无需附加文件
  • Commit 56ea479通过设置标头允许多部分表单提交;自动添加边界。