当前位置: 首页 > news >正文

泰安房产信息网官网首页seo服务外包公司

泰安房产信息网官网首页,seo服务外包公司,网上110在线咨询,最近湖北疫情又严重了目录 一、Apache HttpClient 5介绍 (1)核心特性 (2)Apache HttpClient 5 的新特性 (3)在 Java 项目的主要使用场景及缺点 使用场景: 缺点: 二、在实际项目中的应用 &#xf…

        

目录

一、Apache HttpClient 5介绍

(1)核心特性

(2)Apache HttpClient 5 的新特性

(3)在 Java 项目的主要使用场景及缺点

使用场景:

缺点:

二、在实际项目中的应用

(1)引入maven配置

(2)自定义HttpUtils工具类---实现连接管理、重试策略等

功能概述

(3)代码中的具体实现

1、Get请求

2、POST请求-常规

3、POST请求-上传文件

(4)高级用法

1、处理重定向

2、SSL/TLS 支持

3、处理异步请求

4、处理 Cookie

5、HTTPDNS支持


一、Apache HttpClient 5介绍

Apache HttpClient 5 是一个功能齐全且高度可定制的 HTTP 客户端库, 其专门用于发送 HTTP 请求、处理 HTTP 响应并支持各种 HTTP 协议特性。特别适合处理复杂的 HTTP 请求需求,如多协议支持、认证、连接池、代理等。它适合中大型项目或需要高级 HTTP 特性的应用开发。

(1)核心特性

  1. 功能强大:

    • 同步与异步支持:支持同步和异步的 HTTP 请求处理,异步请求在处理大量并发请求时能够显著提高效率。
    • 连接池管理:内置的连接池机制能提高并发处理性能,减少资源消耗。
    • 多种认证机制:支持多种认证方式,包括 Basic、Digest、NTLM、Kerberos 等,能够处理多种安全场景。
    • 支持Cookie管理:内置 Cookie 管理功能,能够自动处理服务端返回的 Cookie 并在后续请求中使用,模拟浏览器行为。
  2. 协议支持广泛:

    • HTTP/1.1 和 HTTP/2 支持:支持现代 HTTP 协议,包括 HTTP/2 的多路复用和流优先级等特性,提升了网络请求效率;特别是 HTTP/2 多路复用的优势。
    • SSL/TLS 支持:支持 HTTPS,提供自定义 SSL/TLS 配置,确保通信的安全性。HttpClient 5 可以方便地处理 HTTPS 请求,支持定制化的 SSL/TLS 配置。
  3. 灵活性和可扩展性:

    • 易于扩展和定制:允许开发者根据需要进行灵活的定制,HttpClient 5 的设计高度模块化,用户可以根据需要对连接管理、重定向策略、请求重试策略、代理设置等进行灵活定制。
    • 代理支持:可以轻松配置 HTTP 或 SOCKS 代理,用于跨网络访问和隐私保护。
  4. 健壮的错误处理机制:

    • 自动重试和重定向处理:内置的重试机制和自动重定向处理,可以减少由于网络问题导致的失败请求。开发者可以定制是否启用自动重定向。

(2)Apache HttpClient 5 的新特性

与之前的 4.x 版本相比,HttpClient 5 进行了大量的改进和优化,特别是在性能和安全性方面:

  • HTTP/2 支持:提供了完整的 HTTP/2 支持,包括多路复用、流优先级等特性。
  • 更好的异步支持:在处理并发请求时,通过异步模型极大提升了响应速度和吞吐量。
  • 灵活的响应处理:通过改进的响应处理 API,可以更方便地处理大型响应体,避免内存溢出问题。

(3)在 Java 项目的主要使用场景及缺点

使用场景:

  1. RESTful API 客户端:与第三方 API 交互,发送各种 HTTP 请求。
  2. Web 爬虫:抓取网页内容,处理重定向、Cookie 等。
  3. 分布式系统通信:用于微服务间的 HTTP 通信。
  4. 测试与自动化:模拟 HTTP 请求,进行集成和自动化测试。
  5. 代理与网关:处理请求代理和认证机制。
  6. 文件上传下载:实现大文件的传输。
  7. 认证交互:处理 OAuth2、JWT 等认证协议。
  8. 安全通信:处理 HTTPS 请求,确保数据安全。

缺点:

  1. 学习曲线陡峭:高级特性(如自定义连接池、SSL 配置、异步请求等)较为复杂,新手需要时间学习和掌握。
  2. 体积较大:相比轻量级客户端,如 OkHttp,HttpClient 依赖库较多,可能不适合小型项目。
  3. 性能消耗高:默认配置下的内存和 CPU 占用较高,需调整才能达到最佳性能。
  4. 配置繁琐:高级定制(如连接管理、认证、代理)需要较多配置,增加开发复杂度。
  5. 异步编程复杂:异步请求的回调、错误处理等逻辑复杂,增加代码难度。

