hutool 的HttpUtil是http1还是http2方式
Hutool 的 HttpUtil
工具类是基于 Apache HttpClient 4.x 实现的,因此默认使用的是 HTTP 1.1 协议。但是,HttpClient 4.3 及以上版本已经支持 HTTP 2 协议,可以通过特定的配置启用 HTTP 2 支持。
要启用 HttpClient 的 HTTP 2 支持,需要使用自定义的 HttpClientBuilder
对象进行配置。以下是一个示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class HttpUtilExample { public static void main(String[] args) throws IOException { CloseableHttpClient httpClient = HttpClientBuilder.create() .setSSLHostnameVerifier( new NoopHostnameVerifier()) .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE) .setConnectionManager( new PoolingHttpClientConnectionManager( RegistryBuilder.<ConnectionSocketFactory>create() .register( "http" , PlainConnectionSocketFactory.getSocketFactory()) .register( "https" , SSLConnectionSocketFactory.getSystemSocketFactory()) .build(), null , null , null , - 1 , TimeUnit.MILLISECONDS, new SystemDefaultDnsResolver())) .setRetryHandler( new DefaultHttpRequestRetryHandler( 3 , true )) .setUserAgent(HttpClients.USER_AGENT) .setProtocolVersion(HttpVersion.HTTP_2) .build(); String response = HttpUtil.createGet( "https://http2.pro/api/v1" ) .setHttpClient(httpClient) .execute() .body(); System.out.println(response); } } |
在上面的示例中,首先创建了一个自定义的 HttpClientBuilder
对象,并通过 setProtocolVersion()
方法将协议版本设置为 HTTP 2。然后使用该对象创建了一个 CloseableHttpClient
对象,并通过 setHttpClient()
方法将其设置为 HttpUtil
的默认 HttpClient。最后发送了一个 HTTP GET 请求,并输出了响应结果。
需要注意的是,在使用 HttpClient 发送 HTTP 2 请求时,还需要进行一些其他的配置,如设置 SSL HostnameVerifier、ConnectionReuseStrategy、ConnectionManager、RetryHandler 等。上面的示例代码已经包含了这些配置,可以直接使用。
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
2020-03-11 解决 org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
2013-03-11 MyEclipse 10.6 汉化和破解工具
2013-03-11 MyEclipse10安装subclipse插件的javaHL问题