public class JdkHttpConnection extends Object implements HeaderOperation<JdkHttpConnection>, Closeable
Constructor and Description |
---|
JdkHttpConnection(URL url,
Proxy proxy)
构造HttpConnection
|
Modifier and Type | Method and Description |
---|---|
void |
close()
断开连接
|
JdkHttpConnection |
closeQuietly()
静默断开连接。
|
JdkHttpConnection |
connect()
连接
|
int |
getCode()
获取响应码
|
InputStream |
getErrorStream()
当返回错误代码时,获得错误内容流
|
HttpURLConnection |
getHttpURLConnection()
获取HttpURLConnection对象
|
InputStream |
getInputStream()
获得输入流对象
输入流对象用于读取数据 |
Method |
getMethod()
获取请求方法,GET/POST
|
OutputStream |
getOutputStream()
获取输出流对象 输出流对象用于发送数据
|
Proxy |
getProxy()
获得代理
|
URL |
getUrl()
获取请求URL
|
String |
header(String name)
获取Http请求头
|
JdkHttpConnection |
header(String header,
String value,
boolean isOverride)
设置请求头
当请求头存在时,覆盖之 |
Map<String,List<String>> |
headers()
获取所有Http请求头
|
static JdkHttpConnection |
of(String urlStr,
Proxy proxy)
创建HttpConnection
|
static JdkHttpConnection |
of(URL url,
Proxy proxy)
创建HttpConnection
|
JdkHttpConnection |
setChunkedStreamingMode(int blockSize)
采用流方式上传数据,无需本地缓存数据。
|
JdkHttpConnection |
setConnectionAndReadTimeout(int timeout)
设置连接和读取的超时时间
|
JdkHttpConnection |
setConnectTimeout(int timeout)
设置连接超时
|
JdkHttpConnection |
setDisableCache(boolean isDisableCache)
是否禁用缓存
|
JdkHttpConnection |
setFixedLengthStreamingMode(long contentLength)
设置固定长度的流模式,会设置HTTP请求头中的Content-Length字段,告知服务器整个请求体的精确字节大小。
|
JdkHttpConnection |
setInstanceFollowRedirects(boolean isInstanceFollowRedirects)
设置自动HTTP 30X跳转
|
JdkHttpConnection |
setMethod(Method method)
设置请求方法
|
JdkHttpConnection |
setReadTimeout(int timeout)
设置读取超时
|
JdkHttpConnection |
setSSLInfo(SSLInfo sslInfo)
设置https中SSL相关请求参数
有些时候htts请求会出现com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl的实现,此为sun内部api,按照普通http请求处理 |
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
auth, basicAuth, bearerAuth, contentEncoding, contentLength, contentType, cookie, cookie, cookie, disableCookie, enableDefaultCookie, header, header, header, header, header, header, isChunked, keepAlive, proxyAuth
public static JdkHttpConnection of(String urlStr, Proxy proxy)
urlStr
- URLproxy
- 代理,无代理传null
public static JdkHttpConnection of(URL url, Proxy proxy)
url
- URLproxy
- 代理,无代理传null
public Method getMethod()
public JdkHttpConnection setMethod(Method method)
method
- 请求方法public URL getUrl()
public HttpURLConnection getHttpURLConnection()
public JdkHttpConnection setDisableCache(boolean isDisableCache)
isDisableCache
- 是否禁用缓存URLConnection.setUseCaches(boolean)
public JdkHttpConnection setConnectTimeout(int timeout)
timeout
- 超时public JdkHttpConnection setReadTimeout(int timeout)
timeout
- 超时public JdkHttpConnection setConnectionAndReadTimeout(int timeout)
timeout
- 超时时间public JdkHttpConnection setSSLInfo(SSLInfo sslInfo) throws HttpException
sslInfo
- SSLInfo
HttpException
- KeyManagementException和NoSuchAlgorithmException异常包装public JdkHttpConnection setFixedLengthStreamingMode(long contentLength)
contentLength
- 请求体的长度。如果内容长度大于0,则启用固定长度流模式。public JdkHttpConnection setChunkedStreamingMode(int blockSize)
blockSize
- 块大小(bytes数),0或小于0表示不设置Chuncked模式public JdkHttpConnection setInstanceFollowRedirects(boolean isInstanceFollowRedirects)
isInstanceFollowRedirects
- 是否自定跳转public JdkHttpConnection header(String header, String value, boolean isOverride)
header
in interface HeaderOperation<JdkHttpConnection>
header
- 头名value
- 头值isOverride
- 是否覆盖旧值public String header(String name)
header
in interface HeaderOperation<JdkHttpConnection>
name
- Header名public Map<String,List<String>> headers()
headers
in interface HeaderOperation<JdkHttpConnection>
public JdkHttpConnection connect() throws IOException
IOException
- IO异常public InputStream getInputStream() throws IOException
IOException
- IO异常public InputStream getErrorStream()
public OutputStream getOutputStream() throws IOException
IOException
- IO异常public int getCode() throws IOException
IOException
- IO异常public JdkHttpConnection closeQuietly()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2025. All rights reserved.