public class HttpConnection extends Object
| 构造器和说明 |
|---|
HttpConnection(String urlStr,
Method method)
构造HttpConnection
|
| 限定符和类型 | 方法和说明 |
|---|---|
String |
charset()
获得字符集编码
|
HttpConnection |
connect()
连接
|
static HttpConnection |
create(String urlStr,
Method method)
创建HttpConnection
|
HttpConnection |
disableCache()
关闭缓存
|
HttpConnection |
disconnect()
断开连接
|
InputStream |
getErrorStream()
当返回错误代码时,获得错误内容流
|
HttpURLConnection |
getHttpURLConnection()
获取HttpURLConnection对象
|
InputStream |
getInputStream()
获得输入流对象
输入流对象用于读取数据 |
Method |
getMethod()
获取请求方法,GET/POST
|
OutputStream |
getOutputStream()
获取输出流对象
输出流对象用于发送数据
|
URL |
getUrl()
获取请求URL
|
String |
header(Header name)
获取Http请求头
|
HttpConnection |
header(Header header,
String value,
boolean isOverride)
设置请求头
当请求头存在时,覆盖之 |
HttpConnection |
header(Map<String,List<String>> headers)
设置请求头
不覆盖原有请求头 |
String |
header(String name)
获取Http请求头
|
HttpConnection |
header(String header,
String value,
boolean isOverride)
设置请求头
当请求头存在时,覆盖之 |
Map<String,List<String>> |
headers()
获取所有Http请求头
|
HttpConnection |
initConn()
初始化连接相关信息
|
int |
responseCode()
获取响应码
|
HttpConnection |
setChunkedStreamingMode(int blockSize)
采用流方式上传数据,无需本地缓存数据。
|
HttpConnection |
setConnectionAndReadTimeout(int timeout)
设置连接和读取的超时时间
|
HttpConnection |
setConnectTimeout(int timeout)
设置连接超时
|
HttpConnection |
setCookie(String cookie)
设置Cookie
|
void |
setHostnameVerifier(HostnameVerifier hostnameVerifier)
设置域名验证器
只针对HTTPS请求,如果不设置,不做验证,所有域名被信任 |
void |
setMethod(Method method)
设置请求方法
|
HttpConnection |
setReadTimeout(int timeout)
设置读取超时
|
void |
setTrustManagers(TrustManager... trustManagers)
设置信任信息
|
void |
setUrl(URL url)
设置请求URL
|
public static HttpConnection create(String urlStr, Method method)
urlStr - URLmethod - HTTP方法public HttpConnection initConn()
public Method getMethod()
public void setMethod(Method method)
method - 请求方法public void setHostnameVerifier(HostnameVerifier hostnameVerifier)
hostnameVerifier - HostnameVerifierpublic void setTrustManagers(TrustManager... trustManagers)
trustManagers - TrustManager列表public URL getUrl()
public void setUrl(URL url)
url - 请求URLpublic HttpURLConnection getHttpURLConnection()
public HttpConnection header(String header, String value, boolean isOverride)
header - 头名value - 头值isOverride - 是否覆盖旧值public HttpConnection header(Header header, String value, boolean isOverride)
header - 头名value - 头值isOverride - 是否覆盖旧值public HttpConnection header(Map<String,List<String>> headers)
headers - 请求头public HttpConnection disableCache()
public HttpConnection setConnectTimeout(int timeout)
timeout - 超时public HttpConnection setReadTimeout(int timeout)
timeout - 超时public HttpConnection setConnectionAndReadTimeout(int timeout)
timeout - 超时时间public HttpConnection setCookie(String cookie)
cookie - Cookiepublic HttpConnection setChunkedStreamingMode(int blockSize)
blockSize - 块大小(bytes数)public HttpConnection connect() throws IOException
IOExceptionpublic HttpConnection disconnect()
public InputStream getInputStream() throws IOException
IOExceptionpublic InputStream getErrorStream() throws IOException
IOExceptionpublic OutputStream getOutputStream() throws IOException
IOExceptionpublic int responseCode()
throws IOException
IOExceptionpublic String charset()
Copyright © 2016. All rights reserved.