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 接口调用的处理方法

相关推荐
CUMT_DJ1 小时前
matlab计算算法的运行时间
开发语言·算法·matlab
SUPER52662 小时前
FastApi项目启动失败 got an unexpected keyword argument ‘loop_factory‘
java·服务器·前端
咕噜咕噜啦啦2 小时前
Eclipse集成开发环境的使用
java·ide·eclipse
Overboom4 小时前
[C++] --- 常用设计模式
开发语言·c++·设计模式
Univin4 小时前
C++(10.4)
开发语言·数据结构·c++
KyollBM4 小时前
每日羊题 (质数筛 + 数学 | 构造 + 位运算)
开发语言·c++·算法
光军oi5 小时前
全栈开发杂谈————关于websocket若干问题的大讨论
java·websocket·apache
weixin_419658315 小时前
Spring 的统一功能
java·后端·spring
Paul_09206 小时前
golang面经——map模块和sync.Map模块
开发语言
小许学java6 小时前
Spring AI-流式编程
java·后端·spring·sse·spring ai