【随笔】Java客户端选了HTTP/3,怎样知道请求实际走了哪个版本

服务端访问日志里有一条200,客户端构建时也选了HTTP_3。这两条信息足以证明这次请求使用了HTTP/3吗?前者记录处理结果,后者记录客户端偏好。要回答传输版本,还需要拿到这次请求自己的响应。

1. 新枚举带来了什么

JDK 26在java.net.http.HttpClient.Version中加入HTTP_3 。这让使用标准库的Java服务可以表达HTTP/3偏好,也能用同一个枚举记录实际响应版本。在客户端代码里,配置发生在发送之前,观测发生在响应返回之后。把两者记进不同的字段,升级期间才能看清哪些请求真的走上了新协议。JDK 26版本枚举

2. 一个只打印证据的客户端

保存为ProtocolProbe.java,使用JDK 26运行。把命令里的地址换成你有权访问的HTTPS测试服务。同一客户端连续发送两次只读GET,保留两次观测,以便发现首个请求与后续请求的差异。

java 复制代码
import java.net.URI;
import java.net.http.*;
import java.time.Duration;

public class ProtocolProbe {
    public static void main(String[] args) throws Exception {
        URI uri = URI.create(args[0]);
        try (var client = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_3)
                .connectTimeout(Duration.ofSeconds(5))
                .build()) {
            var request = HttpRequest.newBuilder(uri)
                    .timeout(Duration.ofSeconds(10)).GET().build();
            for (int n = 1; n <= 2; n++) {
                var response = client.send(request,
                        HttpResponse.BodyHandlers.discarding());
                System.out.printf("%d preferred=%s actual=%s status=%d%n",
                        n, client.version(), response.version(),
                        response.statusCode());
            }
        }
    }
}
sh 复制代码
java ProtocolProbe.java https://example.org/

preferred 读取客户端设置,actual 读取当前响应,status 提供HTTP状态码。若输出包含actual=HTTP_2、status=200,应解读为"这次HTTP/2请求成功"。只有actual=HTTP_3能直接支持"这次使用了HTTP/3"。域名、网络和服务配置都会影响样例输出;两次结果也可能相同。HttpResponse.version()

3. 为何普通网页能打开,HTTP/3仍需单独观察

HTTP/3把HTTP语义映射到QUIC,QUIC使用UDP。经典HTTPS服务的TCP路径可用,并不能同时验证UDP路径和服务端HTTP/3监听。响应头里的Alt-Svc还能告诉客户端某个源提供替代服务;它提供的是后续连接的线索,无法追溯证明携带这条响应头的请求已经使用替代服务。HTTP/3规范

观察服务端时先明确是哪一跳:若边缘节点终止客户端连接,源站日志记录的是边缘到源站的连接。两段可以选择不同版本。要验证Java应用到边缘的这一段,用客户端的响应版本和边缘接入日志对照,避免拿源站协议列去解释另一段连接。

4. 怎样做一次有意义的小范围上线

先保留状态码、实际版本和失败类型三个维度。成功收到HTTP响应时记录实际版本;连接超时等异常发生在拿到响应之前,应单列为请求失败。若在异常分支用客户端偏好填充实际版本,就会凭空制造一批"已使用HTTP/3"的记录。

然后确定验收目标。功能验收看成功响应和业务内容;协议验收看响应版本;性能比较还要控制响应体大小、连接复用以及网络条件。上面的探针只丢弃响应体并报告元数据,适合检查协议选择,无法给出哪种协议更快的结论。

5. 🧠 思维导图

