# http-client **Repository Path**: dufafei/http-client ## Basic Information - **Project Name**: http-client - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-13 - **Last Updated**: 2026-01-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # HTTP客户端 ## 概述 数据集成子项目: 1.插件扩展模块:https://gitee.com/dufafei/plugin 2.DAG解析模块:https://gitee.com/dufafei/dag 3. 4.函数模块:https://gitee.com/dufafei/fel 5.http客户端封装:https://gitee.com/dufafei/http-client ## 功能 支持常见认证方式和自定义认证方式 支持gzip压缩 ## 使用 ### 1.编程式使用 #### 1.1 支持执行前脚本预处理 ```java HttpRuntimeContext http = new HttpRuntimeContext(); ScriptProcessor scriptProcessor = new ScriptProcessor(); scriptProcessor.addVariable("http", http); scriptProcessor.compile( "http.addVariable('a', 'b1');http.request.headers.put('id','${a}');print(http)"); scriptProcessor.eval(); ``` #### 1.2 发送请求 ```java //1.multipart/form-data HttpRuntimeContext context = new HttpRuntimeContext(); context.getRequest().setMethod(HttpMethod.POST.getName()); context.getRequest().setPath("http://localhost:7079/http/test1"); context.getRequest().setContentType(HttpContentType.MULTIPART_FORM_DATA.getName()); context.getRequest().setBody("{\"id\":1,\"name\":\"读法非\"}"); HttpRequestFile file = new HttpRequestFile("file", "test.text", new FileInputStream(new File(System.getProperty("user.dir") + "/src/test/java/test.text"))); context.getRequest().addFile(file); client.send(context); System.out.println(context.getResponse().getBody()); //2.application/x-www-form-urlencoded DefaultHttpClient client = new DefaultHttpClient(new HttpRequestSettings()); HttpRuntimeContext context = new HttpRuntimeContext(); context.getRequest().setMethod(HttpMethod.POST.getName()); context.getRequest().setPath("http://localhost:7079/http/test2"); context.getRequest().setContentType(HttpContentType.APPLICATION_X_WWW_FORM_URLENCODED.getName()); context.getRequest().setBody("{\"id\":1,\"name\":\"读法非\"}"); client.send(context); System.out.println(context.getResponse().getBody()); //3.application/json DefaultHttpClient client = new DefaultHttpClient(new HttpRequestSettings()); HttpRuntimeContext context = new HttpRuntimeContext(); context.getRequest().setMethod(HttpMethod.POST.getName()); context.getRequest().setPath("http://localhost:7079/http/test3"); context.getRequest().setContentType(HttpContentType.APPLICATION_JSON.getName()); context.getRequest().setBody("{\"id\":1,\"name\":\"读法非\"}"); client.send(context); System.out.println(context.getResponse().getBody()); //4.application/octet-stream DefaultHttpClient client = new DefaultHttpClient(new HttpRequestSettings()); HttpRuntimeContext context = new HttpRuntimeContext(); context.getRequest().setMethod(HttpMethod.POST.getName()); context.getRequest().setPath("http://localhost:7079/http/test4"); context.getRequest().setContentType(HttpContentType.APPLICATION_OCTET_STREAM.getName()); HttpRequestFile file = new HttpRequestFile("file", "test.text", new FileInputStream(new File(System.getProperty("user.dir") + "/src/test/java/test.text"))); context.getRequest().addFile(file); System.out.println(context.getResponse().getBody()); //5. 文件下载 DefaultHttpClient client = new DefaultHttpClient(new HttpRequestSettings()); HttpRuntimeContext context = new HttpRuntimeContext(); context.getRequest().setMethod(HttpMethod.GET.getName()); context.getRequest().setPath("http://localhost:7079/http/test5"); context.getRequest().setContentType(HttpContentType.APPLICATION_OCTET_STREAM.getName()); context.getRequest().addParam("fileName", System.getProperty("user.dir") + "/src/test/java/test.text"); client.send(context, new FileDownloadCallback(System.getProperty("user.dir") + "/src/test/java/", "test1.text")); System.out.println(context.getResponse().getBody()); ``` #### 1.3 支持执行后脚本预处理 ```java HttpRuntimeContext http = new HttpRuntimeContext(); ScriptProcessor scriptProcessor = new ScriptProcessor(); scriptProcessor.compile("http.response.headers.put('id','${a}');print(http)"); scriptProcessor.eval(); ``` ### 2.在脚本里面发送请求 ```java HttpRequestSettings settings = new HttpRequestSettings(); settings.setEnableReplaceBody(true); HttpRuntimeContext http = new HttpRuntimeContext(); ScriptProcessor scriptProcessor = new ScriptProcessor(); scriptProcessor.addVariable("http", http); scriptProcessor.addVariable("settings", settings); scriptProcessor.compile("" + "http.addVariable('id', 2);" + "http.request.method = 'POST';" + "http.request.path = 'http://localhost:8080/http/test3';" + "http.request.contentType = 'application/json';" + "http.request.body = '{\"id\":${id},\"name\":\"读法非\"})';" + "http.send(settings);"); scriptProcessor.eval(); ``` ### 3.使用连接池 ```java DefaultHttpPoolClient client = new DefaultHttpPoolClient(new HttpRequestSettings()); ExecutorService executorService = Executors.newFixedThreadPool(10); for (int i = 0; i < 10; i++) { executorService.submit(()->{ while (true) { HttpRuntimeContext context = new HttpRuntimeContext(); context.getRequest().setMethod(HttpMethod.POST.getName()); context.getRequest().setPath("http://localhost:8080/http/test3"); context.getRequest().setContentType(HttpContentType.APPLICATION_JSON.getName()); context.getRequest().setBody("{\"id\":1,\"name\":\"读法非\"}"); try { client.send(context); } catch (Exception e) { e.printStackTrace(); } Thread.sleep(100); } }); } Thread.sleep(Integer.MAX_VALUE); ``` ### 4.监控连接池 连接池带有jmx监控,可以结合jolokia使用Rest API获取当前连接使用情况。 监控参数: ```java public interface HttpPoolClientMXBean { /** * @return 已租用连接数 * 当前正在被使用(即已被 HTTP 请求占用)的连接数量 * 反映了当前并发请求数量 * 如果该值长期接近 getMax(),说明连接池压力大,可能需要增加最大连接数或检查请求是否超时未释放 */ int getLeasedConnections(); /** * @return 待定/等待连接数 * 当前有多少个线程正在等待获取一个连接(因为连接池中没有可用连接,且已达到最大连接限制) * getPending() > 0,说明连接池已成为瓶颈,请求在排队,响应延迟会增加 * 持续为正数:必须优化(如增加 maxTotal / maxPerRoute,或检查连接未及时释放) */ int getPendingConnections(); /** * @return 可用连接数 * 当前处于空闲状态、可以立即被复用的连接数量 * 值高:说明连接池资源充足,请求可以快速获取连接 * 值低或为 0:新请求可能需要等待或创建新连接(如果未达上限) */ int getAvailableConnections(); /** * @return 最大连接数 * 该统计范围(总连接池或特定路由)允许的最大连接总数 * getTotalStats().getMax() 返回的是整个连接池的 maxTotal * getStats(route).getMax() 返回的是该路由的 maxPerRoute */ int getMaxConnections(); } ``` 执行脚本添加:-javaagent:D:\Work\project\http-client\lib\jolokia-jvm-1.7.2.jar=port=8778,host=localhost 浏览器请求,获取监控参数: http://localhost:8778/jolokia/read/org.apache.http.client:type=DefaultHttpPoolClient/MaxConnections ```json {"request":{ "mbean":"org.apache.http.client:type=DefaultHttpPoolClient", "attribute":"MaxConnections","type":"read"}, "value":50, "timestampValue":1755258839, "status":200 } ``` http://localhost:8778/jolokia/read/org.apache.http.client:type=DefaultHttpPoolClient/PendingConnections http://localhost:8778/jolokia/read/org.apache.http.client:type=DefaultHttpPoolClient/AvailableConnections http://localhost:8778/jolokia/read/org.apache.http.client:type=DefaultHttpPoolClient/MaxConnections ## 参考项目 ## 1.Forest 一款极大程度解放你的 HTTP 接入工作的强大 HTTP 客户端框架。 官方网站:http://forest.dtflyx.com Gitee托管仓库:https://gitee.com/dromara/forest Github托管仓库:https://github.com/dromara/forest