gpt4 book ai didi

java - JSOUP + 多部分/表单数据响应

转载 作者:行者123 更新时间:2023-12-01 16:54:52 24 4
gpt4 key购买 nike

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

作为示例,采用一个简单的表单来执行您的查询。

<表单操作=« localhost:8000 » method=«post» enctype=«multipart/form-data»
<输入类型=«文本»名称=«文本»值=«文本默认»
<输入类型=«文件»名称=«文件1»
<输入类型=«文件»名称=«文件2»
提交

通过浏览器发布响应:

>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

最佳答案

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

关于java - JSOUP + 多部分/表单数据响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34570836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com