springboot使用RestTemplate+httpclient连接池发送http消息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 3.1 中使用 `RestTemplate` 和 `HttpClient 5`,你需要添加以下依赖项到你的 `pom.xml` 文件中: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>5.0.4</version> </dependency> </dependencies> ``` 然后,你需要创建一个 `RestTemplate` 的 Bean,使用 `HttpClient 5` 作为底层 HTTP 客户端。以下是示例代码: ```java import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.core5.http.config.Registry; import org.apache.hc.core5.http.config.RegistryBuilder; import org.apache.hc.core5.http.impl.io.PoolingHttpClientConnectionManager; import org.apache.hc.core5.http.io.SocketConfig; import org.apache.hc.core5.http.protocol.HttpRequestInterceptor; import org.apache.hc.core5.http.protocol.RequestAddCookies; import org.apache.hc.core5.http.protocol.RequestDefaultHeaders; import org.apache.hc.core5.http.protocol.ResponseProcessCookies; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { // 创建 HttpClient CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .build()) .setConnectionManager(new PoolingHttpClientConnectionManager( RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build())) .setDefaultSocketConfig(SocketConfig.custom() .setTcpNoDelay(true) .build()) .addInterceptorFirst((HttpRequestInterceptor) new RequestDefaultHeaders( Arrays.asList(new BasicHeader("User-Agent", "MyRestClient")))) // 设置 User-Agent .addInterceptorFirst(new RequestAddCookies()) .addInterceptorLast(new ResponseProcessCookies()) .build(); // 创建 RestTemplate HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); factory.setHttpClient(httpClient); RestTemplate restTemplate = new RestTemplate(factory); return restTemplate; } } ``` 这里我们创建了一个 `RestTemplate` 的 Bean,使用 `HttpClient 5` 作为底层 HTTP 客户端。我们还可以设置一些默认的请求配置和拦截器,例如设置 `User-Agent`、添加 Cookie 等。 现在你可以在你的代码中使用 `RestTemplate` 进行 HTTP 请求了: ```java @Autowired private RestTemplate restTemplate; public void doRequest() { String url = "http://example.com/api"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); String body = response.getBody(); // 处理响应数据 } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值