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

西安网站建设地址厦门人才网597人才网

西安网站建设地址,厦门人才网597人才网,做书封面的网站,公司简介范本前言 每种编程语言里最常用的库恐怕是Http请求库了,如python里的requests包,nodejs里的request模块。 在Java世界里,也是百花齐放,山头林立。常用的有: HttpURLConnection: 最早的JDK提供的类Java 11提供的HttpClien…

前言

每种编程语言里最常用的库恐怕是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.zhongyajixie.com/news/49927.html

相关文章:

  • 网站程序设计上海做网站优化
  • 政府门户网站建设的重点网盘搜索引擎入口
  • 后端开发网站做一些什么百度网址ip
  • uni做网站首页seo网络推广到底是做什么的
  • 工业园区网站建设方案合肥网络推广营销
  • 凡客诚品网站推广关键词百度云
  • 龙陵县住房和城乡建设局网站微指数查询入口
  • 域名建设好了怎么在建设网站写手接单平台
  • 新手学做网站 pdf 网盘游戏推广论坛
  • 平台类网站做多久比较好的免费网站
  • wordpress直接上传视频seo排名查询工具
  • metinfo 建站sem是什么意思啊
  • 中企动力做网站服务怎么样新媒体营销策略有哪些
  • 郑州建设工程交易中心网站北京百度seo
  • 石家庄商城网站搭建多少钱抖音推广平台
  • 泰州专业做网站直通车关键词怎么选 选几个
  • 武汉电脑网站制作公司html网页制作软件有哪些
  • 学做莱网站北京seo优化厂家
  • 怎么做代刷网站教程中国科技新闻网
  • 怎么做网站设计程序西安优化排名推广
  • wordpress 数据 拆分seo优化的主要任务
  • 电子商务官方网站建设教育培训机构官网
  • 学网页设计报班上海网站营销seo方案
  • php网站开发过程互动营销公司
  • 除了个性化阶段东莞整站优化
  • 阿里巴巴批发网1688网官网seo权重查询
  • 合肥网络公司网站建设百度地图导航2022最新版
  • 学做披萨的网站广州seo团队
  • 旅行社网站 模板收录网站
  • 上海最好的网站是什么seo推广策划