public class HttpConnection extends Object
| Constructor and Description |
|---|
HttpConnection(URL url,
Proxy proxy)
构造HttpConnection
|
| Modifier and Type | Method and Description |
|---|---|
HttpConnection |
connect()
连接
|
static HttpConnection |
create(String urlStr,
Proxy proxy)
创建HttpConnection
|
static HttpConnection |
create(URL url,
Proxy proxy)
创建HttpConnection
|
HttpConnection |
disableCache()
关闭缓存
|
HttpConnection |
disconnect()
断开连接
|
HttpConnection |
disconnectQuietly()
静默断开连接。
|
Charset |
getCharset()
获取字符集编码
从Http连接的头信息中获得字符集 从ContentType中获取 |
String |
getCharsetName()
获得字符集编码
从Http连接的头信息中获得字符集 从ContentType中获取 |
InputStream |
getErrorStream()
当返回错误代码时,获得错误内容流
|
HttpURLConnection |
getHttpURLConnection()
获取HttpURLConnection对象
|
InputStream |
getInputStream()
获得输入流对象
输入流对象用于读取数据 |
Method |
getMethod()
获取请求方法,GET/POST
|
OutputStream |
getOutputStream()
获取输出流对象 输出流对象用于发送数据
|
Proxy |
getProxy()
获得代理
|
URL |
getUrl()
获取请求URL
|
String |
header(Header name)
获取Http请求头
|
HttpConnection |
header(Header header,
String value,
boolean isOverride)
设置请求头
当请求头存在时,覆盖之 |
HttpConnection |
header(Map<String,List<String>> headerMap,
boolean isOverride)
设置请求头
不覆盖原有请求头 |
HttpConnection |
header(Map<String,List<String>> headerMap,
boolean isOverride,
boolean isHeaderAggregated)
设置请求头
不覆盖原有请求头并判断是否需要聚合请求头 |
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
|
HttpConnection |
setFixedLengthStreamingMode(long contentLength)
设置固定长度的流模式,会设置HTTP请求头中的Content-Length字段,告知服务器整个请求体的精确字节大小。
|
HttpConnection |
setHttpsInfo(HostnameVerifier hostnameVerifier,
SSLSocketFactory ssf)
设置https请求参数
有些时候htts请求会出现com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl的实现,此为sun内部api,按照普通http请求处理 |
HttpConnection |
setInstanceFollowRedirects(boolean isInstanceFollowRedirects)
设置自动HTTP 30X跳转
|
HttpConnection |
setMethod(Method method)
设置请求方法
|
HttpConnection |
setReadTimeout(int timeout)
设置读取超时
|
String |
toString() |
public static HttpConnection create(String urlStr, Proxy proxy)
urlStr - URLproxy - 代理,无代理传nullpublic static HttpConnection create(URL url, Proxy proxy)
url - URLproxy - 代理,无代理传nullpublic HttpConnection initConn()
public Method getMethod()
public HttpConnection setMethod(Method method)
method - 请求方法public URL getUrl()
public 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>> headerMap, boolean isOverride)
headerMap - 请求头isOverride - 是否覆盖public HttpConnection header(Map<String,List<String>> headerMap, boolean isOverride, boolean isHeaderAggregated)
headerMap - 请求头isOverride - 是否覆盖isHeaderAggregated - 是否聚合public HttpConnection setHttpsInfo(HostnameVerifier hostnameVerifier, SSLSocketFactory ssf) throws HttpException
hostnameVerifier - 域名验证器,非https传入nullssf - SSLSocketFactory,非https传入nullHttpException - KeyManagementException和NoSuchAlgorithmException异常包装public HttpConnection disableCache()
URLConnection.setUseCaches(boolean)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 setFixedLengthStreamingMode(long contentLength)
contentLength - 固定长度public HttpConnection setChunkedStreamingMode(int blockSize)
blockSize - 块大小(bytes数),0或小于0表示不设置Chuncked模式public HttpConnection setInstanceFollowRedirects(boolean isInstanceFollowRedirects)
isInstanceFollowRedirects - 是否自定跳转public HttpConnection connect() throws IOException
IOException - IO异常public HttpConnection disconnectQuietly()
public HttpConnection disconnect()
public InputStream getInputStream() throws IOException
IOException - IO异常public InputStream getErrorStream()
public OutputStream getOutputStream() throws IOException
IOException - IO异常public int responseCode()
throws IOException
IOException - IO异常public String getCharsetName()
public Charset getCharset()
Charset编码Copyright © 2025. All rights reserved.