public class WebUtils extends Object
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
buildForm(String baseUrl,
Map<String,String> parameters) |
static String |
buildQuery(Map<String,String> params,
String charset) |
static String |
decode(String value)
使用默认的UTF-8字符集反编码请求参数值。
|
static String |
decode(String value,
String charset)
使用指定的字符集反编码请求参数值。
|
static String |
doGet(String url,
Map<String,String> params)
执行HTTP GET请求。
|
static String |
doGet(String url,
Map<String,String> params,
String charset)
执行HTTP GET请求。
|
static String |
doPost(String url,
Map<String,String> params,
Map<String,FileItem> fileParams,
String charset,
int connectTimeout,
int readTimeout,
String proxyHost,
int proxyPort,
Map<String,String> headers,
Map<String,String> resHeaders)
执行带文件上传的HTTP POST请求。
|
static String |
doPost(String url,
Map<String,String> params,
String charset,
int connectTimeout,
int readTimeout,
String proxyHost,
int proxyPort,
Map<String,String> headers,
Map<String,String> resHeaders)
执行HTTP POST请求,可使用代理proxy。
|
static String |
doPost(String url,
String ctype,
byte[] content,
int connectTimeout,
int readTimeout,
String proxyHost,
int proxyPort,
Map<String,String> headers,
Map<String,String> resHeaders)
执行HTTP POST请求。
|
static String |
encode(String value)
使用默认的UTF-8字符集编码请求参数值。
|
static String |
encode(String value,
String charset)
使用指定的字符集编码请求参数值。
|
static HttpURLConnection |
getConnection(URL url,
String method,
String ctype,
Map<String,String> headers) |
static HttpURLConnection |
getConnection(URL url,
String method,
String ctype,
String proxyHost,
int proxyPort,
Map<String,String> headers) |
static byte[] |
getFileEntry(String fieldName,
String fileName,
String mimeType,
String charset) |
static Map<String,String> |
getParamsFromUrl(String url) |
protected static String |
getResponseAsString(HttpURLConnection conn) |
static byte[] |
getTextEntry(String fieldName,
String fieldValue,
String charset) |
static boolean |
isNeedCheckServerTrusted()
Getter method for property needCheckServerTrusted.
|
static void |
setKeepAliveTimeout(int timeout)
设置KeepAlive连接超时时间,一次HTTP请求完成后,底层TCP连接将尝试尽量保持该超时时间后才关闭,以便其他HTTP请求复用TCP连接
KeepAlive连接超时时间设置为0,表示使用默认的KeepAlive连接缓存时长(目前为5s)
连接并非一定能保持指定的KeepAlive超时时长,比如服务端断开了连接
注:该方法目前只在JDK8上测试有效
|
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 boolean isNeedCheckServerTrusted()
public static void setKeepAliveTimeout(int timeout)
KeepAlive连接超时时间设置为0,表示使用默认的KeepAlive连接缓存时长(目前为5s)
连接并非一定能保持指定的KeepAlive超时时长,比如服务端断开了连接
注:该方法目前只在JDK8上测试有效
timeout - KeepAlive超时时间,单位秒public static String doPost(String url, Map<String,String> params, String charset, int connectTimeout, int readTimeout, String proxyHost, int proxyPort, Map<String,String> headers, Map<String,String> resHeaders) throws IOException
url - 请求地址params - 请求参数charset - 字符集,如UTF-8, GBK, GB2312connectTimeout - 连接超时时间readTimeout - 请求超时时间proxyHost - 代理host,传null表示不使用代理proxyPort - 代理端口,传0表示不使用代理IOExceptionpublic static String doPost(String url, String ctype, byte[] content, int connectTimeout, int readTimeout, String proxyHost, int proxyPort, Map<String,String> headers, Map<String,String> resHeaders) throws IOException
url - 请求地址ctype - 请求类型content - 请求字节数组connectTimeout - 连接超时时间readTimeout - 请求超时时间proxyHost - 代理host,传null表示不使用代理proxyPort - 代理端口,传0表示不使用代理IOExceptionpublic static String doPost(String url, Map<String,String> params, Map<String,FileItem> fileParams, String charset, int connectTimeout, int readTimeout, String proxyHost, int proxyPort, Map<String,String> headers, Map<String,String> resHeaders) throws IOException
url - 请求地址params - 文本请求参数fileParams - 文件请求参数charset - 字符集,如UTF-8, GBK, GB2312connectTimeout - 连接超时时间readTimeout - 请求超时时间proxyHost - 代理host,传null表示不使用代理proxyPort - 代理端口,传0表示不使用代理IOExceptionpublic static byte[] getTextEntry(String fieldName, String fieldValue, String charset) throws IOException
IOExceptionpublic static byte[] getFileEntry(String fieldName, String fileName, String mimeType, String charset) throws IOException
IOExceptionpublic static String doGet(String url, Map<String,String> params) throws IOException
url - 请求地址params - 请求参数IOExceptionpublic static String doGet(String url, Map<String,String> params, String charset) throws IOException
url - 请求地址params - 请求参数charset - 字符集,如UTF-8, GBK, GB2312IOExceptionpublic static HttpURLConnection getConnection(URL url, String method, String ctype, Map<String,String> headers) throws IOException
IOExceptionpublic static HttpURLConnection getConnection(URL url, String method, String ctype, String proxyHost, int proxyPort, Map<String,String> headers) throws IOException
IOExceptionpublic static String buildQuery(Map<String,String> params, String charset) throws IOException
IOExceptionprotected static String getResponseAsString(HttpURLConnection conn) throws IOException
IOExceptionpublic static String decode(String value)
value - 参数值public static String decode(String value, String charset)
value - 参数值charset - 字符集public static String encode(String value, String charset)
value - 参数值charset - 字符集public static Map<String,String> splitUrlQuery(String query)
query - URL地址Copyright © 2025. All rights reserved.