mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
811c1f1025
commit
a60f20f241
@ -10,6 +10,8 @@ import java.lang.reflect.Field;
|
|||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.net.CookieManager;
|
import java.net.CookieManager;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
import java.security.AccessController;
|
||||||
|
import java.security.PrivilegedAction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP 全局参数配置
|
* HTTP 全局参数配置
|
||||||
@ -181,6 +183,18 @@ public class HttpGlobalConfig implements Serializable {
|
|||||||
GlobalCookieManager.setCookieManager(null);
|
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>
|
* 增加支持的METHOD方法<br>
|
||||||
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法<br>
|
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法<br>
|
||||||
@ -188,7 +202,7 @@ public class HttpGlobalConfig implements Serializable {
|
|||||||
*
|
*
|
||||||
* @since 5.7.4
|
* @since 5.7.4
|
||||||
*/
|
*/
|
||||||
synchronized public static void allowPatch() {
|
synchronized private static void doAllowPatch() {
|
||||||
if (isAllowPatch) {
|
if (isAllowPatch) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -507,7 +507,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 存储服务端设置的Cookie信息
|
// 存储服务端设置的Cookie信息
|
||||||
GlobalCookieManager.store(httpConnection);
|
GlobalCookieManager.store(httpConnection, this.headers);
|
||||||
|
|
||||||
// 获取响应编码
|
// 获取响应编码
|
||||||
final Charset charset = httpConnection.getCharset();
|
final Charset charset = httpConnection.getCharset();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.hutool.http.cookie;
|
package cn.hutool.http.cookie;
|
||||||
|
|
||||||
import cn.hutool.core.io.IORuntimeException;
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.net.URLUtil;
|
import cn.hutool.core.net.URLUtil;
|
||||||
import cn.hutool.http.HttpConnection;
|
import cn.hutool.http.HttpConnection;
|
||||||
|
|
||||||
@ -85,13 +86,24 @@ public class GlobalCookieManager {
|
|||||||
* @param conn {@link HttpConnection}
|
* @param conn {@link HttpConnection}
|
||||||
*/
|
*/
|
||||||
public static void store(final HttpConnection conn) {
|
public static void store(final HttpConnection conn) {
|
||||||
if(null == cookieManager) {
|
store(conn, conn.headers());
|
||||||
// 全局Cookie管理器关闭
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储响应的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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
cookieManager.put(getURI(conn), conn.headers());
|
cookieManager.put(getURI(conn), responseHeaders);
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -204,4 +204,10 @@ public class HttpRequestTest {
|
|||||||
final HttpRequest httpRequest = new HttpRequest(urlBuilder);
|
final HttpRequest httpRequest = new HttpRequest(urlBuilder);
|
||||||
httpRequest.setMethod(Method.GET).execute();
|
httpRequest.setMethod(Method.GET).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void getCookieTest(){
|
||||||
|
HttpRequest.get("http://localhost:8888/getCookier").execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
package cn.hutool.http.server;
|
package cn.hutool.http.server;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.ListUtil;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.lang.Console;
|
import cn.hutool.core.lang.Console;
|
||||||
import cn.hutool.core.net.multipart.UploadFile;
|
import cn.hutool.core.net.multipart.UploadFile;
|
||||||
import cn.hutool.http.ContentType;
|
import cn.hutool.http.ContentType;
|
||||||
|
import cn.hutool.http.Header;
|
||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
|
|
||||||
|
import java.net.HttpCookie;
|
||||||
|
|
||||||
public class SimpleServerTest {
|
public class SimpleServerTest {
|
||||||
|
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) {
|
||||||
@ -54,7 +58,13 @@ public class SimpleServerTest {
|
|||||||
.addAction("test/zeroStr", (req, res)-> {
|
.addAction("test/zeroStr", (req, res)-> {
|
||||||
res.write("0");
|
res.write("0");
|
||||||
Console.log("Write 0 OK");
|
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();
|
.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user