diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java index 1cce58bd1..38b7f80d5 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java @@ -615,7 +615,7 @@ public class HttpUtil { * @return 参数Map */ public static Map> 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> 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> decodeParams(String paramsStr, Charset charset) { - final Map 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> decodeParams(String paramsStr, Charset charset, boolean isFormUrlEncoded) { + final Map queryMap = + UrlQuery.of(paramsStr, charset, true, isFormUrlEncoded).getQueryMap(); if (MapUtil.isEmpty(queryMap)) { return MapUtil.empty(); } diff --git a/hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java index 124ae2bfd..05555f58a 100644 --- a/hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java @@ -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)); } } } diff --git a/hutool-http/src/test/java/cn/hutool/http/HttpUtilTest.java b/hutool-http/src/test/java/cn/hutool/http/HttpUtilTest.java index fe9788dc9..e4532385e 100755 --- a/hutool-http/src/test/java/cn/hutool/http/HttpUtilTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/HttpUtilTest.java @@ -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); + } }