From 3ade0af0acaa3e750f00d7a503b9d3aee37de14c Mon Sep 17 00:00:00 2001
From: lidongdong <1459917685@qq.com>
Date: 星期三, 12 六月 2024 15:40:00 +0800
Subject: [PATCH] 花城 党员详情接口 新增单位管理员电话
---
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 179 insertions(+), 1 deletions(-)
diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java
index 68c3341..0472925 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java
@@ -1,10 +1,16 @@
package com.panzhihua.common.utlis;
import java.io.*;
+import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
+import java.security.KeyManagementException;
import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -14,11 +20,14 @@
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
+import org.apache.http.client.config.RequestConfig;
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.methods.HttpUriRequest;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
@@ -33,6 +42,8 @@
import lombok.extern.slf4j.Slf4j;
import javax.net.ssl.SSLContext;
+
+import static com.panzhihua.common.utlis.wx.WXPayConstants.USER_AGENT;
/**
* @program: springcloud_k8s_panzhihuazhihuishequ
@@ -190,7 +201,7 @@
HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() != 200) {
String errorLog = "请求失败,errorCode:" + httpResponse.getStatusLine().getStatusCode();
- log.info(errorLog);
+ log.info("errorLog:{}"+errorLog);
throw new Exception(url + errorLog);
}
// 读取返回信息
@@ -215,6 +226,8 @@
bufferedReader.close();
}
}
+
+
/**
* http请求工具类,post请求
@@ -264,6 +277,103 @@
}
}
+
+
+ // HTTP GET请求
+ public static String sendGet(String url) throws Exception {
+ URL obj = new URL(url);
+ HttpURLConnection con = (HttpURLConnection) obj.openConnection();
+ //默认值GET
+ con.setRequestMethod("GET");
+ //添加请求头
+ con.setRequestProperty("User-Agent", USER_AGENT);
+ int responseCode = con.getResponseCode();
+ log.info("发送 'GET' 请求到 URL:{}" + url);
+ log.info("Response Code:{}" + responseCode);
+ BufferedReader in = new BufferedReader(
+ new InputStreamReader(con.getInputStream()));
+ String inputLine;
+ StringBuffer response = new StringBuffer();
+ while ((inputLine = in.readLine()) != null) {
+ response.append(inputLine);
+ }
+ in.close();
+ //打印结果
+ log.info("uu洗车返回:{}"+response.toString());
+ return response.toString();
+ }
+
+ /**
+ * http请求工具类,post请求
+ *
+ * @param url
+ * url
+ * @param param
+ * 参数值 仅支持String
+ * @return
+ * @throws Exception
+ */
+ public static String httpPostAndToken(String url, String param,Map headerMap) throws Exception {
+ DefaultHttpClient defaultHttpClient = null;
+ BufferedReader bufferedReader = null;
+ try {
+ defaultHttpClient = new DefaultHttpClient();
+ HttpPost httpPost = new HttpPost(url);
+ httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
+// httpPost.setHeader("Authorization", headerMap.get("Authorization").toString());
+// httpPost.setHeader("Host", headerMap.get("Host").toString());
+
+ for (Object key : headerMap.keySet()) {
+ httpPost.setHeader(key.toString(), headerMap.get(key).toString());
+ }
+ if (StringUtils.isNotBlank(param)) {
+ log.info("参数值:{}", param);
+ HttpEntity httpEntity = new StringEntity(param, "utf-8");
+ httpPost.setEntity(httpEntity);
+// httpPost.setHeader("Content-Length", String.valueOf(httpEntity.getContentLength()));
+ }
+ HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
+ if (httpResponse.getStatusLine().getStatusCode() != 200) {
+ int statusCode = httpResponse.getStatusLine().getStatusCode();
+ String errorLog = "请求失败,errorCode:" + httpResponse.getStatusLine().getStatusCode();
+ log.info(errorLog);
+ throw new Exception(url + errorLog);
+ }
+ // 读取返回信息
+ String output;
+ bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "utf-8"));
+ StringBuilder stringBuilder = new StringBuilder();
+ while ((output = bufferedReader.readLine()) != null) {
+ stringBuilder.append(output);
+ }
+ log.info("调用接口返回的参数:" + stringBuilder.toString());
+ return stringBuilder.toString();
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw e;
+ } finally {
+ if (defaultHttpClient != null)
+ defaultHttpClient.getConnectionManager().shutdown();
+ if (bufferedReader != null)
+ bufferedReader.close();
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+ DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
+ HttpPost httpPost = new HttpPost("https://dptest.d-power.com.cn:14404/v1/face");
+// httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
+ httpPost.setHeader("Authorization", "DpToken P3JHgjLbyljfLrFnS9OZbATRJmacdt4b");
+ httpPost.setHeader("Host", "123.60.2.66");
+// httpPost.setHeader("Content-Length", String.valueOf(entity.getContentLength()));
+
+// httpPost.setHeader("User-Agent", "Apache-HttpClient/4.5.12 (Java/1.8.0_162)[\\r][\\n]");
+ httpPost.setEntity(new StringEntity("{\"positions\":[{\"role\":\"occupant\",\"communityId\":\"64f99b2ed26106d4f0fe93f4\",\"unitId\":\"64f99b2ed26106d4f0fe93f7\"}],\"tel\":\"15696695118\",\"name\":\"四月里3\",\"timeout\":10,\"image\":\"fsdfsdfsdf\"}", "utf-8"));
+// httpPost.setHeader("Content-Length", String.valueOf(entity.getContentLength()));
+ HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
+ System.out.println(httpResponse.getEntity());
+ }
+
/**
* get请求
*
@@ -291,6 +401,74 @@
}
/**
+ * get请求
+ *
+ * @param url
+ * 请求地址(get请求时参数自己组装到url上)
+ * @param headerMap
+ * 请求头
+ * @return 响应文本
+ */
+ public static String getUU(String url, Map<String, String> headerMap, String param) {
+ // 请求地址,以及参数设置
+ HttpPost post = new HttpPost(url);
+
+ SSLContext sslContext = null;
+ try {
+ sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
+ @Override
+ public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
+ return true;
+ }
+ }).build();
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ } catch (KeyManagementException e) {
+ e.printStackTrace();
+ } catch (KeyStoreException e) {
+ e.printStackTrace();
+ }
+
+ CloseableHttpClient client = HttpClients.custom().setSslcontext(sslContext).
+ setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
+
+
+ //请求超时时间设置
+ post.setConfig(RequestConfig.custom()// 连接超时时间
+ .setConnectTimeout(5000)
+ // 请求超时时间
+ .setConnectionRequestTimeout(5000)
+ // Socket读取超时时间
+ .setSocketTimeout(5000)
+ // 是否允许重定向
+ .setRedirectsEnabled(false)
+ .build());
+
+ //发送请求
+ CloseableHttpResponse response = null;
+ try {
+ response = client.execute(post);
+ } catch (IOException e) {
+ e.printStackTrace();
+ log.error("request error. ");
+
+ }
+ HttpEntity entity = response.getEntity();
+ try {
+ if (entity != null) {
+ //按指定编码转换结果实体为String类型
+ return EntityUtils.toString(entity, "UTF-8");
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+
+
+
+ /**
* 获取响应信息(String)
*/
public static String getRespString(HttpUriRequest request) {
--
Gitblit v1.7.1