CloseableHttpResponse 类(代表一个可关闭的 HTTP 响应)

CloseableHttpResponse类是 Apache HttpClient 库中的一个类,代表一个可关闭的 HTTP 响应。当你使用 HttpClient 发送请求时,你会得到一个 CloseableHttpResponse 实例,它包含了服务器的响应数据和状态。处理完响应后,你应该关闭这个响应对象来释放底层的系统资源。

常用方法

  1. 获取响应实体

    • HttpEntity getEntity():返回响应实体,可能包含二进制数据或文本数据。
  2. 获取状态行

    • StatusLine getStatusLine():返回响应的状态行,包含 HTTP 版本和状态码。
  3. 获取所有头信息

    • Header[] getAllHeaders():返回响应的所有头信息。
  4. 获取特定的头信息

    • Header getFirstHeader(String name):返回指定名称的第一个头信息。
    • Header getLastHeader(String name):返回指定名称的最后一个头信息。
  5. 迭代头信息

    • HeaderIterator headerIterator():返回一个头信息迭代器。
    • HeaderIterator headerIterator(String name):返回指定名称的头信息迭代器。
  6. 关闭响应

    • void close():关闭响应并释放底层资源。

代码案例

案例 1 :使用 CloseableHttpResponse 获取和处理响应实体。

java 复制代码
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://example.com");

try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
    // 获取状态线对象,可以获取状态码和HTTP版本
    StatusLine statusLine = response.getStatusLine();
    System.out.println("HTTP version: " + statusLine.getProtocolVersion());
    System.out.println("Status code: " + statusLine.getStatusCode());

    // 获取响应实体
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        // 将响应实体转换为字符串
        String result = EntityUtils.toString(entity);
        System.out.println(result);
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    // 确保关闭 httpClient 资源
    try {
        httpClient.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

在这个例子中,我们创建了一个 HttpGet 对象来发送 GET 请求,然后执行请求并处理响应。我们打印了 HTTP 版本和状态码,并将响应实体转换为字符串。

案例 2 :使用 CloseableHttpResponse 获取和处理特定的响应头。

java 复制代码
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.Header;

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://example.com");

try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
    // 获取并打印 'Content-Type' 头信息
    Header contentTypeHeader = response.getFirstHeader("Content-Type");
    if (contentTypeHeader != null) {
        System.out.println("Content-Type: " + contentTypeHeader.getValue());
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    // 确保关闭 httpClient 资源
    try {
        httpClient.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

在这个例子中,我们获取了响应的第一个 Content-Type 头信息并打印了它的值。

在使用 CloseableHttpResponse 时,非常重要的一点是确保在处理完响应后调用 close() 方法来释放系统资源。从 Apache HttpClient 4.3.6 开始,可以使用 try-with-resources 语句来自动管理资源,如案例 1 所示。这确保了即使在发生异常的情况下,响应也会被正确关闭。

相关推荐
2501_9387918319 小时前
Rust Axum 框架开发后端服务:实现高性能 TCP 连接的处理逻辑
网络·tcp/ip·rust
北极光SD-WAN组网20 小时前
5G智慧网络如何实现异地组网?基于智能组网模块的解决方案解析
网络·5g
刘孬孬沉迷学习20 小时前
5G网络gNB与核心网(5GC)连接架构及传输协议
网络·网络协议·tcp/ip·5g·架构·udp·信息与通信
Xiaok101820 小时前
libpcap 抓包:从打开网卡到解析数据包
服务器·网络·php
爱奥尼欧20 小时前
【Linux笔记】网络部分——传输层协议TCP(1)
linux·运维·网络·笔记·tcp/ip·1024程序员节
lang2015092821 小时前
WebSocket子协议STOMP
网络·websocket·网络协议
饺子大魔王的男人21 小时前
3秒传输GB级文件:FastSend让P2P共享告别云存储依赖
网络·网络协议·p2p
tonydf1 天前
如何正确统计网络用户数量?别再被连接数和独立IP骗了!
后端·网络协议
一叶知秋yyds1 天前
openwrt 系统下通过命令行设置允许wan口进行Luci页面的访问
网络·openwrt·luci 开启wan 口访问
网络安全-海哥1 天前
Web安全深度实战:从漏洞挖掘到安全防护
网络·web安全·网络安全·网络攻击·转行