From 1b8ad0cc6a79f84fb32fdffae39742afa964748e Mon Sep 17 00:00:00 2001 From: Pu Zhibing <393733352@qq.com> Date: 星期二, 24 九月 2024 16:09:50 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/2.0' into 2.0 --- cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/HttpRequestUtil.java | 78 ++++++++++++++++++++++++++++++++++++++- 1 files changed, 76 insertions(+), 2 deletions(-) diff --git a/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/HttpRequestUtil.java b/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/HttpRequestUtil.java index f63c573..5adbfaa 100644 --- a/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/HttpRequestUtil.java +++ b/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/HttpRequestUtil.java @@ -53,7 +53,39 @@ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); - } + } + return result; + }/*** + * get请求(带参数) + * @param url + * @return String + */ + public static String getReq1(String url, Map<String, String> params) { + String result = null; + try { + URIBuilder uriBuilder = new URIBuilder(url); + Iterator maplist = params.entrySet().iterator(); + while (maplist.hasNext()) { + Map.Entry<String, String> map = (Map.Entry<String, String>) maplist.next(); + uriBuilder.addParameter(map.getKey(), map.getValue()); + } + CloseableHttpClient client = HttpClientBuilder.create().build(); + HttpPost get = new HttpPost(uriBuilder.build()); + // todo 验签 + get.setHeader("Authorization", "close"); + get.setHeader("Content-Type", "application/json"); + get.setHeader("Accept", "application/json"); + get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"); + HttpResponse response = client.execute(get); + result = EntityUtils.toString(response.getEntity(), "UTF-8"); + + } catch (URISyntaxException e) { + e.printStackTrace(); + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } return result; } public static String postRequest(String url, Map<String, String> params) { @@ -96,7 +128,49 @@ } return result; } - + public static String postRequest1(String url, Map<String, Object> params) { + // 构造HttpClient的实例 + HttpClient httpClient = new HttpClient(); + // 创建POST方法的实例 + PostMethod postMethod = new PostMethod(url); + // 设置请求头信息 + postMethod.setRequestHeader("Authorization", "close"); + postMethod.setRequestHeader("Accept", "application/json"); + postMethod.addRequestHeader("Content-Type", "application/json"); + postMethod.addRequestHeader("Wechatpay-Serial", "application/json"); + postMethod.setRequestHeader("Connection", "close"); + // 添加参数 + for (Map.Entry<String, Object> entry : params.entrySet()) { + postMethod.addParameter(entry.getKey(), entry.getValue().toString()); + } + // 使用系统提供的默认的恢复策略,设置请求重试处理,用的是默认的重试处理:请求三次 + httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false); + // 接收处理结果 + String result = null; + try { + // 执行Http Post请求 + httpClient.executeMethod(postMethod); + // 返回处理结果 + result = postMethod.getResponseBodyAsString(); + } catch (HttpException e) { + // 发生致命的异常,可能是协议不对或者返回的内容有问题 + System.out.println("请检查输入的URL!"); + e.printStackTrace(); + } catch (IOException e) { + // 发生网络异常 + System.out.println("发生网络异常!"); + e.printStackTrace(); + } finally { + // 释放链接 + postMethod.releaseConnection(); + // 关闭HttpClient实例 + if (httpClient != null) { + ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown(); + httpClient = null; + } + } + return result; + } public static String getRequest(String url, Map<String, String> params) { // 构造HttpClient实例 HttpClient client = new HttpClient(); -- Gitblit v1.7.1