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

Hutool 发送 HTTP 请求的几种常见写法

最简单的 GET 请求:

String result = HttpUtil.get("https://www.baidu.com");

带参数的 GET 请求:

// 方法1: 直接拼接URL参数
String result = HttpUtil.get("https://www.baidu.com?name=张三&age=18");// 方法2: 使用 HashMap 构建参数
HashMap<String, Object> params = new HashMap<>();
params.put("name", "张三");
params.put("age", "18");
String result = HttpUtil.get("https://www.baidu.com", params);

POST 请求:

// 简单POST
String result = HttpUtil.post("https://www.baidu.com", "body content");// 带表单参数的POST
HashMap<String, Object> params = new HashMap<>();
params.put("name", "张三");
params.put("age", "18");
String result = HttpUtil.post("https://www.baidu.com", params);

发送 JSON:

HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("name", "张三");
paramMap.put("age", 18);String result = HttpRequest.post("https://www.baidu.com").header("Content-Type", "application/json").body(JSONUtil.toJsonStr(paramMap)).execute().body();

高度自定义的请求:

HttpRequest request = HttpRequest.post("https://www.baidu.com").header("Authorization", "Bearer token123").header("Custom-Header", "value").timeout(20000)  //超时时间20秒.form(params)    //表单参数.cookie("sessionId", "abc123");HttpResponse response = request.execute();
String result = response.body();
int status = response.getStatus();

文件上传:

HashMap<String, Object> params = new HashMap<>();
params.put("file", FileUtil.file("path/file.jpg"));
String result = HttpUtil.post("https://www.baidu.com/upload", params);

处理响应:

HttpResponse response = HttpRequest.get("https://www.baidu.com").execute();
// 获取响应状态码
int status = response.getStatus();
// 获取响应头
String contentType = response.header("Content-Type");
// 获取响应体
String body = response.body();
// 获取Cookies
List<HttpCookie> cookies = response.getCookies();

设置代理:

HttpRequest.get("https://www.baidu.com").setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888))).execute();

请求建议:

  1. 建议在生产环境中设置合适的超时时间
  2. 对于大量请求,使用 HttpUtil.createGet() 或 HttpUtil.createPost() 预创建请求对象
  3. 处理响应时注意异常处理
  4. 如果需要复用连接,考虑使用 HttpUtil.createHttp() 创建客户端

错误处理示例:

try {HttpResponse response = HttpRequest.get("https://www.baidu.com").timeout(20000).execute();if (response.isOk()) {String result = response.body();// 处理正常响应}
} catch (HttpException e) {// 处理超时等网络异常e.printStackTrace();
} catch (Exception e) {// 处理其他异常e.printStackTrace();
}
http://www.lryc.cn/news/512169.html

相关文章:

  • 【Linux】进度条
  • 【zookeeper核心源码解析】第四课:客户端与服务端读写的io核心流程
  • 强化学习蘑菇书笔记
  • 《机器学习》——线性回归模型
  • Linux(Centos 7.6)网卡信息没有了问题处理
  • WEB攻防-通用漏洞-文件上传-js验证-MIME验证-user.ini-语言特征
  • mybatis-plus代码生成器
  • 12.24-12.28Mysql锁阅读笔记
  • 支持最新 mysql9的workbench8.0.39 中文汉化教程来了
  • golang连接jenkins构建build
  • SCAU高程进阶题(自用)
  • 基于STM32F103控制L298N驱动两相四线步进电机
  • libreoffice在Windows和Linux环境的安装和结合Springboot使用教程
  • 前端开发 -- 自动回复机器人【附完整源码】
  • vue+echarts实现疫情折线图
  • 服务器nfs文件共享
  • 基于Vue+SSM+SpringCloudAlibaba的科目课程管理系统
  • vue3配置caddy作为静态服务器,在浏览器地址栏刷新出现404
  • 深入理解委托:C# 编程中的强大工具
  • 【Java 数据结构】合并两个有序链表
  • 基于微信小程序的校园访客登记系统
  • uniapp 判断多选、选中取消选中的逻辑处理
  • php8.0版本更新了哪些内容
  • Browser Use:AI智能体自动化操作浏览器的开源工具
  • Android笔记(四十):ViewPager2嵌套RecyclerView滑动冲突进一步解决
  • POS系统即销售点系统 文档与数据库设计
  • 安全合规遇 AI 强援:深度驱动行业发展新引擎 | 倍孜网络CEO聂子尧出席ICT深度观察报告会!
  • 算法进阶:贪心算法
  • C++ 设计模式:工厂方法(Factory Method)
  • 手机联系人 查询 添加操作