This commit is contained in:
Looly 2022-05-31 21:33:22 +08:00
parent 811c1f1025
commit a60f20f241
5 changed files with 48 additions and 6 deletions

View File

@ -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方法<br>
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性增加PATCH方法<br>
* see: <a href="https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch">https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch</a>
*/
public static void allowPatch(){
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
doAllowPatch();
return null;
});
}
/**
* 增加支持的METHOD方法<br>
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性增加PATCH方法<br>
@ -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;
}

View File

@ -507,7 +507,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
}
// 存储服务端设置的Cookie信息
GlobalCookieManager.store(httpConnection);
GlobalCookieManager.store(httpConnection, this.headers);
// 获取响应编码
final Charset charset = httpConnection.getCharset();

View File

@ -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信息到本地<br>
* 通过读取
*
* @param conn {@link HttpConnection}
* @param responseHeaders 头信息Map
*/
public static void store(final HttpConnection conn, final Map<String, List<String>> 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);
}

View File

@ -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();
}
}

View File

@ -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();
}
}