public class HttpUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static String |
buildQuery(Map<String,String> params) |
static HttpResponse |
doDelete(String url,
Map<String,String> params,
Map<String,String> headers,
String contentType,
int connectTimeout,
int readTimeout)
执行HTTP DELETE请求。
|
static HttpResponse |
doGet(String url,
Map<String,String> headers,
Map<String,String> params,
String contentType,
int connectTimeout,
int readTimeout)
执行HTTP GET请求。
|
static HttpResponse |
doPost(String url,
Map<String,String> headers,
Map<String,String> params,
String contentType,
String body,
int connectTimeout,
int readTimeout,
String proxyHost,
int proxyPort)
执行HTTP POST请求,可使用代理proxy。
|
static HttpResponse |
doPost(String url,
Map<String,String> headers,
String contentType,
byte[] content,
int connectTimeout,
int readTimeout,
String proxyHost,
int proxyPort)
执行HTTP POST请求。
|
static HttpResponse |
doPut(String url,
Map<String,String> headers,
Map<String,String> params,
String contentType,
String body,
int connectTimeout,
int readTimeout,
String proxyHost,
int proxyPort)
执行HTTP PUT请求
|
static HttpResponse |
doPut(String url,
Map<String,String> headers,
String ctype,
byte[] content,
int connectTimeout,
int readTimeout,
String proxyHost,
int proxyPort) |
static HttpURLConnection |
getConnection(URL url,
String method,
Map<String,String> headers,
String ctype) |
static HttpURLConnection |
getConnection(URL url,
String method,
Map<String,String> headers,
String contentType,
String proxyHost,
int proxyPort) |
protected static HttpResponse |
getHttpResponse(HttpURLConnection conn) |
static void |
setKeepAliveTimeout(int timeout)
设置KeepAlive连接超时时间,一次HTTP请求完成后,底层TCP连接将尝试尽量保持该超时时间后才关闭,以便其他HTTP请求复用TCP连接
|
static void |
setNeedCheckServerTrusted(boolean needCheckServerTrusted)
设置是否校验SSL服务端证书
|
static Map<String,String> |
splitUrlQuery(String query)
从URL中提取所有的参数。
|
public static void setNeedCheckServerTrusted(boolean needCheckServerTrusted)
needCheckServerTrusted - true:需要校验(默认,推荐);
false:不需要校验(仅当部署环境不便于进行服务端证书校验,且已有其他方式确保通信安全时,可以关闭SSL服务端证书校验功能)
public static void setKeepAliveTimeout(int timeout)
KeepAlive连接超时时间设置为0,表示使用默认的KeepAlive连接缓存时长(目前为5s)
连接并非一定能保持指定的KeepAlive超时时长,比如服务端断开了连接
注:该方法目前只在JDK8上测试有效
timeout - KeepAlive超时时间,单位秒public static HttpResponse doPost(String url, Map<String,String> headers, Map<String,String> params, String contentType, String body, int connectTimeout, int readTimeout, String proxyHost, int proxyPort) throws IOException
url - 请求地址headers - 请求头params - 请求参数contentType - 请求类型body - 请求体connectTimeout - 连接超时时间readTimeout - 请求超时时间proxyHost - 代理host,传null表示不使用代理proxyPort - 代理端口,传0表示不使用代理IOExceptionpublic static HttpResponse doPost(String url, Map<String,String> headers, String contentType, byte[] content, int connectTimeout, int readTimeout, String proxyHost, int proxyPort) throws IOException
url - 请求地址headers - 请求头contentType - 请求类型content - 请求字节数组connectTimeout - 连接超时时间readTimeout - 请求超时时间proxyHost - 代理host,传null表示不使用代理proxyPort - 代理端口,传0表示不使用代理IOExceptionpublic static HttpResponse doGet(String url, Map<String,String> headers, Map<String,String> params, String contentType, int connectTimeout, int readTimeout) throws IOException
url - 请求地址headers - 请求头params - 请求参数contentType - 请求格式connectTimeout - 连接超时时间readTimeout - 请求超时时间IOExceptionpublic static HttpResponse doPut(String url, Map<String,String> headers, Map<String,String> params, String contentType, String body, int connectTimeout, int readTimeout, String proxyHost, int proxyPort) throws IOException
url - 请求地址headers - 请求头params - 请求参数connectTimeout - 连接超时时间readTimeout - 请求超时时间proxyHost - 代理host,传null表示不使用代理proxyPort - 代理端口,传0表示不使用代理contentType - 请求格式IOExceptionpublic static HttpResponse doPut(String url, Map<String,String> headers, String ctype, byte[] content, int connectTimeout, int readTimeout, String proxyHost, int proxyPort) throws IOException
url - 请求地址headers - 请求头ctype - 请求类型content - 请求字节数组connectTimeout - 连接超时时间readTimeout - 请求超时时间proxyHost - 代理host,传null表示不使用代理proxyPort - 代理端口,传0表示不使用代理IOExceptionpublic static HttpResponse doDelete(String url, Map<String,String> params, Map<String,String> headers, String contentType, int connectTimeout, int readTimeout) throws IOException
url - 请求地址headers - 请求头params - 请求参数contentType - 请求格式connectTimeout - 请求格式readTimeout - 请求格式IOExceptionpublic static HttpURLConnection getConnection(URL url, String method, Map<String,String> headers, String ctype) throws IOException
IOExceptionpublic static HttpURLConnection getConnection(URL url, String method, Map<String,String> headers, String contentType, String proxyHost, int proxyPort) throws IOException
IOExceptionpublic static String buildQuery(Map<String,String> params) throws IOException
IOExceptionprotected static HttpResponse getHttpResponse(HttpURLConnection conn) throws IOException
IOExceptionCopyright © 2021. All rights reserved.