当前位置: 首页 > news >正文

Java HTTP client常见库

前言

每种编程语言里最常用的库恐怕是Http请求库了,如python里的requests包,nodejs里的request模块。
在Java世界里,也是百花齐放,山头林立。常用的有:

  • HttpURLConnection: 最早的JDK提供的类
  • Java 11提供的HttpClient
  • Apache HttpComponents项目中的HTTPClient
  • Square提供的OkHttpClient
  • Spring 自带的WebClient

Apache HttpComponents

该组件提供了两个核心类:

  • HttpCore: 更底层的传输处理类
  • HttpClient:基于HttpCore实现的HTTP-compliant 处理类

JDK 11+ HTTP Client使用举例

Post同步的json数据:

public void invokePost() {try {String requestBody = prepareRequest();HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://reqbin.com/echo/post/json")).POST(HttpRequest.BodyPublishers.ofString(requestBody)).header("Accept", "application/json").build();HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());System.out.println(response.body());} catch (IOException | InterruptedException e) {e.printStackTrace();}}private String prepareRequest() throws JsonProcessingException {var values = new HashMap<String, String>() {{put("Id", "12345");put("Customer", "Roger Moose");put("Quantity", "3");put("Price","167.35");}};var objectMapper = new ObjectMapper();String requestBody = objectMapper.writeValueAsString(values);return requestBody;}

发送异步请求:

public void invoke() throws URISyntaxException {HttpClient client = HttpClient.newBuilder().version(Version.HTTP_2).followRedirects(Redirect.NORMAL).build();HttpRequest request = HttpRequest.newBuilder().uri(new URI(URLConstants.URL)).GET().header(URLConstants.API_KEY_NAME, URLConstants.API_KEY_VALUE).timeout(Duration.ofSeconds(10)).build();client.sendAsync(request, BodyHandlers.ofString()).thenApply(HttpResponse::body).thenAccept(System.out::println).join();}

HTTP Client包装库

cVurl

cVurl is an open-source wrapper for the Java HTTP client. It is written in Java 11 and can be used with any JDK 11.0.2 or newer.

public void cVurl() {CVurl cVurl = new CVurl();//POSTResult result = cVurl.post("https://api.imgflip.com/caption_image").queryParams(Map.of("template_id", "112126428","username", "test-user","password", "123test321","text0", "text0","text1", "text1")).asObject(Result.class);System.out.println("CVurl POST: " + result);
}

它支持Compression、Multipart、Form data这些Java 11 HttpClient不具备的特性。

Avaje-HTTP

  • Fluid API for building URLs and payload
  • JSON marshaling using Avaje Jsonb/Jackson/Gson
  • Light Feign-style interfaces via annotation processing.
  • Request/Response Interception
  • Authorization via Basic Auth or OAuth Bearer Tokens
  • Async and sync API

个人建议

在实际项目中,设计符合自身项目需求的HTTP client接口,并基于JDK 11 HTTP client实现,独立于任何上述库。

参考链接

  • https://github.com/corese4rch/cvurl
  • https://github.com/avaje/avaje-http/tree/master
  • https://reflectoring.io/comparison-of-java-http-clients/
http://www.lryc.cn/news/113865.html

相关文章:

  • 【Java基础教程】(四十四)IO篇 · 上:File类、字节流与字符流,分析字节输出流、字节输入流、字符输出流和字符输入流的区别~
  • 电商数据获取:网络爬虫还是付费数据接口?
  • 树形结构——二叉树类型
  • JavaScript对象的方法与原型链
  • Oracle入门初探---第一章 批量创建表、索引并插入测试数据
  • 全面讲解最小二乘法
  • 【阻止IE强制跳转到Edge浏览器】
  • C++/Linux项目——日志系统(简介)
  • 【Redis面试题整理一】
  • 前端权限验证之自定义指令v-permission
  • c++使用条件变量实现生产消费问题(跨平台)
  • 怎么快速搭建BI?奥威BI系统做出了表率
  • Kafka3.4 SASL/kerberos/ACL 证以及 SSL 加密连接
  • UE中低延时播放RTSP监控视频解决方案
  • iOS - 开发者账号续订会员资格更换订阅的账号
  • 大数据课程F3——HIve的基本操作
  • top解析
  • 如何让子组件,router-view,呈现左右分布格局
  • 计算机网络—TCP和UDP、输入url之后显示主页过程、TCP三次握手和四次挥手
  • 使用反汇编工具IDA查看发生异常的汇编代码的上下文去辅助分析C++软件异常
  • 怎么合并多个视频?简单视频合并方法分享
  • webpack基础知识九:如何提高webpack的构建速度?
  • 批量改名字序号和前缀
  • 基于Spring Boot的医院预约挂号网站设计与实现(Java+spring boot+MySQL)
  • Linux命令200例:join将两个文件按照指定的键连接起来分析
  • 谈谈网络安全
  • 机器学习深度学习——文本预处理
  • Qt实现可伸缩的侧边工具栏(鼠标悬浮控制伸缩栏)
  • 【Spring Boot】拦截器与统一功能处理
  • RabbitMQ的6种工作模式