二、在实际项目中的应用

(1)引入maven配置

首先,你需要将 HttpClient 5 的依赖加入到项目中。pom.xml 文件如下:

<dependency><groupId>org.apache.httpcomponents.client5</groupId><artifactId>httpclient5</artifactId><version>5.1</version>
</dependency>

(2)自定义HttpUtils工具类---实现连接管理、重试策略等

import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.DnsResolver;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.util.Timeout;
import org.springframework.stereotype.Component;import java.io.Closeable;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Map;/*** @author 响叮当* @since 2024/8/15 14:10**/
@Slf4j
@Component
public class ApacheHttpClientUtil {private CloseableHttpClient httpClient;public void init() {SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(Timeout.ofMilliseconds(1000)).build();PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create().setDefaultSocketConfig(socketConfig).setMaxConnTotal(1000).setMaxConnPerRoute(50).build();RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(Timeout.ofMilliseconds(8000)).setResponseTimeout(Timeout.ofMilliseconds(8000)).setConnectTimeout(Timeout.ofMilliseconds(8000)).build();httpClient = HttpClients.custom().disableContentCompression().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();}public CloseableHttpResponse getOrHead(String url, String method, Map<String, String> headers) throws IOException {HttpUriRequestBase request = new HttpUriRequestBase(method, URI.create(url));BasicHeader[] head = mapToHeaders(headers);request.setHeaders(head);return httpClient.execute(request);}public CloseableHttpResponse post(String url, Map<String, String> headers, HttpEntity httpEntity) throws IOException {HttpPost request = new HttpPost(url);BasicHeader[] head = mapToHeaders(headers);request.setHeaders(head);request.setEntity(httpEntity);return httpClient.execute(request);}public static BasicHeader[] mapToHeaders(Map<String, String> map) {BasicHeader[] headers = new BasicHeader[map.size()];int i = 0;for (Map.Entry<String, String> entry : map.entrySet()) {headers[i++] = new BasicHeader(entry.getKey(), entry.getValue());}return headers;}public static void closeQuietly(Closeable is) {if (is != null) {try {is.close();} catch (Exception ex) {log.error("Resources encounter an exception when closing,ex:{}", ex.getMessage());}}}}

功能概述

  1. 初始化 HTTP 客户端init() 方法初始化 CloseableHttpClient,配置连接池、请求超时和套接字超时。
    • 连接池配置:通过 PoolingHttpClientConnectionManagerBuilder 设置最大连接数(1000)和每路由的最大连接数(50),用于高并发场景。
    • 请求配置:包括连接超时、请求超时和响应超时,确保在合理的时间内处理请求。
  2. HTTP 请求处理
    • GET/HEAD 请求getOrHead() 方法可发送 GET 或 HEAD 请求,并接受自定义请求头。
    • POST 请求post() 方法可发送 POST 请求,支持自定义请求头和实体。
  3. 资源管理closeQuietly() 方法用于关闭资源,避免抛出异常。

(3)代码中的具体实现

