From a60f20f2412ec8d3dcbba27aef2faca0c54cc189 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 31 May 2022 21:33:22 +0800 Subject: [PATCH] fix code --- .../java/cn/hutool/http/HttpGlobalConfig.java | 16 +++++++++++++++- .../main/java/cn/hutool/http/HttpResponse.java | 2 +- .../http/cookie/GlobalCookieManager.java | 18 +++++++++++++++--- .../java/cn/hutool/http/HttpRequestTest.java | 6 ++++++ .../hutool/http/server/SimpleServerTest.java | 12 +++++++++++- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java b/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java index e09df3dd8..026348596 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java @@ -10,6 +10,8 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.net.CookieManager; import java.net.HttpURLConnection; +import java.security.AccessController; +import java.security.PrivilegedAction; /** * HTTP 全局参数配置 @@ -181,6 +183,18 @@ public class HttpGlobalConfig implements Serializable { GlobalCookieManager.setCookieManager(null); } + /** + * 增加支持的METHOD方法
+ * 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法
+ * see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch + */ + public static void allowPatch(){ + AccessController.doPrivileged((PrivilegedAction) () -> { + doAllowPatch(); + return null; + }); + } + /** * 增加支持的METHOD方法
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法
@@ -188,7 +202,7 @@ public class HttpGlobalConfig implements Serializable { * * @since 5.7.4 */ - synchronized public static void allowPatch() { + synchronized private static void doAllowPatch() { if (isAllowPatch) { return; } diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java index 60ab142da..5b3681e11 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java @@ -507,7 +507,7 @@ public class HttpResponse extends HttpBase implements Closeable { } // 存储服务端设置的Cookie信息 - GlobalCookieManager.store(httpConnection); + GlobalCookieManager.store(httpConnection, this.headers); // 获取响应编码 final Charset charset = httpConnection.getCharset(); diff --git a/hutool-http/src/main/java/cn/hutool/http/cookie/GlobalCookieManager.java b/hutool-http/src/main/java/cn/hutool/http/cookie/GlobalCookieManager.java index b68032af1..5b406817c 100644 --- a/hutool-http/src/main/java/cn/hutool/http/cookie/GlobalCookieManager.java +++ b/hutool-http/src/main/java/cn/hutool/http/cookie/GlobalCookieManager.java @@ -1,6 +1,7 @@ package cn.hutool.http.cookie; import cn.hutool.core.io.IORuntimeException; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.net.URLUtil; import cn.hutool.http.HttpConnection; @@ -85,13 +86,24 @@ public class GlobalCookieManager { * @param conn {@link HttpConnection} */ public static void store(final HttpConnection conn) { - if(null == cookieManager) { - // 全局Cookie管理器关闭 + store(conn, conn.headers()); + } + + /** + * 存储响应的Cookie信息到本地
+ * 通过读取 + * + * @param conn {@link HttpConnection} + * @param responseHeaders 头信息Map + */ + public static void store(final HttpConnection conn, final Map> responseHeaders) { + if(null == cookieManager || MapUtil.isEmpty(responseHeaders)) { + // 全局Cookie管理器关闭或头信息为空 return; } try { - cookieManager.put(getURI(conn), conn.headers()); + cookieManager.put(getURI(conn), responseHeaders); } catch (final IOException e) { throw new IORuntimeException(e); } 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 109da1210..63f65bfb0 100644 --- a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java @@ -204,4 +204,10 @@ public class HttpRequestTest { final HttpRequest httpRequest = new HttpRequest(urlBuilder); httpRequest.setMethod(Method.GET).execute(); } + + @Test + @Ignore + public void getCookieTest(){ + HttpRequest.get("http://localhost:8888/getCookier").execute(); + } } diff --git a/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java b/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java index 30998beca..9681e2ec2 100644 --- a/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java @@ -1,12 +1,16 @@ package cn.hutool.http.server; +import cn.hutool.core.collection.ListUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.lang.Console; import cn.hutool.core.net.multipart.UploadFile; import cn.hutool.http.ContentType; +import cn.hutool.http.Header; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONUtil; +import java.net.HttpCookie; + public class SimpleServerTest { public static void main(final String[] args) { @@ -54,7 +58,13 @@ public class SimpleServerTest { .addAction("test/zeroStr", (req, res)-> { res.write("0"); Console.log("Write 0 OK"); - }) + }).addAction("/getCookie", ((request, response) -> { + response.setHeader(Header.SET_COOKIE.toString(), + ListUtil.of( + new HttpCookie("cc", "123").toString(), + new HttpCookie("cc", "abc").toString())); + response.write("Cookie ok"); + })) .start(); } }