public class HttpResponse extends HttpBase<HttpResponse> implements Closeable
Modifier and Type | Field and Description |
---|---|
protected HttpConfig |
config
Http配置
|
protected HttpConnection |
httpConnection
持有连接对象
|
protected InputStream |
in
Http请求原始流
|
protected int |
status
响应状态码
|
body, charset, DEFAULT_CHARSET, headers, HTTP_1_0, HTTP_1_1, httpVersion
Modifier | Constructor and Description |
---|---|
protected |
HttpResponse(HttpConnection httpConnection,
HttpConfig config,
Charset charset,
boolean isAsync,
boolean isIgnoreBody)
构造
|
Modifier and Type | Method and Description |
---|---|
String |
body()
获取响应主体
|
HttpResponse |
body(byte[] bodyBytes)
设置主体字节码
需在此方法调用前使用charset方法设置编码,否则使用默认编码UTF-8 |
byte[] |
bodyBytes()
获取响应流字节码
此方法会转为同步模式 |
InputStream |
bodyStream()
|
void |
close() |
File |
completeFileNameFromHeader(File targetFileOrDir)
从响应头补全下载文件名
|
String |
contentEncoding()
获取内容编码
|
long |
contentLength()
获取内容长度,以下情况长度无效:
Transfer-Encoding: Chunked
Content-Encoding: XXX
参考:https://blog.csdn.net/jiang7701037/article/details/86304302
|
HttpCookie |
getCookie(String name)
获取Cookie
|
List<HttpCookie> |
getCookies()
获取Cookie
|
String |
getCookieStr()
获取本次请求服务器返回的Cookie信息
|
String |
getCookieValue(String name)
获取Cookie值
|
String |
getFileNameFromDisposition()
从Content-Disposition头中获取文件名
|
String |
getFileNameFromDisposition(String paramName)
从Content-Disposition头中获取文件名,以参数名为`filename`为例,规则为:
首先按照RFC5987规范检查`filename*`参数对应的值,即:`filename*="example.txt"`,则获取`example.txt`
如果找不到`filename*`参数,则检查`filename`参数对应的值,即:`filename="example.txt"`,则获取`example.txt`
按照规范,`Content-Disposition`可能返回多个,此处遍历所有返回头,并且`filename*`始终优先获取,即使`filename`存在并更靠前。
|
int |
getStatus()
获取状态码
|
boolean |
isChunked()
是否为Transfer-Encoding:Chunked的内容
|
boolean |
isDeflate()
是否为zlib(Deflate)压缩过的内容
|
boolean |
isGzip()
是否为gzip压缩过的内容
|
boolean |
isOk()
请求是否成功,判断依据为:状态码范围在200~299内。
|
HttpResponse |
sync()
同步
如果为异步状态,则暂时不读取服务器中响应的内容,而是持有Http链接的 InputStream 。 |
String |
toString() |
long |
writeBody(File targetFileOrDir)
将响应内容写出到文件
异步模式下直接读取Http流写出,同步模式下将存储在内存中的响应内容写出 写出后会关闭Http流(异步模式) |
long |
writeBody(File targetFileOrDir,
StreamProgress streamProgress)
将响应内容写出到文件
异步模式下直接读取Http流写出,同步模式下将存储在内存中的响应内容写出 写出后会关闭Http流(异步模式) |
long |
writeBody(File targetFileOrDir,
String tempFileSuffix,
StreamProgress streamProgress)
将响应内容写出到文件-避免未完成的文件
异步模式下直接读取Http流写出,同步模式下将存储在内存中的响应内容写出 写出后会关闭Http流(异步模式) 来自:https://gitee.com/dromara/hutool/pulls/407 此方法原理是先在目标文件同级目录下创建临时文件,下载之,等下载完毕后重命名,避免因下载错误导致的文件不完整。 |
long |
writeBody(OutputStream out,
boolean isCloseOut,
StreamProgress streamProgress)
|
long |
writeBody(String targetFileOrDir)
将响应内容写出到文件
异步模式下直接读取Http流写出,同步模式下将存储在内存中的响应内容写出 写出后会关闭Http流(异步模式) |
File |
writeBodyForFile(File targetFileOrDir,
StreamProgress streamProgress)
将响应内容写出到文件
异步模式下直接读取Http流写出,同步模式下将存储在内存中的响应内容写出 写出后会关闭Http流(异步模式) |
addHeaders, charset, charset, charset, clearHeaders, header, header, header, header, header, header, header, header, headerList, headerMap, headers, httpVersion, httpVersion, removeHeader, removeHeader
protected HttpConfig config
protected HttpConnection httpConnection
protected InputStream in
protected int status
protected HttpResponse(HttpConnection httpConnection, HttpConfig config, Charset charset, boolean isAsync, boolean isIgnoreBody)
httpConnection
- HttpConnection
config
- Http配置charset
- 编码,从请求编码中获取默认编码isAsync
- 是否异步isIgnoreBody
- 是否忽略读取响应体public int getStatus()
public boolean isOk()
public HttpResponse sync()
InputStream
。public String contentEncoding()
public long contentLength()
public boolean isGzip()
public boolean isDeflate()
public boolean isChunked()
public String getCookieStr()
public List<HttpCookie> getCookies()
public HttpCookie getCookie(String name)
name
- Cookie名HttpCookie
public String getCookieValue(String name)
name
- Cookie名public InputStream bodyStream()
public byte[] bodyBytes()
bodyBytes
in class HttpBase<HttpResponse>
public HttpResponse body(byte[] bodyBytes)
bodyBytes
- 主体public String body() throws HttpException
HttpException
- 包装IO异常public long writeBody(OutputStream out, boolean isCloseOut, StreamProgress streamProgress)
out
- 写出的流isCloseOut
- 是否关闭输出流streamProgress
- 进度显示接口,通过实现此接口显示下载进度public long writeBody(File targetFileOrDir, StreamProgress streamProgress)
targetFileOrDir
- 写出到的文件或目录streamProgress
- 进度显示接口,通过实现此接口显示下载进度public long writeBody(File targetFileOrDir, String tempFileSuffix, StreamProgress streamProgress)
targetFileOrDir
- 写出到的文件或目录tempFileSuffix
- 临时文件后缀,默认".temp"streamProgress
- 进度显示接口,通过实现此接口显示下载进度public File writeBodyForFile(File targetFileOrDir, StreamProgress streamProgress)
targetFileOrDir
- 写出到的文件streamProgress
- 进度显示接口,通过实现此接口显示下载进度public long writeBody(File targetFileOrDir)
targetFileOrDir
- 写出到的文件或目录public long writeBody(String targetFileOrDir)
targetFileOrDir
- 写出到的文件或目录的路径public void close()
close
in interface Closeable
close
in interface AutoCloseable
public String toString()
toString
in class HttpBase<HttpResponse>
public File completeFileNameFromHeader(File targetFileOrDir)
targetFileOrDir
- 目标文件夹或者目标文件public String getFileNameFromDisposition()
public String getFileNameFromDisposition(String paramName)
paramName
- 文件参数名,如果为null
则使用默认的`filename`Copyright © 2024. All rights reserved.