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

重庆网站推广的网站杭州seo外包服务

重庆网站推广的网站,杭州seo外包服务,263企业邮箱登录入口收费,番禺网站建设设计TechZhi HTTP Client Starter 源码特性快速开始1. 添加依赖2. 配置3. 使用 主要功能支持的HTTP方法文件操作功能高级功能配置示例 API使用示例基本请求自定义请求异步请求文件操作示例错误处理 构建和测试依赖说明 本文将介绍一款本人开发的高性能Spring Boot HTTP客户端Starte…

TechZhi HTTP Client Starter

    • 源码
    • 特性
    • 快速开始
      • 1. 添加依赖
      • 2. 配置
      • 3. 使用
    • 主要功能
      • 支持的HTTP方法
      • 文件操作功能
      • 高级功能
      • 配置示例
    • API使用示例
      • 基本请求
      • 自定义请求
      • 异步请求
      • 文件操作示例
      • 错误处理
    • 构建和测试
    • 依赖说明

本文将介绍一款本人开发的高性能Spring Boot HTTP客户端Starter,它完美整合了OkHttp和Apache HttpClient。该项目旨在解决原生HTTP客户端以及RestTemplate在文件上传下载、异步请求、失败重试等场景下的复杂性,提供一个配置简单、功能强大的解决方案。

源码

https://github.com/ShouZhiDuan/easydo/blob/main/http-client-starter/README.md

特性

  • 🚀 高性能: 基于OkHttp和Apache HttpClient 5的高性能HTTP客户端
  • 🔧 易配置: 丰富的配置选项,支持连接池、超时、重试等
  • 🔐 安全支持: 支持SSL/TLS配置,可关闭证书校验
  • 🌐 代理支持: 支持HTTP和SOCKS代理配置
  • 📝 日志集成: 可配置的请求/响应日志记录
  • 🔄 重试机制: 内置智能重试机制
  • 💻 异步支持: 支持同步和异步HTTP请求
  • 🎯 类型安全: 自动JSON序列化/反序列化
  • 🛠️ Spring Boot集成: 无缝集成Spring Boot自动配置
  • 📁 文件操作: 完整的文件上传、下载和流处理支持
  • 📊 进度跟踪: 文件传输进度回调和监控
  • 🔍 智能检测: 自动文件类型检测和Content-Type映射

快速开始

1. 添加依赖

<dependency><groupId>com.techzhi.common</groupId><artifactId>http-client-starter</artifactId><version>1.0.0</version>
</dependency>

2. 配置

techzhi:http-client:enabled: trueclient-type: OK_HTTPconnect-timeout: 10sread-timeout: 30s# 更多配置选项请参考配置文档

3. 使用

@Service
public class MyService {@Autowiredprivate HttpClient httpClient;@Autowiredprivate HttpClientTemplate httpClientTemplate;public void example() {// 基本GET请求HttpResponse<String> response = httpClient.get("https://api.example.com/data");// 带类型转换的GET请求HttpResponse<User> userResponse = httpClient.get("https://api.example.com/user/1", User.class);// POST请求User newUser = new User("John", "john@example.com");HttpResponse<User> createResponse = httpClient.post("https://api.example.com/users", newUser, User.class);// 使用模板类HttpResponse<String> tokenResponse = httpClientTemplate.getWithToken("https://api.example.com/protected", "your-token");}
}

主要功能

支持的HTTP方法

  • GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
  • 同步和异步请求
  • 自动JSON序列化/反序列化
  • 表单提交支持

文件操作功能

  • 文件上传: 支持单个和多个文件上传
  • 多部分表单: multipart/form-data支持,可混合文件和表单字段
  • 文件下载: 支持大文件流式下载,自动创建目录
  • 进度回调: 上传和下载进度实时监控
  • 文件流: 获取文件输入流和字节数组
  • 类型检测: 自动文件类型检测和Content-Type映射
  • 异步操作: 支持异步文件上传和下载

高级功能

