Package com.luna.common.net
Class HttpUtils
java.lang.Object
com.luna.common.net.HttpUtils
Thread-safe HTTP utility class optimized for high concurrency
- Author:
- Luna
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic int设置连接建立的超时时间为10sstatic int最大连接数 - 优化为更高并发static intstatic intstatic intstatic int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidstatic voidaddCookie(org.apache.hc.client5.http.cookie.Cookie cookie) static voidaddCookie(org.apache.hc.client5.http.cookie.Cookie... cookies) voidaddExecInterceptorAfter(String existing, String name, org.apache.hc.client5.http.classic.ExecChainHandler interceptor) Add execution chain interceptor - requires HttpClient recreation for thread safetyvoidaddRequestInterceptorFirst(org.apache.hc.core5.http.HttpRequestInterceptor httpRequestInterceptor) Add request interceptor - requires HttpClient recreation for thread safetystatic voidauthContext(String userName, String password, String host, String authType) Thread-safe authentication context setupstatic voidstatic <T> voidbreakingPointDownload(String host, String path, Map<String, String> headers, Map<String, String> queries, org.apache.hc.core5.concurrent.FutureCallback<Object> futureCallback) static <T> voidbreakingPointUpload(String host, String path, Map<String, String> headers, Map<String, String> queries, String key, File file, org.apache.hc.core5.concurrent.FutureCallback<Object> futureCallback) static voidbuilderHeader(Map<String, String> headers, org.apache.hc.core5.http.message.BasicClassicHttpRequest requestBase) 请求头构建static voidbuilderHeader(Map<String, String> headers, org.apache.hc.core5.http.nio.support.AsyncRequestBuilder requestBase) static String构建urlstatic StringbuildUrlHead(String host, String path) 构建请求路径static String构建请求路径static String构建请求路径static StringcheckResponseAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse) 解析响应体static StringcheckResponseAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse, List<Integer> statusList) 检测响应体并解析static StringcheckResponseAndGetResult(org.apache.hc.core5.http.HttpResponse httpResponse) static StringcheckResponseAndGetResult(org.apache.hc.core5.http.HttpResponse httpResponse, Boolean isEnsure) static StringcheckResponseAndGetResultV2(org.apache.hc.core5.http.ClassicHttpResponse httpResponse, boolean isEnsure) 检测响应体static byte[]checkResponseStreamAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse) 检测响应体获取相应流static byte[]checkResponseStreamAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse, List<Integer> statusList) 检测响应体并解析static voidClean up thread-local resources to prevent memory leaks Call this method when thread is being destroyed or when thread-local cleanup is neededstatic voiddigestAuth(String userName, String password, String host) static org.apache.hc.core5.http.ClassicHttpResponsedoDelete(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) static <T> TdoDelete(String host, String path, Map<String, String> headers, Map<String, String> queries, String body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) delete requeststatic StringdoDeleteHandler(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) static org.apache.hc.core5.http.ClassicHttpResponsestatic <T> TdoGet(String host, String path, Map<String, String> headers, Map<String, String> queries, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) 发送 get 请求static org.apache.hc.core5.http.HttpResponsestatic String发送 get 请求static org.apache.hc.core5.http.HttpResponsestatic org.apache.hc.core5.http.HttpResponsedoPost(String host, String path, Map<String, String> headers, Map<String, String> queries, byte[] body) static <T> TdoPost(String host, String path, Map<String, String> headers, Map<String, String> queries, byte[] body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) Post streamstatic org.apache.hc.core5.http.ClassicHttpResponsedoPost(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) static <T> TdoPost(String host, String path, Map<String, String> headers, Map<String, String> queries, String body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) POST请求体为字符串static org.apache.hc.core5.http.ClassicHttpResponsedoPost(String host, String path, Map<String, String> headers, Map<String, String> queries, Map<String, String> bodies) static <T> TdoPost(String host, String path, Map<String, String> headers, Map<String, String> queries, Map<String, String> bodies, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) POST 发送文件请求static <T> voiddoPost(String host, String path, Map<String, String> headers, Map<String, String> queries, Map<String, String> bodies, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler, org.apache.hc.core5.concurrent.FutureCallback<Object> futureCallback) static <T> TdoPost(String host, String path, Map<String, String> headers, Map<String, String> queries, org.apache.hc.core5.http.HttpEntity httpEntity, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) static StringdoPostHander(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) static org.apache.hc.core5.http.ClassicHttpResponsedoPut(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) PUT 方法请求static <T> TdoPut(String host, String path, Map<String, String> headers, Map<String, String> queries, String body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) PUT 方法请求static StringdoPutHandler(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) static org.apache.hc.client5.http.protocol.HttpClientContextGet current thread's HttpClientContextstatic List<org.apache.hc.client5.http.cookie.Cookie>static org.apache.hc.client5.http.cookie.BasicCookieStoreGet current thread's cookie storestatic org.apache.hc.client5.http.impl.classic.CloseableHttpClientGet current HttpClient instancestatic StringgetUserInfo(String url, String username, String password) 拼接URL用户名和密码static voidinit()Thread-safe initialization with double-checked locking patternstatic booleanstatic voidrefresh()Refresh HttpClient instance (recreate if needed)static voidstatic void使用代理访问static voidstatic Stringstatic String解析生成URLstatic String
-
Field Details
-
MAX_REDIRECTS
public static int MAX_REDIRECTS -
MAX_CONN
public static int MAX_CONN最大连接数 - 优化为更高并发 -
CONNECT_TIMEOUT
public static int CONNECT_TIMEOUT设置连接建立的超时时间为10s -
RESPONSE_TIMEOUT
public static int RESPONSE_TIMEOUT -
MAX_ROUTE
public static int MAX_ROUTE -
SOCKET_TIME_OUT
public static int SOCKET_TIME_OUT
-
-
Constructor Details
-
HttpUtils
public HttpUtils()
-
-
Method Details
-
init
public static void init()Thread-safe initialization with double-checked locking pattern -
getClientContext
public static org.apache.hc.client5.http.protocol.HttpClientContext getClientContext()Get current thread's HttpClientContext -
getCookieStore
public static org.apache.hc.client5.http.cookie.BasicCookieStore getCookieStore()Get current thread's cookie store -
refresh
public static void refresh()Refresh HttpClient instance (recreate if needed) -
basicAuth
-
digestAuth
-
authContext
Thread-safe authentication context setup- Parameters:
userName- 用户名password- 密码host- 主机地址
-
setProxy
-
setProxy
使用代理访问- Parameters:
host- 代理地址port- 代理端口
-
setProxy
-
builderHeader
public static void builderHeader(Map<String, String> headers, org.apache.hc.core5.http.message.BasicClassicHttpRequest requestBase) 请求头构建- Parameters:
headers- 请求头MaprequestBase- 请求对象
-
builderHeader
-
getCookie
-
addCookie
public static void addCookie(org.apache.hc.client5.http.cookie.Cookie cookie) -
addCookie
-
addCookie
public static void addCookie(org.apache.hc.client5.http.cookie.Cookie... cookies) -
doHead
-
doGet
-
doGet
public static <T> T doGet(String host, String path, Map<String, String> headers, Map<String, String> queries, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) 发送 get 请求- Parameters:
host- 主机地址path- 路径headers- 请求头queries- 请求参数responseHandler- 响应处理器- Returns:
- Throws:
Exception
-
doGetHandler
public static String doGetHandler(String host, String path, Map<String, String> headers, Map<String, String> queries) 发送 get 请求- Parameters:
host- 主机地址path- 路径headers- 请求头queries- 请求参数- Returns:
-
doGet
-
doDelete
public static <T> T doDelete(String host, String path, Map<String, String> headers, Map<String, String> queries, String body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) delete request- Parameters:
host- 主机地址path- 路径headers- 请求头queries- 请求参数responseHandler- 响应处理器body-- Returns:
-
doDeleteHandler
-
doDelete
-
doPut
public static <T> T doPut(String host, String path, Map<String, String> headers, Map<String, String> queries, String body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) PUT 方法请求- Parameters:
host- 主机地址path- 路径headers- 请求头queries- 请求参数body- 请求体- Returns:
- ClassicHttpResponse
-
doPut
public static org.apache.hc.core5.http.ClassicHttpResponse doPut(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) PUT 方法请求- Parameters:
host- 主机地址path- 路径headers- 请求头queries- 请求参数body- 请求体- Returns:
- ClassicHttpResponse
-
doPutHandler
-
doPost
public static <T> T doPost(String host, String path, Map<String, String> headers, Map<String, String> queries, Map<String, String> bodies, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) POST 发送文件请求- Parameters:
host- 主机地址path- 路径headers- 请求头queries- 请求参数bodies- 文件列表- Returns:
- ClassicHttpResponse
- Throws:
Exception- 运行时异常
-
doPost
-
breakingPointDownload
-
breakingPointUpload
-
doPost
-
doPost
public static <T> T doPost(String host, String path, Map<String, String> headers, Map<String, String> queries, String body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) POST请求体为字符串- Parameters:
host- 主机地址path- 路径headers- 请求头queries- 请求参数body- 请求体- Returns:
- ClassicHttpResponse
-
doPost
-
doPost
-
doPostHander
-
doPost
public static <T> T doPost(String host, String path, Map<String, String> headers, Map<String, String> queries, byte[] body, org.apache.hc.core5.http.io.HttpClientResponseHandler<T> responseHandler) Post stream- Parameters:
host- 主机地址path- 路径headers- 请求头queries- 请求参数body- 请求体- Returns:
- HttpResponse
- Throws:
RuntimeException
-
doPost
-
isUrl
-
buildUrlHead
构建请求路径- Parameters:
host- 主机地址path- 请求路径- Returns:
- String
-
buildUrlObject
构建请求路径- Parameters:
host- 主机地址path- 请求路径queries- 请求参数- Returns:
- String
-
buildUrlString
构建请求路径- Parameters:
host- 主机地址path- 请求路径queries- 请求参数- Returns:
- String
-
buildUrl
构建url- Parameters:
host- 主机地址path- 路径queries- 请求参数- Returns:
- String
-
getUserInfo
拼接URL用户名和密码- Parameters:
url-username- 用户名password-- Returns:
-
checkResponseAndGetResultV2
public static String checkResponseAndGetResultV2(org.apache.hc.core5.http.ClassicHttpResponse httpResponse, boolean isEnsure) 检测响应体- Parameters:
httpResponse- 响应体- Returns:
- String
-
checkResponseStreamAndGetResult
public static byte[] checkResponseStreamAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse) 检测响应体获取相应流- Parameters:
httpResponse- 响应体- Returns:
-
urlEncode
解析生成URL- Parameters:
map- 键值对- Returns:
- 生成的URL尾部
-
urlEncodeWithUtf8
-
urlEncode
-
checkResponseStreamAndGetResult
public static byte[] checkResponseStreamAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse, List<Integer> statusList) 检测响应体并解析- Parameters:
httpResponse- 响应体statusList- 状态码列表- Returns:
- 解析字节
-
checkResponseAndGetResult
public static String checkResponseAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse, List<Integer> statusList) 检测响应体并解析- Parameters:
httpResponse- 响应体statusList- 状态码列表- Returns:
- 解析字符串
-
checkResponseAndGetResult
public static String checkResponseAndGetResult(org.apache.hc.core5.http.ClassicHttpResponse httpResponse) 解析响应体- Parameters:
httpResponse- 响应体- Returns:
- String
-
checkResponseAndGetResult
-
checkResponseAndGetResult
-
addRequestInterceptorFirst
public void addRequestInterceptorFirst(org.apache.hc.core5.http.HttpRequestInterceptor httpRequestInterceptor) Add request interceptor - requires HttpClient recreation for thread safety -
addExecInterceptorAfter
public void addExecInterceptorAfter(String existing, String name, org.apache.hc.client5.http.classic.ExecChainHandler interceptor) Add execution chain interceptor - requires HttpClient recreation for thread safety -
cleanupThreadLocal
public static void cleanupThreadLocal()Clean up thread-local resources to prevent memory leaks Call this method when thread is being destroyed or when thread-local cleanup is needed -
getHttpClient
public static org.apache.hc.client5.http.impl.classic.CloseableHttpClient getHttpClient()Get current HttpClient instance- Returns:
- CloseableHttpClient instance
-