diff --git a/CHANGELOG.md b/CHANGELOG.md index 813006991..25387ec00 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.28(2024-04-07) +# 5.8.28(2024-04-08) ### 🐣新特性 * 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee) @@ -10,6 +10,7 @@ * 【extra 】 设置jsch登录认证方式,跳过Kerberos身份验证(pr#3530@Github) ### 🐞Bug修复 +* 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.27(2024-03-29) 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 c699710d0..45609ccb8 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java @@ -5,6 +5,7 @@ import cn.hutool.core.convert.Convert; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.StreamProgress; +import cn.hutool.core.lang.Console; import cn.hutool.core.map.MapUtil; import cn.hutool.core.net.RFC3986; import cn.hutool.core.net.url.UrlQuery; @@ -693,7 +694,8 @@ public class HttpUtil { * @return 合成后的URL */ public static String urlWithFormUrlEncoded(String url, Map form, Charset charset) { - return urlWithForm(url, toParams(form, charset, true), charset, true); + // issue#3536,urlWithForm会对参数进行一次编码,因此toParams无需编码 + return urlWithForm(url, toParams(form, null, false), charset, true); } /** diff --git a/hutool-http/src/test/java/cn/hutool/http/Issue3536Test.java b/hutool-http/src/test/java/cn/hutool/http/Issue3536Test.java new file mode 100644 index 000000000..29bbbacee --- /dev/null +++ b/hutool-http/src/test/java/cn/hutool/http/Issue3536Test.java @@ -0,0 +1,32 @@ +package cn.hutool.http; + +import cn.hutool.core.util.CharsetUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class Issue3536Test { + + @Test + public void urlWithFormUrlEncodedTest() { + String url = "https://hutool.cn/test"; + final Map paramMap = new HashMap<>(); + paramMap.put("redirect_uri", "https://api.hutool.cn/v1/test"); + paramMap.put("scope", "a,b,c你"); + + final String s = HttpUtil.urlWithFormUrlEncoded(url, paramMap, CharsetUtil.CHARSET_UTF_8); + Assert.assertEquals("https://hutool.cn/test?scope=a,b,c%E4%BD%A0&redirect_uri=https://api.hutool.cn/v1/test", s); + } + + @Test + public void toParamsTest() { + final Map paramMap = new HashMap<>(); + paramMap.put("redirect_uri", "https://api.hutool.cn/v1/test"); + paramMap.put("scope", "a,b,c你"); + + final String params = HttpUtil.toParams(paramMap, CharsetUtil.CHARSET_UTF_8, true); + Assert.assertEquals("scope=a%2Cb%2Cc%E4%BD%A0&redirect_uri=https%3A%2F%2Fapi.hutool.cn%2Fv1%2Ftest", params); + } +}