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

jdk8使用okhttp发送http2请求

本文主要用于工作记录,在项目中遇到了就记录一下

在早期,原生的JDK8是不支持HTTP/2协议的,所以,要想使用这个特性,需要有web服务器和应用环境的支持,
例如:在VM中增加-Xbootclasspath/p:/Users/a1234/Downloads/alpn-boot-8.1.11.v20170118.jar来配合使用

但是从8u252开始,ALPN层已经从Java 11向后移植到了Java 8。意味着,只要使用Java
8u252或更新版本,不再要求使用Conscrypt和Jetty就可以使用HTTP/2了。

重点来了:一定要先检查自己的jdk版本是否大于8u252,然后就可以在项目中集成okhttp

项目pom配置

<!-- SpringBoot 依赖配置 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.7.2</version><type>pom</type><scope>import</scope>
</dependency><!-- okhttp 依赖配置 -->
<dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.9.3</version>
</dependency>

简单封装获取http2client请求

 	/*** 获取httpClient请求** @param maxTotalConnections             最大连接数* @param connectionKeepAliveTimeInMillis 最长连接保持活动时间* @return*/private static OkHttpClient createHttpClient(int maxTotalConnections, long connectionKeepAliveTimeInMillis) {ConnectionPool connectionPool = new ConnectionPool(maxTotalConnections, connectionKeepAliveTimeInMillis, TimeUnit.MILLISECONDS);return new OkHttpClient.Builder().followRedirects(false)
//                .protocols(Collections.singletonList(Protocol.H2_PRIOR_KNOWLEDGE)).retryOnConnectionFailure(true).connectionPool(connectionPool).build();}

GET请求示例

	/*** GET请求示例** @return* @throws IOException*/private String getTokenResStr() throws IOException {Request request = new Request.Builder().addHeader("Nonce",123).addHeader("Authorization", configData.getAuthorizationCode()).url(“url地址”).build();//GET by defaultOkHttpClient httpClient = createHttpClient(100, 30000);Response response = httpClient.newCall(request).execute();if (!response.isSuccessful()) {throw new IOException("Unexpected code " + response);}return response.body().string();}

POST请求示例

	/*** POST请求示例** @param orderId* @param tokenResStr* @return* @throws IOException*/private String getOrderDetail(String orderId, String tokenResStr) throws IOException {JSONObject tokenRes = JSONObject.parseObject(tokenResStr);// tokenString accessToken = tokenRes.getString("access_token");// token类型String tokenType = tokenRes.getString("token_type");String authorizationStr = firstUpperCase(tokenType) + " " + accessToken;Request request = new Request.Builder().addHeader("Authorization", authorizationStr).addHeader("Content-Type", "application/json").url(configData.getDetailRpcUrl() + orderId).build();OkHttpClient httpClient = createHttpClient(100, 30000);Response response = httpClient.newCall(request).execute();if (!response.isSuccessful()) {throw new IOException("Unexpected code " + response);}return response.body().string();
http://www.lryc.cn/news/103563.html

相关文章:

  • virbr是什么设备
  • MyBatis缓存-提高检索效率的利器--二级缓存
  • 开心档之CSS !important 规则
  • 深入篇【C++】手搓模拟实现list类(详细剖析底层实现原理)模拟实现正反向迭代器【容器适配器模式】
  • OnTrigger的几种情况
  • 地产变革中,物业等风来
  • (五)springboot实战——springboot自定义事件的发布和订阅
  • AVFoudation - 音频测量
  • 学习记录——TransNormerLLM
  • 【Qt】利用Tool Button控件创建下拉菜单按钮
  • 1.2 eureka注册中心,完成服务注册
  • 【100天精通python】Day20:文件及目录操作_os模块和os.psth模块,文件权限修改
  • 回归预测 | MATLAB实现PSO-GPR粒子群优化高斯过程回归多输入单输出回归预测
  • python_PyQt5开发验证K线视觉想法工具V1.1 _增加标记类型_线段
  • 中文多模态医学大模型智能分析X光片,实现影像诊断,完成医生问诊多轮对话
  • 企业服务器数据库被360后缀勒索病毒攻击后采取的措施
  • FFmpeg-两个文件mix重采样以那个为主
  • 【WebGL】初探WebGL,我了解到这些
  • fwft fifo和standard fifo
  • pdf阅读器哪个好用?这个阅读器别错过
  • 【LeetCode】下降路径最小和
  • 从0到1开发go-tcp框架【2-实现Message模块、解决TCP粘包问题、实现多路由机制】
  • Boost开发指南-3.6weak_ptr
  • Swift 周报 第三十三期
  • 网络空间安全及计算机领域常见英语单词及短语——网络安全(一)
  • Go基准测试Benchmark
  • docker容器的基本操作
  • MySQL绿色安装和配置
  • 《cuda c编程权威指南》03 - cuda小功能汇总
  • Java:Java程序通过执行系统命令调用Python脚本