Class HttpUtil
java.lang.Object
com.sure.tool.http.HttpUtil
HTTP 请求工具类,基于
HttpURLConnection 的零依赖封装,参考 Hutool 的 HttpUtil 设计。
仅支持 http/https 协议,2xx/3xx 返回响应体,其余状态码抛出 HttpException。
English: A zero-dependency HTTP client facade for GET, POST and form requests.
- Since:
- 0.1.0
- Author:
- suretool
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int默认连接超时(毫秒)static final int默认读取超时(毫秒) -
Method Summary
Modifier and TypeMethodDescriptionstatic StringDELETE 请求(无请求体)。static StringDELETE 请求(无请求体)。static StringdeleteJson(String url, String json) DELETE JSON 请求。static StringdeleteJson(String url, String json, int timeoutMillis) DELETE JSON 请求。static File下载文件。static File下载文件。static byte[]downloadBytes(String url) 下载 URL 内容为字节数组。static byte[]downloadBytes(String url, int timeoutMillis) 下载 URL 内容为字节数组。static StringGET 请求。static StringGET 请求(走代理)。static StringGET 请求(参数拼接到 query)。static StringGET 请求。static byte[]获取响应字节。static byte[]获取响应字节。static StringGET JSON 请求。static StringGET JSON 请求。HEAD 请求,返回响应头。HEAD 请求,返回响应头。static StringPATCH JSON 请求。static StringPATCH JSON 请求(基于 JDK HttpClient,HttpURLConnection 不支持 PATCH)。static StringPOST 原始内容请求。static StringPOST 表单请求(application/x-www-form-urlencoded)。static StringPOST 表单请求。static StringPOST 表单请求(走代理)。static StringPOST JSON 请求。static StringPOST JSON 请求。static StringPOST JSON 请求(走代理)。static StringPUT 表单请求。static StringPUT 表单请求。static StringPUT JSON 请求。static StringPUT JSON 请求。static Stringmultipart/form-data 文件上传。static Stringmultipart/form-data 文件上传(同一字段多个文件)。static Stringmultipart/form-data 文件上传(同一字段多个文件)。static Stringmultipart/form-data 文件上传。static Stringmultipart/form-data 文件上传(走代理)。static Stringmultipart/form-data 文件上传(多字段,每字段一个文件,按 Map 迭代顺序)。static Stringmultipart/form-data 文件上传(多字段,每字段一个文件)。
-
Field Details
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUT默认连接超时(毫秒)- See Also:
-
DEFAULT_READ_TIMEOUT
public static final int DEFAULT_READ_TIMEOUT默认读取超时(毫秒)- See Also:
-
-
Method Details
-
get
-
get
-
get
-
post
-
post
-
postJson
-
postJson
-
post
-
getBytes
-
getBytes
获取响应字节。- Parameters:
url- URLtimeoutMillis- 超时(毫秒)- Returns:
- 响应字节
-
download
-
download
-
putJson
-
putJson
-
deleteJson
-
deleteJson
-
delete
-
delete
-
put
-
put
-
patchJson
-
patchJson
-
head
-
head
-
upload
-
upload
-
upload
-
upload
-
upload
-
upload
-
downloadBytes
下载 URL 内容为字节数组。- Parameters:
url- 请求地址- Returns:
- 响应字节
- Throws:
IOException- IO 异常
-
downloadBytes
下载 URL 内容为字节数组。- Parameters:
url- 请求地址timeoutMillis- 超时毫秒- Returns:
- 响应字节
- Throws:
IOException- IO 异常
-
getJson
-
getJson
-
get
-
post
-
postJson
-
upload
-