mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
修复HttpUtil.decodeParams方法,判断是否x-www-form-urlencoded的模式
This commit is contained in:
parent
21eecdc760
commit
5e6804536e
@ -615,7 +615,7 @@ public class HttpUtil {
|
||||
* @return 参数Map
|
||||
*/
|
||||
public static Map<String, List<String>> decodeParams(String paramsStr, String charset) {
|
||||
return decodeParams(paramsStr, CharsetUtil.charset(charset));
|
||||
return decodeParams(paramsStr, charset, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -624,10 +624,34 @@ public class HttpUtil {
|
||||
* @param paramsStr 参数字符串(或者带参数的Path)
|
||||
* @param charset 字符集
|
||||
* @return 参数Map
|
||||
*/
|
||||
public static Map<String, List<String>> decodeParams(String paramsStr, String charset, boolean isFormUrlEncoded) {
|
||||
return decodeParams(paramsStr, CharsetUtil.charset(charset), isFormUrlEncoded);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将URL QueryString参数解析为Map
|
||||
*
|
||||
* @param paramsStr 参数字符串(或者带参数的Path)
|
||||
* @param charset 字符集
|
||||
* @return 参数Map
|
||||
* @since 5.2.6
|
||||
*/
|
||||
public static Map<String, List<String>> decodeParams(String paramsStr, Charset charset) {
|
||||
final Map<CharSequence, CharSequence> queryMap = UrlQuery.of(paramsStr, charset).getQueryMap();
|
||||
return decodeParams(paramsStr, charset, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将URL参数解析为Map(也可以解析Post中的键值对参数)
|
||||
*
|
||||
* @param paramsStr 参数字符串(或者带参数的Path)
|
||||
* @param charset 字符集
|
||||
* @param isFormUrlEncoded 是否为x-www-form-urlencoded模式,此模式下空格会编码为'+'
|
||||
* @return 参数Map
|
||||
*/
|
||||
public static Map<String, List<String>> decodeParams(String paramsStr, Charset charset, boolean isFormUrlEncoded) {
|
||||
final Map<CharSequence, CharSequence> queryMap =
|
||||
UrlQuery.of(paramsStr, charset, true, isFormUrlEncoded).getQueryMap();
|
||||
if (MapUtil.isEmpty(queryMap)) {
|
||||
return MapUtil.empty();
|
||||
}
|
||||
|
@ -331,7 +331,7 @@ public class HttpServerRequest extends HttpServerBase {
|
||||
//解析URL中的参数
|
||||
final String query = getQuery();
|
||||
if(StrUtil.isNotBlank(query)){
|
||||
this.paramsCache.putAll(HttpUtil.decodeParams(query, charset));
|
||||
this.paramsCache.putAll(HttpUtil.decodeParams(query, charset, false));
|
||||
}
|
||||
|
||||
// 解析multipart中的参数
|
||||
@ -341,7 +341,7 @@ public class HttpServerRequest extends HttpServerBase {
|
||||
// 解析body中的参数
|
||||
final String body = getBody();
|
||||
if(StrUtil.isNotBlank(body)){
|
||||
this.paramsCache.putAll(HttpUtil.decodeParams(body, charset));
|
||||
this.paramsCache.putAll(HttpUtil.decodeParams(body, charset, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package cn.hutool.http;
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import cn.hutool.core.net.url.UrlBuilder;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import org.junit.Assert;
|
||||
@ -378,4 +380,20 @@ public class HttpUtilTest {
|
||||
final HttpRequest request = HttpRequest.of(url).method(Method.GET);
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpParameterDecodeTest () {
|
||||
String test = "this is test测试";
|
||||
int port = NetUtil.getUsableLocalPort();
|
||||
HttpUtil.createServer(port)
|
||||
.addAction("/formEncoded", (req, resp) -> resp.write(req.getParam("test")))
|
||||
.addAction("/urlEncoded", (req, resp) -> resp.write(req.getParam("test")))
|
||||
.start();
|
||||
String resp = HttpUtil.createPost(String.format("http://localhost:%s/formEncoded", port))
|
||||
.form("test", test).execute().body();
|
||||
Assert.assertEquals("Form请求参数解码", test, resp);
|
||||
String urlGet = UrlBuilder.of(String.format("http://localhost:%s/urlEncoded", port)).addQuery("test", test).build();
|
||||
String resp2 = HttpUtil.createGet(urlGet).execute().body();
|
||||
Assert.assertEquals("QueryString请求参数编码", test, resp2);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user