漳州网站开发找出博大科技html5做网站链接

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

漳州网站开发找出博大科技,html5做网站链接,医院网站管理制度,一条龙建设网站目录 一、Apache HttpClient 5介绍 #xff08;1#xff09;核心特性 #xff08;2#xff09;Apache HttpClient 5 的新特性 #xff08;3#xff09;在 Java 项目的主要使用场景及缺点 使用场景#xff1a; 缺点#xff1a; 二、在实际项目中的应用 #xf…         目录 一、Apache HttpClient 5介绍 1核心特性 2Apache 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核心特性 功能强大 同步与异步支持支持同步和异步的 HTTP 请求处理异步请求在处理大量并发请求时能够显著提高效率。连接池管理内置的连接池机制能提高并发处理性能减少资源消耗。多种认证机制支持多种认证方式包括 Basic、Digest、NTLM、Kerberos 等能够处理多种安全场景。支持Cookie管理内置 Cookie 管理功能能够自动处理服务端返回的 Cookie 并在后续请求中使用模拟浏览器行为。 协议支持广泛 HTTP/1.1 和 HTTP/2 支持支持现代 HTTP 协议包括 HTTP/2 的多路复用和流优先级等特性提升了网络请求效率特别是 HTTP/2 多路复用的优势。SSL/TLS 支持支持 HTTPS提供自定义 SSL/TLS 配置确保通信的安全性。HttpClient 5 可以方便地处理 HTTPS 请求支持定制化的 SSL/TLS 配置。 灵活性和可扩展性 易于扩展和定制允许开发者根据需要进行灵活的定制HttpClient 5 的设计高度模块化用户可以根据需要对连接管理、重定向策略、请求重试策略、代理设置等进行灵活定制。代理支持可以轻松配置 HTTP 或 SOCKS 代理用于跨网络访问和隐私保护。 健壮的错误处理机制 自动重试和重定向处理内置的重试机制和自动重定向处理可以减少由于网络问题导致的失败请求。开发者可以定制是否启用自动重定向。 2Apache HttpClient 5 的新特性 与之前的 4.x 版本相比HttpClient 5 进行了大量的改进和优化特别是在性能和安全性方面 HTTP/2 支持提供了完整的 HTTP/2 支持包括多路复用、流优先级等特性。更好的异步支持在处理并发请求时通过异步模型极大提升了响应速度和吞吐量。灵活的响应处理通过改进的响应处理 API可以更方便地处理大型响应体避免内存溢出问题。 3在 Java 项目的主要使用场景及缺点 使用场景 RESTful API 客户端与第三方 API 交互发送各种 HTTP 请求。Web 爬虫抓取网页内容处理重定向、Cookie 等。分布式系统通信用于微服务间的 HTTP 通信。测试与自动化模拟 HTTP 请求进行集成和自动化测试。代理与网关处理请求代理和认证机制。文件上传下载实现大文件的传输。认证交互处理 OAuth2、JWT 等认证协议。安全通信处理 HTTPS 请求确保数据安全。 缺点 学习曲线陡峭高级特性如自定义连接池、SSL 配置、异步请求等较为复杂新手需要时间学习和掌握。体积较大相比轻量级客户端如 OkHttpHttpClient 依赖库较多可能不适合小型项目。性能消耗高默认配置下的内存和 CPU 占用较高需调整才能达到最佳性能。配置繁琐高级定制如连接管理、认证、代理需要较多配置增加开发复杂度。异步编程复杂异步请求的回调、错误处理等逻辑复杂增加代码难度。 二、在实际项目中的应用 1引入maven配置 首先你需要将 HttpClient 5 的依赖加入到项目中。pom.xml 文件如下 dependencygroupIdorg.apache.httpcomponents.client5/groupIdartifactIdhttpclient5/artifactIdversion5.1/version /dependency2自定义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, MapString, 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, MapString, 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(MapString, String map) {BasicHeader[] headers new BasicHeader[map.size()];int i 0;for (Map.EntryString, 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 closingex{}, ex.getMessage());}}}} 功能概述 初始化 HTTP 客户端init() 方法初始化 CloseableHttpClient配置连接池、请求超时和套接字超时。 连接池配置通过 PoolingHttpClientConnectionManagerBuilder 设置最大连接数1000和每路由的最大连接数50用于高并发场景。请求配置包括连接超时、请求超时和响应超时确保在合理的时间内处理请求。HTTP 请求处理 GET/HEAD 请求getOrHead() 方法可发送 GET 或 HEAD 请求并接受自定义请求头。POST 请求post() 方法可发送 POST 请求支持自定义请求头和实体。资源管理closeQuietly() 方法用于关闭资源避免抛出异常。 3代码中的具体实现 1、Get请求 GetMapping(/get)public void testGet() {String url http://xxx.com.cn;try {// 1、构建入参、添加 headersMapString, 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);MapString, 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在这里加MapString, 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();