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

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.kler.cn/a/458321.html

相关文章:

  • Linux(Centos 7.6)软件包安装
  • NodeRed使用心得,实现增删改查等
  • 电商项目高级篇07-redisson分布式锁
  • 排序算法之快速排序、归并排序
  • java全栈day21--Web后端实战之利用Mybaits查询数据
  • pd虚拟机 [po] Parallels Desktop 20 激活 for Mac [jie] 安装教程【支持M芯片】
  • 鸿蒙TCPSocket通信模拟智能家居模拟案例
  • 勤工助学系统|Java|SSM|VUE| 前后端分离
  • springboot510基于Springboot+vue线上教育培训办公系统(论文+源码)_kaic
  • JSON的运用与总结
  • 【Python科研数据爬虫】基于国家标准查询平台和能源标准化信息平台的海上风电相关行业标准查询信息爬取及处理
  • STM32-笔记16-定时器中断点灯
  • overleaf中出现TeX capacity exceeded PDF object stream buffer=5000000的原因和解决方案
  • pandas df 如何 输出数据到 sqlite3
  • Android studio-SDK无法安装的问题
  • LeetCode:3083. 字符串及其反转中是否存在同一子字符串(哈希 Java)
  • VM虚拟机配置ubuntu网络
  • 机器人C++开源库The Robotics Library (RL)使用手册(三)
  • 小程序配置文件 —— 16 项目配置文件和配置 sass
  • 拉取docker run hello-world失败