当前位置: 首页 > news >正文

HttpClient

HttpClient

HttpClient 是用于发送 HTTP 请求和处理响应的工具包,在不同的编程语言中都有对应的实现,下面为你分别介绍 Java 和 Python 中的 HttpClient。

特点
  • 功能丰富:支持多种 HTTP 方法(如 GET、POST、PUT、DELETE 等),能处理复杂的 HTTP 协议特性,像身份验证、代理设置、连接池管理等。
  • 高度可定制:用户可以根据自身需求定制请求的各个方面,如设置请求头、超时时间、编码方式等。
  • 稳定性高:经过大量的测试和实际应用验证,具备良好的稳定性和性能。
  • 简洁易用:提供了简单直观的方法来发送各种 HTTP 请求,代码编写简洁。
  • 自动处理多种情况:可以自动处理重定向、Cookie、SSL 验证等常见的 HTTP 相关问题。
  • 广泛应用:在 Python 社区中被广泛使用,有丰富的文档和社区支持。

Jar包

<dependencies><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version> <!-- 正确版本号 --></dependency>
</dependencies>

基本方法

  • HttpClients:是工厂类,用于创建不同配置的 HttpClient 实例。
  • HttpClient:是接口,定义了发送 HTTP 请求、处理响应的基本行为。
  • HttpGet:继承自 HttpRequestBase,用于创建 HTTP GET 请求,从服务器获取资源。
  • HttpPost:继承自 HttpRequestBase,用于创建 HTTP POST 请求,向服务器提交数据。
  • Closeable:是 Java 的一个接口,实现该接口的对象可调用 close() 方法释放资源。
  • CloseableHttpClient:实现了 HttpClient 和 Closeable,是可关闭的 HTTP 客户端,可发送请求并处理响应。

示例

发送Get请求

/*** HttpClient发送Get请求* @throws IOException*/@Testpublic void HttpClientTestGet() throws IOException {//创建HttpClients的对象CloseableHttpClient httpClient = HttpClients.createDefault();//创建Http对象HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");//发送请求 接收响应结果CloseableHttpResponse response = httpClient.execute(httpGet);//获取浏览器状态码int httpcode=response.getStatusLine().getStatusCode();System.out.println("服务器状态码:"+httpcode);//获取响应体HttpEntity entity = response.getEntity();String s= EntityUtils.toString(entity);System.out.println(s);//关闭资源response.close();httpClient.close();}

发送Post请求

 /*** HttpClient发送Post请求*/@Testpublic void HttpClientTestPost() throws JSONException, IOException {//创建HttpClientCloseableHttpClient httpClient = HttpClients.createDefault();//创建Http对象HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");//构造发送数据JSONObject jsonObject = new JSONObject();jsonObject.put("username","admin");jsonObject.put("password","123456");StringEntity entity = new StringEntity(jsonObject.toString());//设置编码个数entity.setContentEncoding("utf-8");//数据格式entity.setContentType("application/json");//写入数据httpPost.setEntity(entity);//发送请求CloseableHttpResponse response = httpClient.execute(httpPost);//解析返回数据int statusCode=response.getStatusLine().getStatusCode();System.out.println("服务器状态码:"+statusCode);//获取响应体HttpEntity responseEntity = response.getEntity();String s=EntityUtils.toString(responseEntity);System.out.println(s);}

http://www.xdnf.cn/news/49123.html

相关文章:

  • 网络基础与 HTTP 协议
  • JavaScript forEach介绍(JS forEach、JS for循环)
  • 精益数据分析(7/126):打破创业幻想,拥抱数据驱动
  • 在 Node.js 中设置响应的 MIME 类型
  • Tailwindcss 入门 v4.1
  • rag搭建,是如何进行向量匹配检索的?
  • jsch(shell终端Java版)
  • LeRobot 项目部署运行逻辑(二)—— Mobile Aloha 真机部署
  • Vue3 打印网页内容
  • 通过Dify快速搭建本地AI智能体开发平台
  • 高边开关和低边开关的区别
  • 前端工程化之自动化部署
  • MVCC介绍
  • 《AI大模型应知应会100篇》第28篇:大模型在文本创作中的应用技巧
  • Matlab FCM模糊聚类
  • AI 编程工具——使用cursor创建一个mcp服务,并在cursor中调用
  • 使用LSTM动态调整SIMPLE算法松弛因子的CFD仿真训练程序
  • 使用tshark命令解析tcpdump抓取的数据包
  • 2025年4月19日
  • 【第四十一周】文献阅读:HippoRAG:受神经生物学启发的大型语言模型长期记忆机制
  • STM32 CubeMx下载及安装(一)
  • 【leetcode100】一和零
  • HarmonyOS-ArkUI-动画分类简介
  • javaSE.链表
  • 前端知识深度学习
  • [论文阅读]Making Retrieval-Augmented Language Models Robust to Irrelevant Context
  • ClickHouse简介
  • TCP常见知识点整理
  • 【C语言】char unsigned char signed char
  • Centos7 ftp、sftp无法使用ftp连接虚拟机