简体   繁体   English

如何使用 java.net.http.HttpClient 跳过中间状态代码(如 103)并只读最终状态代码

[英]How to skip intermediary status codes (like 103) with java.net.http.HttpClient and read only the final status code

I have a server which replies first with status code 103 ( and some headers) and then with 200 ( plus some other headers) curl -I output below:我有一个服务器,它首先回复状态码 103(和一些标题),然后回复 200(加上一些其他标题) curl -I output 下面:

HTTP/2 103  
link: <https://cdn.shopify.com>; rel=preconnect, <https://cdn.shopify.com>; crossorigin; rel=preconnect
    
HTTP/2 200  
date: Mon, 08 Aug 2022 10:28:30 GMT content-type: text/html; charset=utf-8 x-sorting-hat-podid: 236 x-sorting-hat-shopid: 366627 x-storefront-renderer-rendered: 1 set-cookie: secure_customer_sig=;

My java.net.http.HttpClient seems to detect only the first status code (103) and reads only the first headers.我的 java.net.http.HttpClient 似乎只检测到第一个状态码(103)并且只读取第一个标题。

 HttpClient httpClient = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_2)
                .followRedirects(HttpClient.Redirect.NORMAL)
                .connectTimeout(Duration.ofMinutes(1))
                .build();
 
 HttpRequest request = HttpRequest
    .newBuilder(httpReq, (name, value) -> true)
    .header("user-agent", "Mozilla/5.0 (X11....")
    //other headers here
    .build();

 HttpResponse<InputStream> response = httpClient.send(
                    request,
                    HttpResponse.BodyHandlers.ofInputStream()
            );

 logger.debug("call returned status code {}", response.statusCode());

The code above only shows 103 as status code ( which is an intermediary status code as you saw in the output of curl).上面的代码仅将 103 显示为状态代码(这是您在 curl 的 output 中看到的中间状态代码)。 How do I skip that status code + its headers?如何跳过该状态代码 + 它的标题? I'm only interested in the final (200) status code and the subsequent headers.我只对最终 (200) 状态代码和随后的标题感兴趣。

Unfortunately I have only a workaround which I will post here in order to help others: I changed the HTTP 2 to HTTP 1.1.不幸的是,我只有一个解决方法,我将在此处发布以帮助其他人:我将 HTTP 2 更改为 HTTP 1.1。 Feel free to find a better option.随意寻找更好的选择。 On my side I'm thinking about changing java's HttpClient with a more robust HttpClient implementation在我这边,我正在考虑使用更强大的 HttpClient 实现来更改 java 的 HttpClient

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM