http库 之 OKHttpUtil

源码位置

方便实用,个人感觉不错

依赖

xml 复制代码
<dependency>
    <groupId>io.github.admin4j</groupId>
    <artifactId>common-http-starter</artifactId>
    <version>0.7.5</version>
</dependency>

代码实践

java 复制代码
/**
 * 通用http的post接口,返回body的JsonNode对象
 * @param URI 资源地址
 * @param object post请求参数,requestBody
 */
private JsonNode CommonHttpRequest(String URI, Object object){
    JsonNode data = null;
    try {
    	//BaseURL为配置文件中配置的内容
        Response post = HttpUtil.post(BaseURL + URI, object);
        if (post == null || post.body() == null){
            log.error("no response, post or body is null");
            throw new Exception("no response");
        }
        //测试环境为springboot 2.x,所以使用的jackson
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(Objects.requireNonNull(post.body(), "No Response From server").string());
        log.info("response: {}", jsonNode);
        int code = jsonNode.get("code").asInt();
        if (code != 200){
            log.error("Request failed, code: [{}]", code);
            throw new Exception("server code error");
        }
        data = jsonNode.get("data");
    }catch (Exception e){
        log.error("Http process failed.", e);
        throw new BusinessException(CodeEnum.SYSTEM_EXCEPTION);
    }
    return data;
}

以上只是我写的一个测试函数,具体使用还是要看一下源码.

相关推荐
budaoweng06094 小时前
charles报错HTTP/1.1 200 Connection established
网络·网络协议·http
DLYSB_20 小时前
基于 HTTP API 与 Modbus 协议的多源监控声光语音联动告警系统设计与实现
网络·网络协议·http·报警灯
小邓的技术笔记1 天前
解析 gin 接收到 http 请求是怎么处理的
网络协议·http·gin
TlSfoward1 天前
TLSFOWARD抓包工具 TLS指纹
网络协议·http·百度·https
测试员周周2 天前
【skills5】一份 spec 生成 k6/JMeter/Locust:性能压测 + 全站截图,上线前的双保险
功能测试·网络协议·测试工具·jmeter·http·自动化·集成测试
GlobalSign数字证书2 天前
ACME 的 HTTP 验证和 DNS 验证,哪种更适合自动续期?
网络·网络协议·http
Keepingrun3 天前
http登录和授权
网络·网络协议·http
Keepingrun4 天前
HTTP基础
网络·网络协议·http
数据知道4 天前
HTTP/HTTPS 安全深度剖析:抓包、解密与证书陷阱
安全·http·https·mitmproxy·抓包解密
大师兄66685 天前
HarmonyOS7 HTTP 网络请求封装:统一拦截器实战
网络·网络协议·http·arkui·harmonyos7·实战讲解