JSOUP +多部分/表单数据响应 [英] JSOUP + multipart/form-data response
问题描述
通常,我需要通过JSOUP以响应multipart/form-data的形式将数据发送到站点
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 »
<输入类型=文本"名称=文本"值=文本默认值"
<输入类型=«文件»名称=«文件1»
<输入类型=«文件»名称=«文件2»
提交</按钮
</form
<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
通过浏览器发布回复:
>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.
我的代码:
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
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
推荐答案
您可以使用Connection.data
的第三个参数进行操作:
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...
这篇关于JSOUP +多部分/表单数据响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!