Package com.luna.common.net
Class HttpUtils
java.lang.Object
com.luna.common.net.HttpUtils
- Author:
- Luna
-
Field Summary
Modifier and TypeFieldDescriptionstatic final org.apache.hc.client5.http.protocol.HttpClientContext
static int
设置连接建立的超时时间为10sstatic final org.apache.hc.client5.http.cookie.BasicCookieStore
static int
最大连接数static int
static int
static int
static int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
static void
addCookie
(org.apache.hc.client5.http.cookie.Cookie cookie) static void
addCookie
(org.apache.hc.client5.http.cookie.Cookie... cookies) void
addExecInterceptorAfter
(String existing, String name, org.apache.hc.client5.http.classic.ExecChainHandler interceptor) void
addRequestInterceptorFirst
(org.apache.hc.core5.http.HttpRequestInterceptor httpRequestInterceptor) static void
authContext
(String userName, String password, String host, String authType) 需要用户名basic 验证static void
static <T> void
breakingPointDownload
(String host, String path, Map<String, String> headers, Map<String, String> queries, org.apache.hc.core5.concurrent.FutureCallback<Object> futureCallback) static <T> void
breakingPointUpload
(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 void
builderHeader
(Map<String, String> headers, org.apache.hc.core5.http.message.BasicClassicHttpRequest requestBase) 请求头构建static void
builderHeader
(Map<String, String> headers, org.apache.hc.core5.http.nio.support.AsyncRequestBuilder requestBase) static String
构建urlstatic String
buildUrlHead
(String host, String path) 构建请求路径static String
构建请求路径static String
构建请求路径static String
checkResponseAndGetResult
(org.apache.hc.core5.http.ClassicHttpResponse httpResponse) 解析响应体static String
checkResponseAndGetResult
(org.apache.hc.core5.http.ClassicHttpResponse httpResponse, List<Integer> statusList) 检测响应体并解析static String
checkResponseAndGetResult
(org.apache.hc.core5.http.HttpResponse httpResponse) static String
checkResponseAndGetResult
(org.apache.hc.core5.http.HttpResponse httpResponse, Boolean isEnsure) static String
checkResponseAndGetResultV2
(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 void
digestAuth
(String userName, String password, String host) static org.apache.hc.core5.http.ClassicHttpResponse
doDelete
(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) 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 requeststatic String
doDeleteHandler
(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) static org.apache.hc.core5.http.ClassicHttpResponse
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 请求static org.apache.hc.core5.http.HttpResponse
static String
发送 get 请求static org.apache.hc.core5.http.HttpResponse
static org.apache.hc.core5.http.HttpResponse
doPost
(String host, String path, Map<String, String> headers, Map<String, String> queries, byte[] body) 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 streamstatic org.apache.hc.core5.http.ClassicHttpResponse
doPost
(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) 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请求体为字符串static org.apache.hc.core5.http.ClassicHttpResponse
doPost
(String host, String path, Map<String, String> headers, Map<String, String> queries, Map<String, String> bodies) 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 发送文件请求static <T> void
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, org.apache.hc.core5.concurrent.FutureCallback<Object> futureCallback) static <T> T
doPost
(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 String
doPostHander
(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) static org.apache.hc.core5.http.ClassicHttpResponse
doPut
(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) PUT 方法请求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 方法请求static String
doPutHandler
(String host, String path, Map<String, String> headers, Map<String, String> queries, String body) static List<org.apache.hc.client5.http.cookie.Cookie>
static String
getUserInfo
(String url, String username, String password) 拼接URL用户名和密码static void
init()
static boolean
static void
refresh()
static void
static void
使用代理访问static void
static String
static 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
-