From bdef4713e20dd7515eb669c3328169f69b851607 Mon Sep 17 00:00:00 2001 From: looly Date: Thu, 9 Dec 2021 16:48:47 +0800 Subject: [PATCH] fix code --- .../java/cn/hutool/core/net/url/UrlQuery.java | 2 + .../java/cn/hutool/core/net/UrlQueryTest.java | 7 + .../main/java/cn/hutool/http/HttpRequest.java | 200 ++++++++++-------- 3 files changed, 124 insertions(+), 85 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/net/url/UrlQuery.java b/hutool-core/src/main/java/cn/hutool/core/net/url/UrlQuery.java index 0ebbae359..e7507503e 100644 --- a/hutool-core/src/main/java/cn/hutool/core/net/url/UrlQuery.java +++ b/hutool-core/src/main/java/cn/hutool/core/net/url/UrlQuery.java @@ -20,6 +20,8 @@ import java.util.Map; *
  *   key1=v1&key2=&key3=v3
  * 
+ * 查询封装分为解析查询字符串和构建查询字符串,解析可通过charset为null来自定义是否decode编码后的内容,
+ * 构建则通过charset是否为null是否encode参数键值对 * * @author looly * @since 5.3.1 diff --git a/hutool-core/src/test/java/cn/hutool/core/net/UrlQueryTest.java b/hutool-core/src/test/java/cn/hutool/core/net/UrlQueryTest.java index 54ce50a51..9338662a5 100644 --- a/hutool-core/src/test/java/cn/hutool/core/net/UrlQueryTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/net/UrlQueryTest.java @@ -130,4 +130,11 @@ public class UrlQueryTest { final String a = UrlQuery.of(MapUtil.of("a ", " ")).build(CharsetUtil.CHARSET_UTF_8); Assert.assertEquals("a%20=%20", a); } + + @Test + public void parsePercentTest(){ + String queryStr = "a%2B=ccc"; + final UrlQuery query = UrlQuery.of(queryStr, null); + Assert.assertEquals(queryStr, query.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 67a43e027..7f07ff710 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -12,6 +12,7 @@ import cn.hutool.core.map.MapUtil; import cn.hutool.core.net.SSLUtil; import cn.hutool.core.net.url.UrlBuilder; import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.body.BytesBody; @@ -30,6 +31,7 @@ import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URLStreamHandler; +import java.nio.charset.Charset; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; @@ -43,6 +45,112 @@ import java.util.function.Consumer; */ public class HttpRequest extends HttpBase { + // ---------------------------------------------------------------- static Http Method start + + /** + * POST请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest post(String url) { + return of(url).method(Method.POST); + } + + /** + * GET请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest get(String url) { + return of(url).method(Method.GET); + } + + /** + * HEAD请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest head(String url) { + return of(url).method(Method.HEAD); + } + + /** + * OPTIONS请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest options(String url) { + return of(url).method(Method.OPTIONS); + } + + /** + * PUT请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest put(String url) { + return of(url).method(Method.PUT); + } + + /** + * PATCH请求 + * + * @param url URL + * @return HttpRequest + * @since 3.0.9 + */ + public static HttpRequest patch(String url) { + return of(url).method(Method.PATCH); + } + + /** + * DELETE请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest delete(String url) { + return of(url).method(Method.DELETE); + } + + /** + * TRACE请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest trace(String url) { + return of(url).method(Method.TRACE); + } + + /** + * 构建一个HTTP请求 + * + * @param url URL链接,默认自动编码URL中的参数等信息 + * @return HttpRequest + * @since 5.7.18 + */ + public static HttpRequest of(String url) { + return of(url, CharsetUtil.CHARSET_UTF_8); + } + + /** + * 构建一个HTTP请求 + * + * @param url URL链接 + * @param charset 编码,如果为{@code null}不自动解码编码URL + * @return HttpRequest + * @since 5.7.18 + */ + public static HttpRequest of(String url, Charset charset) { + return new HttpRequest(UrlBuilder.of(url, charset)); + } + /** * 设置全局默认的连接和读取超时时长 * @@ -85,6 +193,7 @@ public class HttpRequest extends HttpBase { public static void closeCookie() { GlobalCookieManager.setCookieManager(null); } + // ---------------------------------------------------------------- static Http Method end private UrlBuilder url; private URLStreamHandler urlHandler; @@ -168,95 +277,16 @@ public class HttpRequest extends HttpBase { * @param url {@link UrlBuilder} */ public HttpRequest(UrlBuilder url) { - this.url = url; + this.url = Assert.notNull(url, "URL must be not null!"); + // 给定默认URL编码 + final Charset charset = url.getCharset(); + if (null != charset) { + this.charset(charset); + } // 给定一个默认头信息 this.header(GlobalHeaders.INSTANCE.headers); } - // ---------------------------------------------------------------- static Http Method start - - /** - * POST请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest post(String url) { - return new HttpRequest(url).method(Method.POST); - } - - /** - * GET请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest get(String url) { - return new HttpRequest(url).method(Method.GET); - } - - /** - * HEAD请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest head(String url) { - return new HttpRequest(url).method(Method.HEAD); - } - - /** - * OPTIONS请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest options(String url) { - return new HttpRequest(url).method(Method.OPTIONS); - } - - /** - * PUT请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest put(String url) { - return new HttpRequest(url).method(Method.PUT); - } - - /** - * PATCH请求 - * - * @param url URL - * @return HttpRequest - * @since 3.0.9 - */ - public static HttpRequest patch(String url) { - return new HttpRequest(url).method(Method.PATCH); - } - - /** - * DELETE请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest delete(String url) { - return new HttpRequest(url).method(Method.DELETE); - } - - /** - * TRACE请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest trace(String url) { - return new HttpRequest(url).method(Method.TRACE); - } - // ---------------------------------------------------------------- static Http Method end - /** * 获取请求URL *