#mermaid-svg-UBXHPc6UugPxHfSw{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-UBXHPc6UugPxHfSw .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-UBXHPc6UugPxHfSw .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-UBXHPc6UugPxHfSw .error-icon{fill:#552222;}#mermaid-svg-UBXHPc6UugPxHfSw .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-UBXHPc6UugPxHfSw .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-UBXHPc6UugPxHfSw .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-UBXHPc6UugPxHfSw .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-UBXHPc6UugPxHfSw .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-UBXHPc6UugPxHfSw .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-UBXHPc6UugPxHfSw .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-UBXHPc6UugPxHfSw .marker{fill:#333333;stroke:#333333;}#mermaid-svg-UBXHPc6UugPxHfSw .marker.cross{stroke:#333333;}#mermaid-svg-UBXHPc6UugPxHfSw svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-UBXHPc6UugPxHfSw p{margin:0;}#mermaid-svg-UBXHPc6UugPxHfSw .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-UBXHPc6UugPxHfSw .cluster-label text{fill:#333;}#mermaid-svg-UBXHPc6UugPxHfSw .cluster-label span{color:#333;}#mermaid-svg-UBXHPc6UugPxHfSw .cluster-label span p{background-color:transparent;}#mermaid-svg-UBXHPc6UugPxHfSw .label text,#mermaid-svg-UBXHPc6UugPxHfSw span{fill:#333;color:#333;}#mermaid-svg-UBXHPc6UugPxHfSw .node rect,#mermaid-svg-UBXHPc6UugPxHfSw .node circle,#mermaid-svg-UBXHPc6UugPxHfSw .node ellipse,#mermaid-svg-UBXHPc6UugPxHfSw .node polygon,#mermaid-svg-UBXHPc6UugPxHfSw .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-UBXHPc6UugPxHfSw .rough-node .label text,#mermaid-svg-UBXHPc6UugPxHfSw .node .label text,#mermaid-svg-UBXHPc6UugPxHfSw .image-shape .label,#mermaid-svg-UBXHPc6UugPxHfSw .icon-shape .label{text-anchor:middle;}#mermaid-svg-UBXHPc6UugPxHfSw .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-UBXHPc6UugPxHfSw .rough-node .label,#mermaid-svg-UBXHPc6UugPxHfSw .node .label,#mermaid-svg-UBXHPc6UugPxHfSw .image-shape .label,#mermaid-svg-UBXHPc6UugPxHfSw .icon-shape .label{text-align:center;}#mermaid-svg-UBXHPc6UugPxHfSw .node.clickable{cursor:pointer;}#mermaid-svg-UBXHPc6UugPxHfSw .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-UBXHPc6UugPxHfSw .arrowheadPath{fill:#333333;}#mermaid-svg-UBXHPc6UugPxHfSw .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-UBXHPc6UugPxHfSw .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-UBXHPc6UugPxHfSw .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-UBXHPc6UugPxHfSw .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-UBXHPc6UugPxHfSw .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-UBXHPc6UugPxHfSw .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-UBXHPc6UugPxHfSw .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-UBXHPc6UugPxHfSw .cluster text{fill:#333;}#mermaid-svg-UBXHPc6UugPxHfSw .cluster span{color:#333;}#mermaid-svg-UBXHPc6UugPxHfSw div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-UBXHPc6UugPxHfSw .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-UBXHPc6UugPxHfSw rect.text{fill:none;stroke-width:0;}#mermaid-svg-UBXHPc6UugPxHfSw .icon-shape,#mermaid-svg-UBXHPc6UugPxHfSw .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-UBXHPc6UugPxHfSw .icon-shape p,#mermaid-svg-UBXHPc6UugPxHfSw .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-UBXHPc6UugPxHfSw .icon-shape .label rect,#mermaid-svg-UBXHPc6UugPxHfSw .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-UBXHPc6UugPxHfSw .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-UBXHPc6UugPxHfSw .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-UBXHPc6UugPxHfSw :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 客户端偏好
发起请求
收到响应
连接失败
记录实际版本
记录状态码

总结要点

HTTP_3让Java代码可以表达协议偏好。要判断单次传输,读取响应版本,并说明测量的是哪一段连接。

上线记录要容得下"尚未收到响应"。保留这种状态,才能区分协议协商、请求失败和业务错误。

下一篇继续看Java客户端的超时设置,讨论连接超时和单次请求期限分别能解释哪类失败。

👉 如果你觉得这篇文章对你有所帮助,欢迎点赞、收藏、分享!😊

相关推荐
码云社区2 小时前
Java在线租房系统源码:VR看房素材、房源图片与内容维护方案
java·vue.js·vr·租房小程序·在线租房·看房租房
码云社区2 小时前
Spring Boot智慧物业系统源码:合同到期提醒、续签资料与资产协作
java·spring boot·后端·物业管理·物业系统·智慧物业
AI开源软件2 小时前
Vue3+SpringBoot3 业务配置统一管理:接管、保存与回退怎样保留有效值
java·前端·vue.js·spring boot·vue·ruoyi office
SemiTris3 小时前
Web 前端底层起源:HTML/CSS/JS、浏览器与 HTTP 完整脉络
http·html
陈如水3 小时前
Spring AI Alibaba 在多 Agent 场景下的实践
java·人工智能·spring
honker_X3 小时前
Python字符串全攻略:从入门到精通
开发语言·python
修仙之路3 小时前
2.1 HTTP 协议核心:请求响应模型、状态码、Header 与 RESTful 设计
网络协议·http
半杯咖啡半行码3 小时前
Qt 隐式共享(Implicit Sharing)深度解析:机制、陷阱与 Qt6迁移
开发语言·数据库·qt
吃我一窝jio3 小时前
Docker+Jenkins+K8s全流程部署
java·开发语言·笔记·python