Java中的HTTP / 2响应头(HTTP/2 Response Header in Java)

编程入门 行业动态 更新时间:2024-10-19 19:34:40
Java中的HTTP / 2响应头(HTTP/2 Response Header in Java)

有没有办法在Java中获取HTTP / 2响应头? 我试图搜索URLConnection,Undertow甚至Jetty等一些库,但没有成功。

PS:我在Java Project上使用JDK 1.7。

另外,HTTP / 2的响应头是这样的吗?

GET / HTTP/1.1 Host: server.example.com Connection: Upgrade, HTTP2-Settings Upgrade: h2c HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload>

有没有网站使用过这个版本?

我的主要目标是知道一个网站是否使用HTTP / 2版本,是否有办法知道这一点而无需在Java项目中读取响应头?

谢谢。

Is there already a way to get the HTTP/2 response header in Java? I've tried to search in some libraries like URLConnection, Undertow or even Jetty, but without success.

P.S.: I'm using JDK 1.7 on my Java Project.

Other thing, the response header of the HTTP/2 is like this?

GET / HTTP/1.1 Host: server.example.com Connection: Upgrade, HTTP2-Settings Upgrade: h2c HTTP2-Settings: <base64url encoding of HTTP/2 SETTINGS payload>

Is there any website who use this version already?

My main goal is to know if a website use the HTTP/2 version or not, is there a way to know this without need to read the response header in a Java Project?

Thanks.

最满意答案

HTTP / 2网站通常使用TLS,因为浏览器仅通过TLS支持HTTP / 2。

您尝试使用的方法是HTTP / 1.1升级到HTTP / 2,很少有站点(如果有的话)支持。

您的代码片段代表请求 ,而不是响应。 如果升级成功,HTTP / 2服务器将以HTTP / 1.1格式发回101响应, 以HTTP / 2格式发送对GET请求的响应。 这在RFC 7540第3.2节中规定。

为了实现您想要的,即知道网站是否支持HTTP / 2,您必须尝试使用​​HTTP / 2 + TLS进行连接。 如果连接成功,则表示支持HTTP / 2。 如果失败,则不受支持。

对于Jetty(免责声明,我是HTTP / 2模块维护者),您必须使用JDK 8,代码如下所示:

// Setup.
HTTP2Client http2Client = new HTTP2Client();
SslContextFactory sslContextFactory = new SslContextFactory();
HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), sslContextFactory);
httpClient.start();

// Does webtide.com support HTTP/2 ?
ContentResponse response = httpClient.GET("https://webtide.com/");
// No errors, yes it supports HTTP/2 !
 

如果您收到没有错误的响应,则表示您使用HTTP / 2,否则服务器不支持HTTP / 2。

请记住,要使该代码起作用,您必须在bootclasspath中使用JDK 8和正确的ALPN引导jar,如此处所指定。

HTTP/2 websites typically use TLS, because browsers only support HTTP/2 over TLS.

The method you are trying to use is the HTTP/1.1 upgrade to HTTP/2 which very few sites - if any at all - support.

Your snippet of code represent a request, not a response. If the upgrade is successful, the HTTP/2 server sends back a 101 response in HTTP/1.1 format and the response to the GET request in HTTP/2 format. This is specified in RFC 7540, section 3.2.

In order to achieve what you want, i.e. to know if a website supports HTTP/2, you have to try to connect using HTTP/2 + TLS. If the connection succeeds, you know HTTP/2 is supported. If it fails, it's not supported.

For Jetty (disclaimer, I'm the HTTP/2 module maintainer), you have to use JDK 8, and the code will look like this:

// Setup.
HTTP2Client http2Client = new HTTP2Client();
SslContextFactory sslContextFactory = new SslContextFactory();
HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), sslContextFactory);
httpClient.start();

// Does webtide.com support HTTP/2 ?
ContentResponse response = httpClient.GET("https://webtide.com/");
// No errors, yes it supports HTTP/2 !
 

If you get a response without errors, you are on HTTP/2, otherwise the server does not support HTTP/2.

Remember that for that code to work, you have to use JDK 8 and the proper ALPN boot jar in the bootclasspath, as specified here.

更多推荐

本文发布于:2023-08-04 13:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HTTP   Java   Header   Response

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!