# easyHttp **Repository Path**: hty741203776/easy-http ## Basic Information - **Project Name**: easyHttp - **Description**: 轻量级HTTP工具包,基本请求非常丝滑。待增加异步、上传、下载功能。 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 8 - **Forks**: 2 - **Created**: 2023-11-06 - **Last Updated**: 2024-01-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # easyHttp ![logo![输入图片说明](.idea/1699270797954_1697769451165_AD57455C-325D-4962-98D1-4387A3E69EE4.png)](.idea/1699270797954_1697769451165_AD57455C-325D-4962-98D1-4387A3E69EE4.png) #### 介绍 easyHttp 是一个轻量级的 HTTP 客户端工具包,专为 Java 设计,使得基本的 HTTP 请求变得异常简单。该库主要针对常见的 HTTP 请求提供了简洁的 API,使得开发者无需面对复杂的设置。当前版本已支持基本的请求方式,异步请求、文件上传、结果的流式处理已经开发完毕。 #### 特点 轻量级: 无需任何额外的依赖,只需要一个小的 JAR 文件。 简单易用: 只需几行代码即可发送 HTTP 请求。 接口调用频次限制:对于某些接口,它的调用是有频次的,我们增加了接口调用频次的限制,可以指定时间调用次数 扩展性: 为未来的功能提供了扩展支持,如异步请求、文件上传和下载。 #### 新增功能 1.接口频次限制,针对需要限制频次的接口,会自动根据频次来调用接口 ```java // 创建一个RequestConfig对象并配置请求参数 RequestConfig requestConfig = new RequestConfig(); // 限制次数 requestConfig.limitHttpRequest(6,1, TimeUnit.MINUTES); requestConfig.setRequestType(RequestType.TEST); requestConfig.setHttpMethod(HttpMethod.POST); requestConfig.setRequestBody(new StringBuilder("{\n" + "}").toString()); Map queryParam = new HashMap<>(); requestConfig.setQueryParams(queryParam); Map headerMap = new HashMap<>(); headerMap.put("X-API-KEY", "123456"); requestConfig.setHeaders(headerMap); // 创建适配器并发送请求 GenericHttpAdapter adapter = new GenericHttpAdapter<>(String.class); CustomResponse response = adapter.sendGenericRequest(requestConfig); // 处理响应 if (response.getStatus() == 200) { System.out.println("请求成功:" + response.getData()); } else { System.out.println("请求失败:" + response.getMsg()); } ``` 2.form请求支持,支持form请求 ```java public static void main(String[] args) { RequestConfig requestConfig = new RequestConfig(); requestConfig.setUrl("https://test"); requestConfig.setEndpoint("oauth/token"); requestConfig.setHttpMethod(HttpMethod.POST); // 请求头 Map headers = new HashMap<>(); headers.put("Content-Type","application/x-www-form-urlencoded"); // String requestBody = "grant_type=client_credentials&client_id=l70a99ddd0f0494643884386ad49660b26&client_secret=54f87417c85c4334aec93360624b4e1c"; Map form = new HashMap<>(); form.put("grant_type","client_credentials"); form.put("client_id","123"); form.put("client_secret","456"); GenericHttpAdapter adapter = new GenericHttpAdapter<>(String.class); requestConfig.setForm(form); CustomResponse response = adapter.sendGenericRequest(requestConfig); requestConfig.setHeaders(headers); // 处理响应 if (response.getStatus() == 200) { System.out.println("请求成功:" + response.getData()); } else { System.out.println("请求失败:" + response.getMsg()); } } ``` 3.支持异步请求 ```java public static void main(String[] args) { RequestConfig requestConfig = new RequestConfig(); requestConfig.setUrl("https://wwwcie.xxx.com"); requestConfig.setEndpoint("v1/oauth/token"); requestConfig.setHttpMethod(HttpMethod.POST); // 请求头 Map headers = new HashMap<>(); headers.put("Content-Type", "application/json"); Map form = new HashMap<>(); form.put("Content-Type", "application/x-www-form-urlencoded"); form.put("accept", "application/json"); GenericHttpAdapter adapter = new GenericHttpAdapter<>(String.class); requestConfig.setForm(form); requestConfig.setHeaders(headers); CompletableFuture> customResponseCompletableFuture = adapter.sendGenericRequestAsync(requestConfig); customResponseCompletableFuture .thenAccept(customResponse -> { // 处理响应 if (customResponse.getStatus() == 200) { System.out.println("请求成功:" + customResponse.getData()); } else { System.out.println("请求失败:" + customResponse.getMsg()); } }) .exceptionally(e -> { // 处理异常情况 // 例如: log.error("请求出错", e); return null; // 根据需要返回适当的值 }); } ``` 4.支持http代理,例如:请求cluade2 ```java public static void list_all_conversations() { REQUESTCONFIG.setUrl(BASE_URL); REQUESTCONFIG.setEndpoint("organizations/b691ac7da9ec8e50986/chat_conversations"); Map headers = new HashMap<>(); headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0"); headers.put("Accept-Language", "en-US,en;q=0.5"); headers.put("Referer", "https://claude.ai/chats"); headers.put("Content-Type", "application/json"); headers.put("Sec-Fetch-Dest", "empty"); headers.put("Sec-Fetch-Mode", "cors"); headers.put("Sec-Fetch-Site", "same-origin"); headers.put("Connection", "keep-alive"); headers.put("Cookie", "NFs="); REQUESTCONFIG.setHeaders(headers); REQUESTCONFIG.setProxyHost("10.18.10.10"); REQUESTCONFIG.setProxyPort(8888); GenericHttpAdapter adapter = new GenericHttpAdapter<>(String.class); CustomResponse response = adapter.sendGenericRequest(REQUESTCONFIG); // 处理响应 if (response.getStatus() == 200) { System.out.println("请求成功:" + response.getData()); } else { System.out.println("请求失败:" + response.getMsg()); } } ``` #### 快速开始 ##### 如何发送一个 HTTP 请求 非常简单 ```java public static void main(String[] args) { // 创建一个RequestConfig对象并配置请求参数 RequestConfig requestConfig = new RequestConfig(); requestConfig.setRequestType(RequestType.TEST); requestConfig.setHttpMethod(HttpMethod.POST); requestConfig.setRequestBody(new StringBuilder("{\n" + "}").toString()); Map queryParam = new HashMap<>(); requestConfig.setQueryParams(queryParam); Map headerMap = new HashMap<>(); headerMap.put("X-API-KEY", "123456"); requestConfig.setHeaders(headerMap); // 创建适配器并发送请求 GenericHttpAdapter adapter = new GenericHttpAdapter<>(String.class); CustomResponse response = adapter.sendGenericRequest(requestConfig); // 处理响应 if (response.getStatus() == 200) { System.out.println("请求成功:" + response.getData()); } else { System.out.println("请求失败:" + response.getMsg()); } } ``` #### 后续更新计划 . 支持文件下载和上传 . 支持动态 host 配置 . 支持沙箱模式 . 支持代理ip . 支持自动调度 #### 联系与支持 如有任何疑问或建议,欢迎联系: @email: 524430556@qq.com @csdn: https://blog.csdn.net/weixin_45487988?type=blog