From 0986e60a7ce783630a6886b1bd64ff8d3b267c72 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 1 Apr 2022 12:10:09 +0800 Subject: [PATCH] fix bug --- CHANGELOG.md | 2 ++ .../java/cn/hutool/core/net/url/UrlBuilder.java | 2 +- .../java/cn/hutool/core/net/UrlBuilderTest.java | 16 ++++++++++++++++ .../main/java/cn/hutool/http/HttpRequest.java | 6 ++++-- .../java/cn/hutool/http/HttpRequestTest.java | 15 +++++++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 418be3f2b..2f0b81aa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### ❌不兼容特性 * 【extra 】 【可能兼容问题】BeanCopierCache的key结构变更 * 【http 】 【可能兼容问题】HttpInterceptor增加泛型标识,HttpRequest中配置汇总于HttpConfig +* 【core 】 【可能兼容问题】UrlQuery.addQuery参数2从String变更为Object ### 🐣新特性 * 【core 】 MapUtil增加entry、ofEntries方法 @@ -21,6 +22,7 @@ * 【core 】 IdcardUtil#getCityCodeByIdCard位数问题(issue#2224@Github) * 【core 】 修复urlWithParamIfGet函数逻辑问题(issue#I50IUD@Gitee) * 【core 】 修复IoUtil.readBytes限制长度读取问题(issue#2230@Github) +* 【http 】 修复HttpRequest中编码对URL无效的问题(issue#I50NHQ@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/net/url/UrlBuilder.java b/hutool-core/src/main/java/cn/hutool/core/net/url/UrlBuilder.java index 0a9adc1a8..2332e0f3d 100644 --- a/hutool-core/src/main/java/cn/hutool/core/net/url/UrlBuilder.java +++ b/hutool-core/src/main/java/cn/hutool/core/net/url/UrlBuilder.java @@ -399,7 +399,7 @@ public final class UrlBuilder implements Builder { * @param value 值 * @return this */ - public UrlBuilder addQuery(String key, String value) { + public UrlBuilder addQuery(String key, Object value) { if (StrUtil.isEmpty(key)) { return this; } diff --git a/hutool-core/src/test/java/cn/hutool/core/net/UrlBuilderTest.java b/hutool-core/src/test/java/cn/hutool/core/net/UrlBuilderTest.java index 809bf483c..16ac46ed7 100644 --- a/hutool-core/src/test/java/cn/hutool/core/net/UrlBuilderTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/net/UrlBuilderTest.java @@ -9,6 +9,8 @@ import org.junit.Test; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.LinkedHashMap; public class UrlBuilderTest { @@ -389,4 +391,18 @@ public class UrlBuilderTest { final String build = UrlBuilder.of(url, null).build(); Assert.assertEquals(url, build); } + + @Test + public void issueI50NHQTest(){ + String url = "http://127.0.0.1/devicerecord/list"; + HashMap params = new LinkedHashMap<>(); + params.put("start", "2022-03-31 00:00:00"); + params.put("end", "2022-03-31 23:59:59"); + params.put("page", 1); + params.put("limit", 10); + + final UrlBuilder builder = UrlBuilder.of(url); + params.forEach(builder::addQuery); + Assert.assertEquals("http://127.0.0.1/devicerecord/list?start=2022-03-31%2000:00:00&end=2022-03-31%2023:59:59&page=1&limit=10", builder.toString()); + } } diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java index cb56e4b00..be0a07fb7 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -1077,7 +1077,7 @@ public class HttpRequest extends HttpBase { @Override public String toString() { StringBuilder sb = StrUtil.builder(); - sb.append("Request Url: ").append(this.url).append(StrUtil.CRLF); + sb.append("Request Url: ").append(this.url.setCharset(this.charset)).append(StrUtil.CRLF); sb.append(super.toString()); return sb.toString(); } @@ -1135,7 +1135,9 @@ public class HttpRequest extends HttpBase { } this.httpConnection = HttpConnection - .create(this.url.toURL(this.urlHandler), config.proxy)// + // issue#I50NHQ + // 在生成正式URL前,设置自定义编码 + .create(this.url.setCharset(this.charset).toURL(this.urlHandler), config.proxy)// .setConnectTimeout(config.connectionTimeout)// .setReadTimeout(config.readTimeout)// .setMethod(this.method)// diff --git a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java index 4483e1e28..d54290894 100644 --- a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java @@ -193,4 +193,19 @@ public class HttpRequestTest { HttpRequest request =HttpUtil.createGet(url).form(map); Console.log(request.execute().body()); } + + @Test + public void issueI50NHQTest(){ + String url = "http://127.0.0.1/devicerecord/list"; + HashMap params = new HashMap<>(); + params.put("start", "2022-03-31 00:00:00"); + params.put("end", "2022-03-31 23:59:59"); + params.put("page", 1); + params.put("limit", 10); + + String result = HttpRequest.get(url) + .header("token", "123") + .form(params).toString(); + Console.log(result); + } }