public class HttpClient extends PooledObject implements Closeable
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_USER_AGENT |
| Constructor and Description |
|---|
HttpClient(String urlString)
构建函数
|
HttpClient(String urlString,
int timeout)
构建函数
|
HttpClient(String urlString,
String charset)
构建函数
|
HttpClient(String urlString,
String charset,
int timeout)
构建函数
|
| Modifier and Type | Method and Description |
|---|---|
HttpClient |
addPart(Part part)
设置POST多段请求
类似 Form 的 Actiong="POST" enctype="multipart/form-data"
|
void |
asyncSend(Consumer<Response> async)
连接并发送请求,异步获得响应
|
void |
asyncSend(String location,
Consumer<Response> async)
连接并发送请求,异步获得响应
|
static String |
buildQueryString(Map<String,Object> parameters,
String charset)
构建QueryString
将 Map 集合转换成 QueryString 字符串
|
void |
close()
关闭 HTTP 连接
|
protected void |
finished(Response response)
请求完成
|
List<Cookie> |
getCookies()
获取Cookie集合
|
Header |
getHeader()
获取请求头集合
|
HttpRequest |
getHttpRequest() |
Map<String,Object> |
getParameters()
获取请求参数集合
|
protected TcpSocket |
getSocket()
获取 Socket 连接
|
HttpClient |
initHeader()
重新初始化 http 头
|
protected void |
initWebSocket()
初始化 WebSocket
在 HttpFilter 中触发
|
boolean |
isConnect()
判断是否处于连接状态
|
boolean |
isParamInUrl()
获取参数是否封装在 URL 中, 仅在 POST 等请求中有效
|
ByteBuffer |
loadStream()
读取流
|
static HttpClient |
newInstance(String urlString)
静态构造方法
默认字符集 UTF-8, 默认超时时间 5s
|
static HttpClient |
newInstance(String urlString,
int timeout)
静态构造方法
默认超时时间 5s
|
static HttpClient |
newInstance(String urlString,
String charset)
静态构造方法
默认超时时间 5s
|
static HttpClient |
newInstance(String urlString,
String charset,
int timeout)
静态构造方法
|
HttpClient |
putHeader(String name,
String value)
设置请求头
|
HttpClient |
putParameters(String name,
String value)
设置请求参数
|
Response |
send()
连接并发送请求
|
int |
send(ByteBuffer buffer)
发送二进制数据
|
Response |
send(String location)
连接并发送请求
|
void |
sendStream(ByteBuffer buffer)
直接发送流
|
HttpClient |
setBodyType(Request.RequestType bodyType)
设置报文形式
|
HttpClient |
setData(byte[] data)
设置请求内容
|
HttpClient |
setData(String data)
设置请求内容
|
HttpClient |
setData(String contentType,
String data)
设置请求内容
|
HttpClient |
setData(String contentType,
String data,
String charset)
设置请求内容
|
HttpClient |
setJsonData(Object data)
设置请求内容
|
HttpClient |
setJsonData(Object data,
String charset)
设置请求内容
|
HttpClient |
setMethod(String method)
设置请求方法
|
HttpClient |
setParamInUrl(boolean paramInUrl)
设置是否将参数封装在 URL 中, 仅在 POST 等请求中有效
|
void |
uploadFile(String name,
File file)
上传文件
|
void |
webSocket(String location,
WebSocketRouter webSocketRouter)
连接 Websocket
|
getPoolObjectId, setPoolObjectIdpublic static final String DEFAULT_USER_AGENT
public HttpClient(String urlString)
urlString - 请求的 URL 地址public HttpClient(String urlString, int timeout)
urlString - 请求的 URL 地址timeout - 超时时间public HttpClient(String urlString, String charset, int timeout)
urlString - 请求的 URL 地址charset - 字符集timeout - 超时时间public boolean isParamInUrl()
public HttpClient setParamInUrl(boolean paramInUrl)
paramInUrl - true:是, false: 否public HttpClient initHeader()
protected TcpSocket getSocket()
public HttpRequest getHttpRequest()
public void sendStream(ByteBuffer buffer)
buffer - 字节缓冲对象ByteBufferpublic ByteBuffer loadStream() throws IOException
IOException - IO异常对象public HttpClient setMethod(String method)
method - Http 请求的方法public HttpClient setBodyType(Request.RequestType bodyType)
bodyType - Http 报文形式public HttpClient setData(byte[] data)
data - 请求内容public HttpClient setData(String data)
data - 请求内容public HttpClient setData(String contentType, String data)
contentType - ContentType 类型data - 请求内容public HttpClient setData(String contentType, String data, String charset)
contentType - ContentType 类型data - 请求内容charset - 字符集public HttpClient setJsonData(Object data, String charset)
data - 请求内容charset - 字符集public HttpClient setJsonData(Object data)
data - 请求内容public Header getHeader()
public HttpClient putHeader(String name, String value)
name - Header 名称value - Header 值public HttpClient putParameters(String name, String value)
name - 参数名value - 参数值public HttpClient addPart(Part part)
part - POST 请求包文对象public static String buildQueryString(Map<String,Object> parameters, String charset)
parameters - 用于保存拼装请求字符串参数的 Map 对象charset - 参数的字符集public Response send(String location) throws SendMessageException, ReadMessageException
location - 请求 URLSendMessageException - 发送异常ReadMessageException - 读取异常public void asyncSend(String location, Consumer<Response> async) throws SendMessageException, ReadMessageException
location - 请求 URLasync - 异步响应消费对象SendMessageException - 发送异常ReadMessageException - 读取异常public Response send() throws SendMessageException, ReadMessageException
SendMessageException - 发送异常ReadMessageException - 读取异常public void asyncSend(Consumer<Response> async) throws SendMessageException, ReadMessageException
async - 异步响应消费对象SendMessageException - 发送异常ReadMessageException - 读取异常public int send(ByteBuffer buffer) throws IOException
buffer - 二进制数据IOException - IO 异常protected void finished(Response response)
response - 请求对象public void webSocket(String location, WebSocketRouter webSocketRouter) throws SendMessageException, ReadMessageException
location - URL地址webSocketRouter - WebSocker的路由SendMessageException - 发送异常ReadMessageException - 读取异常protected void initWebSocket()
public void close()
close in interface Closeableclose in interface AutoCloseablepublic boolean isConnect()
public static HttpClient newInstance(String urlString)
urlString - 请求的 URL 地址public static HttpClient newInstance(String urlString, int timeout)
urlString - 请求的 URL 地址timeout - 超时时间public static HttpClient newInstance(String urlString, String charset)
urlString - 请求的 URL 地址charset - 字符集public static HttpClient newInstance(String urlString, String charset, int timeout)
urlString - 请求的 URL 地址charset - 字符集timeout - 超时时间Copyright © 2020 Voovan. All rights reserved.