Package com.luna.common.net
Class HttpUtils
java.lang.Object
com.luna.common.net.HttpUtils
- Author:
- Luna
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.apache.hc.client5.http.protocol.HttpClientContextstatic int设置连接建立的超时时间为10sstatic final org.apache.hc.client5.http.cookie.BasicCookieStorestatic 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) voidaddRequestInterceptorFirst(org.apache.hc.core5.http.HttpRequestInterceptor httpRequestInterceptor) static voidauthContext(String userName, String password, String host, String authType) 需要用户名basic 验证static 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 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 List<org.apache.hc.client5.http.cookie.Cookie>static StringgetUserInfo(String url, String username, String password) 拼接URL用户名和密码static voidinit()static booleanstatic voidrefresh()static voidstatic void使用代理访问static voidstatic Stringstatic String解析生成URLstatic String
-
Field Details
-
CLIENT_CONTEXT
public static final org.apache.hc.client5.http.protocol.HttpClientContext CLIENT_CONTEXT -
COOKIE_STORE
public static final org.apache.hc.client5.http.cookie.BasicCookieStore COOKIE_STORE -
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() -
refresh
public static void refresh() -
basicAuth
-
digestAuth
-
authContext
需要用户名basic 验证- 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-requestBase-
-
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) -
addExecInterceptorAfter
-