1、Get请求

   @GetMapping("/get")public void testGet() {String url = "http://xxx.com.cn";try {// 1、构建入参、添加 headersMap<String, String> headers = new HashMap<>();// 2、发起http请求CloseableHttpResponse httpResponse = apacheHttpClientUtil.getOrHead(url, "GET", headers);// 3、返回结果,异常处理if (httpResponse.getCode() == 200) {String resStr = EntityUtils.toString(httpResponse.getEntity());log.info("request_success, response:{}, httpResponse_Code:{}, reasonPhrase:{}", resStr, httpResponse.getCode(), httpResponse.getReasonPhrase());} else {log.error("request_fail, httpResponse_Code:{}, reasonPhrase:{}", httpResponse.getCode(), httpResponse.getReasonPhrase());}} catch (Exception e) {log.error("request_udmp_fail, ex:{}", e);}}

2、POST请求-常规

    @PostMapping("/post")public void testPost(@RequestBody PostReq req) {String url = "http://xxx.com.cn";try {// 1、构建入参、添加 headersStringEntity stringEntity = new StringEntity(JSONUtil.toJsonStr(req), ContentType.APPLICATION_JSON);Map<String, String> headers = new HashMap<>();// 2、发起http请求CloseableHttpResponse httpResponse = apacheHttpClientUtil.post(url, headers, stringEntity);// 3、返回结果,异常处理if (httpResponse.getCode() == 200) {String resStr = EntityUtils.toString(httpResponse.getEntity());log.info("request_success, response:{}, httpResponse_Code:{}, reasonPhrase:{}", resStr, httpResponse.getCode(), httpResponse.getReasonPhrase());} else {log.error("request_fail, httpResponse_Code:{}, reasonPhrase:{}", httpResponse.getCode(), httpResponse.getReasonPhrase());}} catch (Exception e) {log.error("request_udmp_fail, ex:{}", e);}}

3、POST请求-上传文件

    /*** 上传文件* @param multipartFile* @param token* @param key* @return*/@PostMapping("/uploadFile")public UploadRes testPostFile(@RequestParam("file") MultipartFile multipartFile,@RequestParam("token") String token,@RequestParam("key") String key) {UploadRes res = null;String url = "http://xxx.com.cn";try {// 1、构建File参数File file = new File(multipartFile.getOriginalFilename());try (FileOutputStream fos = new FileOutputStream(file)) {fos.write(multipartFile.getBytes());}MultipartEntityBuilder builder = MultipartEntityBuilder.create();builder.addBinaryBody("file", file, ContentType.MULTIPART_FORM_DATA, "ex.xlsx");// 2、构建其他参数builder.addTextBody("token", token, ContentType.TEXT_PLAIN);builder.addTextBody("key", key, ContentType.TEXT_PLAIN);HttpEntity multipartEntity = builder.build();// 3、需要加 header,在这里加Map<String, String> headers = new HashMap<>();// 4、发起http请求CloseableHttpResponse httpResponse = apacheHttpClientUtil.post(url, headers, multipartEntity);// 5、返回结果,异常处理if (httpResponse.getCode() == 200) {String resStr = EntityUtils.toString(httpResponse.getEntity());res = JSONUtil.toBean(resStr, UploadRes.class);log.info("request_success, response:{}, httpResponse_Code:{}, reasonPhrase:{}", resStr, httpResponse.getCode(), httpResponse.getReasonPhrase());} else {log.error("request_fail, httpResponse_Code:{}, reasonPhrase:{}", httpResponse.getCode(), httpResponse.getReasonPhrase());}} catch (Exception e) {log.error("request_udmp_fail, ex:{}", e);}return res;}

(4)高级用法

1、处理重定向

HttpClient 5 默认会处理 3XX 重定向,但你也可以自定义行为。

CloseableHttpClient httpClient = HttpClients.custom().disableRedirectHandling()  // 禁用自动重定向.build();

2、SSL/TLS 支持

使用 HttpClient 5 可以轻松处理 HTTPS 请求,下面展示如何自定义 SSL 配置。

import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import javax.net.ssl.SSLContext;SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial((chain, authType) -> true)  // 信任所有证书.build();CloseableHttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();

3、处理异步请求

如果你需要发送异步 HTTP 请求,可以使用 HttpAsyncClient

import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.client5.http.classic.methods.HttpGet;CloseableHttpAsyncClient asyncClient = HttpAsyncClients.createDefault();asyncClient.start();HttpGet request = new HttpGet("https://jsonplaceholder.typicode.com/posts/1");asyncClient.execute(request, new FutureCallback<>() {@Overridepublic void completed(CloseableHttpResponse response) {System.out.println("Response received: " + response.getCode());}@Overridepublic void failed(Exception ex) {System.out.println("Request failed: " + ex.getMessage());}@Overridepublic void cancelled() {System.out.println("Request cancelled");}
});

4、处理 Cookie

import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.cookie.CookieStore;CookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

5、HTTPDNS支持

    // 当域名为www.baidu.com结尾时候,转到local的机器上String Target_IP= "127.0.0.1";String[] domains = new String[]{"www.baidu.com"};DnsResolver dnsResolver = new DnsResolver() {@Overridepublic InetAddress[] resolve(String host) throws UnknownHostException {if (host.endsWith(domains[0]))) {return new InetAddress[]{InetAddress.getByName(Target_IP)};}return new InetAddress[0];}@Overridepublic String resolveCanonicalHostname(String s) {return null;}};// @PostConstructpublic void init() {SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(Timeout.ofMilliseconds(1000)).build();PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create().setDnsResolver(dnsResolver) // 这里是HttpDns配置.setDefaultSocketConfig(socketConfig).setMaxConnTotal(1000).setMaxConnPerRoute(50).build();


文章转载自:
http://clearheaded.bbmx.cn
http://wcdma.bbmx.cn
http://electuary.bbmx.cn
http://footle.bbmx.cn
http://astringently.bbmx.cn
http://fairish.bbmx.cn
http://demarch.bbmx.cn
http://versailles.bbmx.cn
http://loiasis.bbmx.cn
http://peso.bbmx.cn
http://subject.bbmx.cn
http://unanswered.bbmx.cn
http://papist.bbmx.cn
http://nema.bbmx.cn
http://achates.bbmx.cn
http://jargon.bbmx.cn
http://pretty.bbmx.cn
http://slakeless.bbmx.cn
http://chronobiology.bbmx.cn
http://verruca.bbmx.cn
http://heptavalence.bbmx.cn
http://micropublishing.bbmx.cn
http://zygotene.bbmx.cn
http://legation.bbmx.cn
http://support.bbmx.cn
http://deciduous.bbmx.cn
http://merrie.bbmx.cn
http://tahina.bbmx.cn
http://homeroom.bbmx.cn
http://gerundival.bbmx.cn
http://cautious.bbmx.cn
http://donnie.bbmx.cn
http://embourgeoisement.bbmx.cn
http://lincoln.bbmx.cn
http://gaize.bbmx.cn
http://keeve.bbmx.cn
http://kit.bbmx.cn
http://fletcherism.bbmx.cn
http://bascule.bbmx.cn
http://inquisitorial.bbmx.cn
http://guttural.bbmx.cn
http://confidant.bbmx.cn
http://poetaster.bbmx.cn
http://symphily.bbmx.cn
http://habana.bbmx.cn
http://neuraxon.bbmx.cn
http://durative.bbmx.cn
http://glade.bbmx.cn
http://fingerplate.bbmx.cn
http://dyeability.bbmx.cn
http://bryology.bbmx.cn
http://tragi.bbmx.cn
http://clothback.bbmx.cn
http://entreat.bbmx.cn
http://cookbook.bbmx.cn
http://inferrable.bbmx.cn
http://avoidable.bbmx.cn
http://accomplishable.bbmx.cn
http://breech.bbmx.cn
http://kanu.bbmx.cn
http://ambulate.bbmx.cn
http://narghile.bbmx.cn
http://gemmiform.bbmx.cn
http://op.bbmx.cn
http://schoolfellow.bbmx.cn
http://unfinished.bbmx.cn
http://book.bbmx.cn
http://tibiotarsus.bbmx.cn
http://hypothenar.bbmx.cn
http://conveyer.bbmx.cn
http://hen.bbmx.cn
http://wilsonian.bbmx.cn
http://squamose.bbmx.cn
http://modernism.bbmx.cn
http://mitospore.bbmx.cn
http://zenana.bbmx.cn
http://trysail.bbmx.cn
http://azov.bbmx.cn
http://unzipped.bbmx.cn
http://melanoma.bbmx.cn
http://quaverous.bbmx.cn
http://oppose.bbmx.cn
http://plagiocephalic.bbmx.cn
http://underrun.bbmx.cn
http://interwoven.bbmx.cn
http://grisliness.bbmx.cn
http://pyaemic.bbmx.cn
http://contrabassoon.bbmx.cn
http://gazel.bbmx.cn
http://demit.bbmx.cn
http://eluvial.bbmx.cn
http://galactogogue.bbmx.cn
http://demilune.bbmx.cn
http://rushed.bbmx.cn
http://electrotactic.bbmx.cn
http://toughly.bbmx.cn
http://reaffirmation.bbmx.cn
http://outclearing.bbmx.cn
http://stressor.bbmx.cn
http://bondieuserie.bbmx.cn
http://www.15wanjia.com/news/91336.html

相关文章:

  • wordpress 存储自定义段优化教程网官网
  • 有用模板网在线制作免费网站如何网上销售自己的产品
  • 西安南郊网站建设南宁今日头条最新消息
  • 网站单页做301厦门seo代运营
  • 网站建设及规划企业培训课程有哪些
  • 酒类网站建设方案案百度网站优化培训
  • wordpress windows 10网站优化公司大家好
  • 小型购物网站开发0元入驻的电商平台
  • 人脉做的最好的网站沧州搜索引擎优化
  • 做网站点击率赚钱吗seo优化排名
  • 学院网站建设作用什么是seo是什么意思
  • wordpress 简单西安网络推广优化培训
  • 合肥瑶海区网站建设价格百度动态排名软件
  • 网站wordpress错误商品推广
  • 论文网站的负载测试是如何做的百度竞价sem
  • wordpress主题pacify广州seo优化排名推广
  • 订票网站开发公司教育培训机构加盟
  • 公司品牌网站建设价格百度品牌专区
  • 中核二二公司真实情况奶糖 seo 博客
  • 建设好网站的在线沟通功能营销型网站建设报价
  • 青岛即墨网站开发免费网站申请域名
  • 学校自己做的网站需要买服务器吗如何找客户资源
  • 北京做网站建设的公司软文广告经典案例分析
  • 企业网站建设服务哪家好惠州seo推广优化
  • 网站开发命名规范百度快照推广有效果吗
  • 怎么用eclipse做网站开发网站信息查询
  • 建设银行住房公积网站seo优化教程自学
  • 花生壳做网站速度教育机构在线咨询
  • 网站没服务器行吗免费b2b
  • php网站开发实例教程代码广告推广渠道有哪些