Hutool_HttpRequest_POST_GET编码转码问题

POST

实现form表单中文不编码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
String result = HttpRequest.post(AEP_GATE_URL.concat(url))
.header(AEP_NONCE,uuid)
.header(AEP_APPKEY,appKeyValue)
.header(AEP_TIMESTAMP,timestamp)
.header(AEP_SIGNATURE,hmac256Encode(getBody(uuid, timestamp)))
.body(MapUtil.sortJoin(fromcs, "&", "=",true)) // 关键
.timeout(20000)//超时,毫秒
// 可以使用抓包工具,查看是否调用请求参数对的
.setHttpProxy("127.0.0.1", 8888)
.execute().body();
// 完整请求头
// POST //xxx/xxx HTTP/1.1
// x-aep-timestamp: 1689823636846
// Accept: text/html,application/json,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
// x-aep-signature: 32f04bdbc64a69a89aa9706e2d2ac60ee192e3c2b4f331d3e60f03d460946626
// User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 // Hutool
// x-aep-appkey: 1633010746407477248
// Accept-Encoding: gzip, deflate
// Accept-Language: zh-CN,zh;q=0.8
// x-aep-nonce: d652ec8b-79ad-4981-83ba-de668dbd63f5
// Content-Type: application/x-www-form-urlencoded;charset=UTF-8
// Cache-Control: no-cache
// Pragma: no-cache
// Host: 10.1.1.5:8099
// Content-Length: 19
// Connection: keep-alive
//
// userName=test测试

指定编码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
String result = HttpRequest.post(AEP_GATE_URL.concat(url))
.header(AEP_NONCE,uuid)
.header(AEP_APPKEY,appKeyValue)
.header(AEP_TIMESTAMP,timestamp)
.header(AEP_SIGNATURE,hmac256Encode(getBody(uuid, timestamp)))
.contentType("application/x-www-form-urlencoded;charset=GBK") // 关键
.form(fromcs)
.timeout(20000)//超时,毫秒
// 可以使用抓包工具,查看是否调用请求参数对的
.setHttpProxy("127.0.0.1", 8888)
.execute().body();

// 完整请求头
// POST //xxx/xxx HTTP/1.1
//x-aep-timestamp: 1689824206833
//Accept: text/html,application/json,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
//x-aep-signature: da5c218510eb47a3fb6a8674e2e5905a0d0bf6e58f2d78118836f9da4653d613
//User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 //Hutool
//x-aep-appkey: 1633010746407477248
//Accept-Encoding: gzip, deflate
//Accept-Language: zh-CN,zh;q=0.8
//x-aep-nonce: 1c7fe5d4-a001-43bd-bd47-79968cc3af9c
//Content-Type: application/x-www-form-urlencoded;charset=GBK
//Cache-Control: no-cache
//Pragma: no-cache
//Host: 10.1.1.5:8099
//Content-Length: 31
//Connection: keep-alive
//
//userName=test%E6%B5%8B%E8%AF%95
1
2
3
4
String result2= HttpUtil.get(phone.getUrl(),CharsetUtil.CHARSET_GBK);
//发送http请求
String rsp = HttpRequest.post(url).contentType("application/x-www-form-urlencoded;charset=GBK").charset("GBK")
.form(map).execute().body();