  • 连接池管理
  • 代理配置(HTTP/SOCKS)
  • SSL/TLS配置(支持关闭证书验证)
  • 重试机制
  • 请求/响应日志
  • Bearer Token和Basic认证

配置示例

techzhi:http-client:enabled: trueclient-type: OK_HTTP  # OK_HTTP 或 APACHE_HTTP_CLIENTconnect-timeout: 10sread-timeout: 30swrite-timeout: 30s# 连接池配置pool:max-total: 200default-max-per-route: 50time-to-live: 5m# 代理配置proxy:enabled: falsetype: HTTPhost: proxy.example.comport: 8080# SSL配置ssl:verify-hostname: trueverify-certificate-chain: true# 重试配置retry:enabled: truemax-attempts: 3retry-interval: 1s

API使用示例

基本请求

// GET请求
HttpResponse<String> response = httpClient.get("https://httpbin.org/get");// POST请求
Map<String, Object> data = Map.of("key", "value");
HttpResponse<String> postResponse = httpClient.post("https://httpbin.org/post", data);

自定义请求

HttpRequest request = HttpRequest.post("https://httpbin.org/post").header("Authorization", "Bearer token").header("Content-Type", "application/json").queryParam("page", "1").jsonBody(requestData);HttpResponse<ResponseData> response = httpClient.execute(request, ResponseData.class);

异步请求

CompletableFuture<HttpResponse<String>> future = httpClient.getAsync("https://httpbin.org/get");
future.thenAccept(response -> {System.out.println("Response: " + response.getBody());
});

文件操作示例

// 1. 单个文件上传
File file = new File("document.pdf");
HttpResponse<String> uploadResponse = httpClient.uploadFile("https://api.example.com/upload", "file", file
);// 2. 文件上传带进度回调
FileProgressCallback progressCallback = new FileProgressCallback() {@Overridepublic void onProgress(long bytesTransferred, long totalBytes, double percentage) {System.out.printf("上传进度: %.2f%%\n", percentage);}@Overridepublic void onComplete(long totalBytes) {System.out.println("上传完成: " + totalBytes + " bytes");}
};httpClient.uploadFile("https://api.example.com/upload", "file", file, progressCallback);// 3. 多文件上传
List<MultipartFile> files = Arrays.asList(MultipartFile.of("file1", new File("doc1.pdf")),MultipartFile.of("file2", new File("doc2.pdf")),MultipartFile.of("data", "info.json", jsonData.getBytes(), "application/json")
);HttpResponse<String> multiResponse = httpClient.uploadFiles("https://api.example.com/upload", files);// 4. 文件和表单数据混合上传
Map<String, String> formFields = new HashMap<>();
formFields.put("title", "文档标题");
formFields.put("category", "技术文档");httpClient.uploadFilesWithForm("https://api.example.com/upload", files, formFields);// 5. 文件下载
boolean downloadSuccess = httpClient.downloadFile("https://api.example.com/download/report.pdf", "/path/to/save/report.pdf"
);// 6. 文件下载带进度回调
httpClient.downloadFile("https://api.example.com/download/large-file.zip", "/path/to/save/large-file.zip", progressCallback
);// 7. 获取文件字节数组
byte[] fileBytes = httpClient.getFileBytes("https://api.example.com/download/small-file.txt");// 8. 获取文件流
try (InputStream fileStream = httpClient.getFileStream("https://api.example.com/download/data.csv")) {// 处理文件流BufferedReader reader = new BufferedReader(new InputStreamReader(fileStream));String line;while ((line = reader.readLine()) != null) {// 处理每一行}
}// 9. 异步文件操作
CompletableFuture<HttpResponse<String>> asyncUpload = httpClient.uploadFileAsync("https://api.example.com/upload", "file", file
);CompletableFuture<Boolean> asyncDownload = httpClient.downloadFileAsync("https://api.example.com/download/file.zip", "/path/to/save/file.zip"
);

错误处理

HttpResponse<String> response = httpClient.get("https://httpbin.org/status/404");if (response.isSuccessful()) {String data = response.getBody();
} else if (response.isClientError()) {System.out.println("Client error: " + response.getStatusCode());
} else if (response.isServerError()) {System.out.println("Server error: " + response.getStatusCode());
}

构建和测试

# 编译项目
mvn clean compile# 运行测试
mvn test# 打包
mvn clean package# 安装到本地仓库
mvn install

依赖说明

本项目支持以下HTTP客户端实现:

  • OkHttp: 默认实现,高性能、轻量级
  • Apache HttpClient 5: 功能丰富,企业级特性

根据项目需求选择合适的实现,通过配置techzhi.http-client.client-type来切换。

http://www.dtcms.com/wzjs/454399.html

相关文章:

  • 模块化网站建设360搜索首页
  • 网站做管制户外刀具万网建站
  • 重庆电商网站建设网页优化建议
  • vue做的pc线上网站百度联系方式人工客服
  • 网站报错解析天津搜狗seo推广
  • 美团网站做疏通广告怎样做搜索引擎推广
  • 前端用什么框架做网站商城网站开发公司
  • ps如何做网站导航图建立网站需要什么
  • wordpress菜单图标特效上海网站seo外包
  • 千库网网站百度关键词优化多少钱一年
  • 深圳专业专业网站设计中国搜索引擎排行榜
  • php网站开发环境的设置手机端关键词排名免费软件
  • 编程正规学校有哪几所黑帽seo是什么意思
  • asp.net 政府网站企业网站免费制作
  • 微网站模板代码网络营销战略的内容
  • 网站建设的心得免费刷赞网站推广免费
  • 400全国服务热线顺德手机网站建设公众号推广渠道
  • 网站的静态资源服务器怎么做2023年最新时政热点
  • 怎么注册app软件优化网站排名需要多少钱
  • 用cms建网站容易吗最近的电脑培训学校
  • 做h5小程序的网站青岛网站推广关键词
  • 上海做网站吧简述网站建设的基本流程
  • 淘宝网站做淘宝客建设网站的基本流程
  • 织梦网站如何更新系统百度快照在哪里找
  • 深圳龙华汽车网站建设域名排名查询
  • 建设班级网站手机google官网注册账号入口
  • 哪家网站开发网上怎么发布广告
  • 有公网ip 建网站网络营销推广的方式有哪些
  • 知名seo网站优化网站关键词如何快速上首页
  • 织梦网站如何做软件下载南京seo培训