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

相关推荐
TTGGGFF14 小时前
Supertonic 部署与使用全流程保姆级指南(附已部署镜像)
开发语言·python
木木木一14 小时前
Rust学习记录--C7 Package, Crate, Module
开发语言·学习·rust
love530love14 小时前
升级到 ComfyUI Desktop v0.7.0 版本后启动日志报 KeyError: ‘tensorrt‘ 错误解决方案
开发语言·windows·python·pycharm·virtualenv·comfyui·comfyui desktop
Evand J15 小时前
【MATLAB例程】【空地协同】UAV辅助的UGV协同定位,无人机辅助地面无人车定位,带滤波,附MATLAB代码下载链接
开发语言·matlab·无人机·无人车·uav·协同定位·ugv
chao18984415 小时前
基于MATLAB实现多变量高斯过程回归(GPR)
开发语言·matlab·回归
ytttr87320 小时前
隐马尔可夫模型(HMM)MATLAB实现范例
开发语言·算法·matlab
天远Date Lab20 小时前
Python实战:对接天远数据手机号码归属地API,实现精准用户分群与本地化运营
大数据·开发语言·python
listhi52020 小时前
基于Gabor纹理特征与K-means聚类的图像分割(Matlab实现)
开发语言·matlab
野生的码农20 小时前
码农的妇产科实习记录
android·java·人工智能
qq_4337764221 小时前
【无标题】
开发语言·php