引入依赖
| <dependency> |
| <groupId>org.apache.httpcomponents</groupId> |
| <artifactId>httpclient</artifactId> |
| <version>4.5.0</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.httpcomponents</groupId> |
| <artifactId>httpclient</artifactId> |
| <version>${httpclient.version}</version> |
| <exclusions> |
| <exclusion> |
| <groupId>commons-codec</groupId> |
| <artifactId>commons-codec</artifactId> |
| </exclusion> |
| </exclusions> |
| </dependency> |
准备工具类
| package com.bm.test.utils; |
| |
| import org.apache.http.NameValuePair; |
| import org.apache.http.client.entity.UrlEncodedFormEntity; |
| import org.apache.http.client.methods.CloseableHttpResponse; |
| import org.apache.http.client.methods.HttpGet; |
| import org.apache.http.client.methods.HttpPost; |
| import org.apache.http.client.utils.URIBuilder; |
| import org.apache.http.entity.ContentType; |
| import org.apache.http.entity.StringEntity; |
| import org.apache.http.impl.client.CloseableHttpClient; |
| import org.apache.http.impl.client.HttpClients; |
| import org.apache.http.message.BasicNameValuePair; |
| import org.apache.http.util.EntityUtils; |
| import org.springframework.util.ObjectUtils; |
| |
| import java.io.IOException; |
| import java.net.URI; |
| import java.util.ArrayList; |
| import java.util.Map; |
| |
| public class HttpClientUtil { |
| |
| public static String doGet(String url, Map<String, String> params) { |
| |
| CloseableHttpClient httpClient = HttpClients.createDefault(); |
| |
| String resultStr = ""; |
| |
| CloseableHttpResponse response = null; |
| try { |
| |
| URIBuilder uriBuilder = new URIBuilder(url); |
| if (params != null) { |
| for (String key : params.keySet()) { |
| uriBuilder.addParameter(key, params.get(key)); |
| } |
| } |
| URI uri = uriBuilder.build(); |
| |
| HttpGet httpGet = new HttpGet(uri); |
| |
| response = httpClient.execute(httpGet); |
| |
| if (response.getStatusLine().getStatusCode() == 200) { |
| resultStr = EntityUtils.toString(response.getEntity(), "UTF-8"); |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| try { |
| if (response != null) { |
| response.close(); |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| return resultStr; |
| } |
| |
| public static String doGet(String url) { |
| return doGet(url, null); |
| } |
| |
| public static String doPost(String url, Map<String, String> params) { |
| |
| CloseableHttpClient httpClient = HttpClients.createDefault(); |
| |
| String resultStr = ""; |
| |
| CloseableHttpResponse response = null; |
| try { |
| |
| HttpPost httpPost = new HttpPost(url); |
| ArrayList<NameValuePair> paramList = new ArrayList<>(); |
| if (params != null) { |
| for (String key : params.keySet()) { |
| paramList.add(new BasicNameValuePair(key, params.get(key))); |
| } |
| UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); |
| httpPost.setEntity(entity); |
| } |
| |
| response = httpClient.execute(httpPost); |
| resultStr = EntityUtils.toString(response.getEntity(), "UTF-8"); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| try { |
| if (!ObjectUtils.isEmpty(response)) { |
| response.close(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| return resultStr; |
| } |
| |
| public static String doPost(String url) { |
| return doGet(url, null); |
| } |
| |
| public static String doPostJson(String url, String json) { |
| |
| CloseableHttpClient httpClient = HttpClients.createDefault(); |
| |
| String resultStr = ""; |
| |
| CloseableHttpResponse response = null; |
| try { |
| |
| HttpPost httpPost = new HttpPost(url); |
| |
| StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); |
| httpPost.setEntity(entity); |
| |
| response = httpClient.execute(httpPost); |
| resultStr = EntityUtils.toString(response.getEntity(), "UTF-8"); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| try { |
| response.close(); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| return resultStr; |
| } |
| } |
调用方法
| String state = "fail"; |
| Integer isOpened = 1; |
| Map<String, String> params = new HashMap<>(); |
| params.put("eid", "04F06D14-F1C9-4A46-BDFF-F25CA6D3A176"); |
| String result = HttpClientUtil.doPost("url", params); |
| if (!StringUtils.isEmpty(result)) { |
| JSONObject resultJSON = JSONObject.parseObject(result); |
| state = (String) resultJSON.get("status"); |
| isOpened = (Integer) resultJSON.get("is_opened"); |
| } else { |
| state = "ok"; |
| isOpened = 0; |
| } |
| if ("ok".equals(state) && 0 == isOpened) { |
| return R.error(); |
| } |
关注我的公众号SpaceObj 领取idea系列激活码

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!