项目简介
SmartHttpClient是一款强大的HTTP请求工具,提供简单、统一且一致的方式处理HTTP请求。它支持多种HTTP客户端实现,如OkHttp3、Apache HttpClient、Jdk HttpClient和Jodd HttpClient,能通过配置轻松切换不同实现,无需修改代码。同时具备丰富功能,如GET、POST、文件上传下载、多线程下载、断点续传等,可满足各类HTTP请求需求。
项目的主要特性和功能
- 统一的HTTP请求接口:定义统一接口,实现不同HTTP客户端无缝切换,支持GET、POST、文件上传下载等常见操作,提供丰富参数设置。
- 多客户端支持:支持OkHttp3、Apache HttpClient、Jdk HttpClient、Jodd HttpClient,可通过配置切换客户端实现,无需修改业务代码。
- 丰富的请求参数支持:支持URL和查询参数、请求头、请求体、文件上传、多线程下载、断点续传。
- 灵活的配置管理:可设置全局默认配置,如连接超时、读取超时、字符集等,也能针对每个请求单独设置参数,优先级逐渐升高。
- 拦截器支持:支持拦截器功能,可在请求发送前、响应返回后、发生错误时以及请求结束时执行自定义逻辑,用于日志记录、权限校验、参数校验等场景。
- 文件下载和上传:提供文件下载和上传功能,支持多线程下载和断点续传,适用于大文件下载,支持上传单个或多个文件并附带其他参数。
安装使用步骤
1. 引入依赖
在项目的build.gradle
或pom.xml
文件中添加以下依赖:
Gradle
groovy
dependencies {
implementation 'top.jfunc.network:unihttp-all:${version}'
}
Maven
xml
<dependency>
<groupId>top.jfunc.network</groupId>
<artifactId>unihttp-all</artifactId>
<version>${version}</version>
</dependency>
2. 创建SmartHttpClient实例
java
SmartHttpClient smartHttpClient = new JdkSmartHttpClient();
// 或者
SmartHttpClient smartHttpClient = new OkHttp3SmartHttpClient();
// 或者
SmartHttpClient smartHttpClient = new ApacheSmartHttpClient();
// 或者
SmartHttpClient smartHttpClient = new JoddSmartHttpClient();
3. 发送HTTP请求
GET请求
java
Response response = smartHttpClient.get("https://example.com/api/resource");
System.out.println(response.getBody());
POST请求
java
Request request = Request.of("https://example.com/api/resource")
.setBody("{\"name\":\"John\"}")
.setContentType(MediaType.APPLICATION_JSON);
Response response = smartHttpClient.post(request);
System.out.println(response.getBody());
文件上传
java
FormFile formFile = new FormFile(new File("path/to/file.txt"));
Request request = Request.of("https://example.com/api/upload")
.addFormFile(formFile);
Response response = smartHttpClient.upload(request);
System.out.println(response.getBody());
文件下载
java
Request request = Request.of("https://example.com/api/download/file.zip")
.setFile(new File("path/to/save/file.zip"));
File downloadedFile = smartHttpClient.download(request);
System.out.println(downloadedFile.getAbsolutePath());
4. 配置拦截器
java
smartHttpClient.setConfig(Config.defaultConfig()
.addInterceptor(new LoggingInterceptor())
.addInterceptor(new RequestIdInterceptor()));
5. 多线程下载
java
MultiThreadDownloader downloader = new MultiThreadDownloader(smartHttpClient, 5);
Request request = Request.of("https://example.com/api/download/largefile.zip")
.setFile(new File("path/to/save/largefile.zip"));
File downloadedFile = downloader.download(request);
System.out.println(downloadedFile.getAbsolutePath());
6. 断点续传
java
InterruptBaseDownloadFileDownloader downloader = new InterruptBaseDownloadFileDownloader(smartHttpClient);
Request request = Request.of("https://example.com/api/download/largefile.zip")
.setFile(new File("path/to/save/largefile.zip"));
File downloadedFile = downloader.download(request);
System.out.println(downloadedFile.getAbsolutePath());
通过以上步骤,可轻松使用SmartHttpClient进行各种HTTP请求操作,并根据需要进行配置和扩展。
下载地址
点击下载 【提取码: 4003】【解压密码: www.makuang.net】