修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题

This commit is contained in:
Looly 2024-04-08 17:31:25 +08:00
parent 983eccf3e6
commit 9a6ac21798
3 changed files with 37 additions and 2 deletions

View File

@ -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)

View File

@ -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<String, Object> form, Charset charset) {
return urlWithForm(url, toParams(form, charset, true), charset, true);
// issue#3536urlWithForm会对参数进行一次编码因此toParams无需编码
return urlWithForm(url, toParams(form, null, false), charset, true);
}
/**

View File

@ -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<String, Object> 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<String, Object> 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);
}
}