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

相关推荐
代码中介商14 小时前
HTTP 完全指南(二):缓存机制深度详解
网络协议·http·缓存
c2385614 小时前
C++11final与override6、智能指针
开发语言·c++
*neverGiveUp*15 小时前
初步了解Django框架
开发语言·python·django
Java_2017_csdn15 小时前
在 Java 中,MessageFormat.format() 和 String.format() 函数对比?
java·开发语言·前端·数据库
绛洞花主敏明15 小时前
Go操作xorm中间表多对多关联实战
开发语言·后端·golang
噢,我明白了15 小时前
MyBatis-Plus 中IPage的分页查询
java·mybatis
Jun62615 小时前
QT(4)-EXCEL操作
开发语言·qt·excel
fengfuyao98515 小时前
基于MATLAB的HHT变换完整实现(含EMD分解与三维时频谱生成)
开发语言·算法·matlab
剑挑星河月15 小时前
98.验证二叉搜索树
java·算法·leetcode
我登哥MVP15 小时前
Spring Boot 从“会用”到“精通”:请求映射原理
java·spring boot·后端·spring·servlet·maven·intellij-idea