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();
//POST
Result 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/
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgejkkf
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01