HttpClient
- Maven:
1 2 3 4 5 6 7 8 9 10 11 | <!--https: //github.com/apache/httpcomponents-client--> <dependency> <groupId>org.apache.httpcomponents.core5</groupId> <artifactId>httpcore5</artifactId> <version> 5.0 . 4 </version> </dependency> <dependency> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> <version> 5.0 . 4 </version> </dependency> |
- Get,Post的封装:HttpClientUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | import org.apache.hc.client5.http.ClientProtocolException; import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.ParseException; import org.apache.hc.core5.http.io.HttpClientResponseHandler; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.io.entity.StringEntity; import java.io.IOException; import java.net.URISyntaxException; import java.nio.charset.Charset; public class HttpClientUtil { public static String doGet(String url){ try { final CloseableHttpClient httpclient = HttpClients.createDefault(); //final HttpGet httpget = new HttpGet("https://api.doctorxiong.club/v1/fund/detail?code=000001&startDate=2020-09-01"); final HttpGet httpget = new HttpGet(url); System.out.println( "Executing request " + httpget.getMethod() + " " + httpget.getUri()); final HttpClientResponseHandler<String> responseHandler = new HttpClientResponseHandler<String>() { @Override public String handleResponse( final ClassicHttpResponse response) throws IOException { final int status = response.getCode(); if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) { final HttpEntity entity = response.getEntity(); try { return entity != null ? EntityUtils.toString(entity) : null ; } catch ( final ParseException ex) { throw new ClientProtocolException(ex); } } else { throw new ClientProtocolException( "Unexpected response status: " + status); } } }; final String responseBody = httpclient.execute(httpget, responseHandler); return responseBody; } catch (URISyntaxException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null ; } public static String doPost(String url,String params){ try { final CloseableHttpClient httpclient = HttpClients.createDefault(); final HttpPost httppost = new HttpPost(url); StringEntity entiry = new StringEntity(params, Charset.forName( "utf-8" )); httppost.setEntity(entiry); httppost.setHeader( "Content-Type" , "application/json" ); System.out.println( "Executing request " + httppost.getMethod() + " " + httppost.getUri()); final HttpClientResponseHandler<String> responseHandler = new HttpClientResponseHandler<String>() { @Override public String handleResponse( final ClassicHttpResponse response) throws IOException { final int status = response.getCode(); if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) { final HttpEntity entity = response.getEntity(); try { return entity != null ? EntityUtils.toString(entity) : null ; } catch ( final ParseException ex) { throw new ClientProtocolException(ex); } } else { throw new ClientProtocolException( "Unexpected response status: " + status); } } }; final String responseBody = httpclient.execute(httppost, responseHandler); return responseBody; } catch (URISyntaxException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null ; } } |
- 测试用例
1 2 3 4 5 6 7 8 | String responseBody = HttpClientUtil.doGet( "https://api.doctorxiong.club/v1/fund/detail?code=000001&startDate=2020-09-01" ); if (responseBody != null ){ JSONObject json = JSONObject.parseObject(responseBody); if ( "200" .equals(json.getString( "code" ))){ System.out.println( "第三方返回:" + json.getString( "data" )); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | JSONObject jsonObject = new JSONObject(); jsonObject.put( "fundType" ,Arrays.asList( "zs" )); jsonObject.put( "sort" , "z" ); jsonObject.put( "fundCompany" ,Arrays.asList( "80000248" )); jsonObject.put( "pageIndex" , 1 ); jsonObject.put( "pageSize" , 1 ); String responseBody = HttpClientUtil.doPost( "https://api.doctorxiong.club/v1/fund/rank" ,JSONObject.toJSONString(jsonObject)); if (responseBody != null ){ JSONObject json = JSONObject.parseObject(responseBody); if ( "200" .equals(json.getString( "code" ))){ System.out.println( "第三方返回:" + json.getString( "data" )); } } |
参考文档:http://hc.apache.org/httpcomponents-client-5.1.x/quickstart.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?