2021-01-20 17:10:45 +08:00
|
|
|
|
package cn.hutool.http;
|
|
|
|
|
|
2021-07-09 20:29:47 +08:00
|
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
2021-12-06 01:07:56 +08:00
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
2021-07-08 09:29:02 +08:00
|
|
|
|
import cn.hutool.core.util.ReflectUtil;
|
|
|
|
|
import cn.hutool.http.cookie.GlobalCookieManager;
|
|
|
|
|
|
2021-01-20 17:10:45 +08:00
|
|
|
|
import java.io.Serializable;
|
2021-07-08 09:29:02 +08:00
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.Modifier;
|
2021-01-20 17:10:45 +08:00
|
|
|
|
import java.net.CookieManager;
|
2021-07-08 09:29:02 +08:00
|
|
|
|
import java.net.HttpURLConnection;
|
2021-01-20 17:10:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* HTTP 全局参数配置
|
2021-07-08 09:29:02 +08:00
|
|
|
|
*
|
2021-01-20 17:10:45 +08:00
|
|
|
|
* @author Looly
|
|
|
|
|
* @since 4.6.2
|
|
|
|
|
*/
|
|
|
|
|
public class HttpGlobalConfig implements Serializable {
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
2021-12-25 12:49:21 +08:00
|
|
|
|
private static int timeout = -1;
|
2021-07-08 09:29:02 +08:00
|
|
|
|
private static boolean isAllowPatch = false;
|
2021-12-06 01:07:56 +08:00
|
|
|
|
private static String boundary = "--------------------Hutool_" + RandomUtil.randomString(16);
|
2021-12-25 12:49:21 +08:00
|
|
|
|
private static int maxRedirectCount = 0;
|
2021-01-20 17:10:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取全局默认的超时时长
|
2021-07-08 09:29:02 +08:00
|
|
|
|
*
|
2021-01-20 17:10:45 +08:00
|
|
|
|
* @return 全局默认的超时时长
|
|
|
|
|
*/
|
|
|
|
|
public static int getTimeout() {
|
|
|
|
|
return timeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置默认的连接和读取超时时长
|
2021-07-08 09:29:02 +08:00
|
|
|
|
*
|
2021-01-20 17:10:45 +08:00
|
|
|
|
* @param customTimeout 超时时长
|
|
|
|
|
*/
|
2021-07-08 09:29:02 +08:00
|
|
|
|
synchronized public static void setTimeout(int customTimeout) {
|
2021-01-20 17:10:45 +08:00
|
|
|
|
timeout = customTimeout;
|
|
|
|
|
}
|
2021-07-08 09:29:02 +08:00
|
|
|
|
|
2021-12-06 01:07:56 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取全局默认的Multipart边界
|
|
|
|
|
*
|
|
|
|
|
* @return 全局默认的Multipart边界
|
|
|
|
|
* @since 5.7.17
|
|
|
|
|
*/
|
|
|
|
|
public static String getBoundary() {
|
|
|
|
|
return boundary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置默认的Multipart边界
|
|
|
|
|
*
|
|
|
|
|
* @param customBoundary 自定义Multipart边界
|
|
|
|
|
* @since 5.7.17
|
|
|
|
|
*/
|
|
|
|
|
synchronized public static void setBoundary(String customBoundary) {
|
|
|
|
|
boundary = customBoundary;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 12:49:21 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取全局默认的最大重定向次数,如设置0表示不重定向<br>
|
|
|
|
|
* 如果设置为1,表示重定向一次,即请求两次
|
|
|
|
|
*
|
|
|
|
|
* @return 全局默认的最大重定向次数
|
|
|
|
|
* @since 5.7.19
|
|
|
|
|
*/
|
|
|
|
|
public static int getMaxRedirectCount() {
|
|
|
|
|
return maxRedirectCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置默认全局默认的最大重定向次数,如设置0表示不重定向<br>
|
|
|
|
|
* 如果设置为1,表示重定向一次,即请求两次
|
|
|
|
|
*
|
|
|
|
|
* @param customMaxRedirectCount 全局默认的最大重定向次数
|
|
|
|
|
* @since 5.7.19
|
|
|
|
|
*/
|
|
|
|
|
synchronized public static void setMaxRedirectCount(int customMaxRedirectCount) {
|
|
|
|
|
maxRedirectCount = customMaxRedirectCount;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-20 17:10:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取Cookie管理器,用于自定义Cookie管理
|
2021-07-08 09:29:02 +08:00
|
|
|
|
*
|
2021-01-20 17:10:45 +08:00
|
|
|
|
* @return {@link CookieManager}
|
|
|
|
|
* @since 4.1.0
|
|
|
|
|
* @see GlobalCookieManager#getCookieManager()
|
|
|
|
|
*/
|
|
|
|
|
public static CookieManager getCookieManager() {
|
|
|
|
|
return GlobalCookieManager.getCookieManager();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 自定义{@link CookieManager}
|
2021-07-08 09:29:02 +08:00
|
|
|
|
*
|
2021-01-20 17:10:45 +08:00
|
|
|
|
* @param customCookieManager 自定义的{@link CookieManager}
|
|
|
|
|
* @since 4.5.14
|
|
|
|
|
* @see GlobalCookieManager#setCookieManager(CookieManager)
|
|
|
|
|
*/
|
2021-12-25 12:49:21 +08:00
|
|
|
|
synchronized public static void setCookieManager(CookieManager customCookieManager) {
|
2021-01-20 17:10:45 +08:00
|
|
|
|
GlobalCookieManager.setCookieManager(customCookieManager);
|
|
|
|
|
}
|
2021-07-08 09:29:02 +08:00
|
|
|
|
|
2021-01-20 17:10:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* 关闭Cookie
|
2021-07-08 09:29:02 +08:00
|
|
|
|
*
|
2021-01-20 17:10:45 +08:00
|
|
|
|
* @since 4.1.9
|
|
|
|
|
* @see GlobalCookieManager#setCookieManager(CookieManager)
|
|
|
|
|
*/
|
2021-12-25 12:49:21 +08:00
|
|
|
|
synchronized public static void closeCookie() {
|
2021-01-20 17:10:45 +08:00
|
|
|
|
GlobalCookieManager.setCookieManager(null);
|
|
|
|
|
}
|
2021-07-08 09:29:02 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 增加支持的METHOD方法<br>
|
|
|
|
|
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法<br>
|
|
|
|
|
* see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch
|
|
|
|
|
*
|
|
|
|
|
* @since 5.7.4
|
|
|
|
|
*/
|
|
|
|
|
synchronized public static void allowPatch() {
|
|
|
|
|
if(isAllowPatch){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
final Field methodsField = ReflectUtil.getField(HttpURLConnection.class, "methods");
|
2021-07-09 20:29:47 +08:00
|
|
|
|
if (null == methodsField) {
|
|
|
|
|
throw new HttpException("None static field [methods] with Java version: [{}]", System.getProperty("java.version"));
|
2021-07-08 09:29:02 +08:00
|
|
|
|
}
|
2021-07-09 20:29:47 +08:00
|
|
|
|
|
|
|
|
|
// 去除final修饰
|
|
|
|
|
ReflectUtil.setFieldValue(methodsField, "modifiers", methodsField.getModifiers() & ~Modifier.FINAL);
|
|
|
|
|
final String[] methods = {
|
|
|
|
|
"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"
|
|
|
|
|
};
|
|
|
|
|
ReflectUtil.setFieldValue(null, methodsField, methods);
|
|
|
|
|
|
|
|
|
|
// 检查注入是否成功
|
|
|
|
|
final Object staticFieldValue = ReflectUtil.getStaticFieldValue(methodsField);
|
|
|
|
|
if(false == ArrayUtil.equals(methods, staticFieldValue)){
|
|
|
|
|
throw new HttpException("Inject value to field [methods] failed!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isAllowPatch = true;
|
2021-07-08 09:29:02 +08:00
|
|
|
|
}
|
2021-01-20 17:10:45 +08:00
|
|
|
|
}
|