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

相关推荐
油丶酸萝卜别吃2 小时前
utils.js 说明文档
开发语言·javascript·ecmascript
rannn_1115 小时前
【力扣hot100】链表专题|160、206、234、141、142
java·算法·leetcode·链表·面试·开发
leisoo80975 小时前
100GBA股股票数据怎么存ClickHouseRedisMySQLJSON完整对比
大数据·linux·服务器·开发语言·python
不会代码的小猴5 小时前
21. 泛型编程上
开发语言·c++·笔记·算法
IKUN家族6 小时前
Spring IoC&DI
java·spring·rpc
一只旭宝6 小时前
细讲C加加【9】C++ std::function与std::bind详解|仿函数、绑定器、类成员绑定、占位符、成员偏移指针
开发语言·c++·算法
山荷枝7 小时前
03-框架--Spring
java·后端·spring
ChaHae-In8 小时前
SpringBoot统一功能处理
java·spring boot·后端
coder!mq8 小时前
说几个常见的语法糖?
java·开发语言·算法
gugucoding8 小时前
38. 【Java】Stream API(下):高级操作与性能
java·开发语言