Class HttpResponse

java.lang.Object
com.sure.tool.http.HttpResponse

public class HttpResponse extends Object
HTTP 响应:状态码、响应头、响应体(已完整读取并缓存,连接已释放),参考 Hutool 的 HttpResponse 设计。 与 HttpUtil 的"非 2xx 抛异常"语义不同:本类保留任意状态码,由调用方通过 isOk() 判断。 响应体字节、响应头均为不可变视图。
Since:
0.1.0
Author:
suretool
  • Method Details

    • getStatus

      public int getStatus()
      响应状态码。
      Returns:
      状态码
    • isOk

      public boolean isOk()
      是否 2xx 成功。
      Returns:
      是否成功
    • getUrl

      public String getUrl()
      最终请求 URL(跟随重定向后)。
      Returns:
      URL
    • headers

      public Map<String, List<String>> headers()
      全部响应头(大小写保持原始,值为不可变 List)。
      Returns:
      响应头 Map
    • headerValues

      public List<String> headerValues(String name)
      指定响应头的全部值(大小写不敏感),不存在返回空 List。
      Parameters:
      name - 头名
      Returns:
      头值列表
    • header

      public String header(String name)
      指定响应头的第一个值(大小写不敏感),不存在返回 null。
      Parameters:
      name - 头名
      Returns:
      头值或 null
    • contentType

      public String contentType()
      Content-Type 响应头,无则返回 null。
      Returns:
      Content-Type 或 null
    • getCharset

      public Charset getCharset()
      响应字符集:从 Content-Type 解析,解析失败或无 charset 时回退 UTF-8。
      Returns:
      字符集
    • body

      public String body()
      响应体文本(按 getCharset() 解码)。
      Returns:
      响应体
    • bodyBytes

      public byte[] bodyBytes()
      响应体字节(防御性拷贝)。
      Returns:
      响应体字节
    • cookies

      public Map<String,String> cookies()
      解析全部 Set-Cookie 响应头为 Cookie 键值 Map(同名取第一个,忽略 Path/Expires 等属性)。
      Returns:
      Cookie Map(无 Cookie 时为空 Map)
    • getCookie

      public String getCookie(String name)
      获取指定 Cookie 的值(来自 Set-Cookie 响应头),无则返回 null。
      Parameters:
      name - Cookie 名
      Returns:
      Cookie 值或 null