Java - 发送 HTTP 请求的及其简单的方法模块 - hutool

目录

  • [一、POST 传递简单的字符串内容 .body(params)](#一、POST 传递简单的字符串内容 .body(params))
  • [二、POST 传递 Json 数据,以表单类型传递 .form(params)](#二、POST 传递 Json 数据,以表单类型传递 .form(params))
  • [二、POST 传递 Json 数据,以表单类型传递 .form(params) 和 .body(params) 方法效果等效的思路](#二、POST 传递 Json 数据,以表单类型传递 .form(params) 和 .body(params) 方法效果等效的思路)
  • [四、传统接口带 token 验证的代码模板](#四、传统接口带 token 验证的代码模板)
  • 参考链接

一、POST 传递简单的字符串内容 .body(params)

演示代码

java 复制代码
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import cn.hutool.http.HttpRequest;
/**
 * cf
 */
public class TqOdpServiceClient {

     private static String url="url";;
     public static String execute(String http,String params) {
         JSONObject response = JSONObject.parseObject(
         HttpRequest.post(http + url)
                 .body(params)
                 .execute()
                 .body()
                 );
         return response;
     }
}

二、POST 传递 Json 数据,以表单类型传递 .form(params)

演示代码

java 复制代码
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import cn.hutool.http.HttpRequest;
/**
 * cf
 */
public class TqOdpServiceClient {

     private static String url="url";;
     public static String execute(String http) {
     	HashMap<String, Object> params = new HashMap<>(2);
        params.put("test1", "测试数据");
        params.put("test2", "测试数据");
        JSONObject response = JSONObject.parseObject(
        HttpRequest.post(http + url)
                 .form(params)
                 .execute()
                 .body()
                 );
         return response;
     }
}

二、POST 传递 Json 数据,以表单类型传递 .form(params) 和 .body(params) 方法效果等效的思路

演示代码:可以看到 String newParams = JSON.toJSONString(params);将 HashMap 类型的数据转换为字符串类型,就可以作为字符串被传递到 body 内,后面就是对应接口的后端数据处理问题了。

java 复制代码
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import cn.hutool.http.HttpRequest;
/**
 * cf
 */
public class TqOdpServiceClient {

     private static String url="url";;
     public static String execute(String http) {
     	HashMap<String, Object> params = new HashMap<>(2);
        params.put("test1", "测试数据");
        params.put("test2", "测试数据");
        String newParams = JSON.toJSONString(params);
        JSONObject response = JSONObject.parseObject(
        HttpRequest.post(http + url)
                 .body(newParams)
                 .execute()
                 .body()
                 );
         return response;
     }
}

四、传统接口带 token 验证的代码模板

java 复制代码
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

/**
 * cf
 */
public class TqOdpServiceClient {

     private static String url="url";;
     public static String execute(String http, String accessToken) {
        JSONObject response = JSONObject.parseObject(
        HttpRequest.get(http + url)
        	.header(Header.AUTHORIZATION, "Bearer ".concat(accessToken))
            .execute()
            .body()
        	);
         return response;
     }
}

参考链接

java】hutool发送http请求,配置ssl忽略

SpringBoot 项目使用hutool 工具进行 http 接口调用的处理方法

相关推荐
SimonKing4 小时前
OpenCode AI辅助编程,不一样的编程思路,不写一行代码
java·后端·程序员
FastBean4 小时前
Jackson View Extension Spring Boot Starter
java·后端
Seven975 小时前
剑指offer-79、最⻓不含重复字符的⼦字符串
java
皮皮林55115 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河15 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程18 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅19 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者20 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺20 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端