From f0f58829c5c8dd9b249f594526f01fa957cbdd59 Mon Sep 17 00:00:00 2001 From: yn147 <2270338776@qq.com> Date: 星期三, 12 四月 2023 18:03:21 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 167 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java b/src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java new file mode 100644 index 0000000..dafe729 --- /dev/null +++ b/src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java @@ -0,0 +1,167 @@ +package com.qxueyou.scc.wx.utils; + +import com.alibaba.fastjson.JSONObject; +import net.sf.json.JSONString; +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 java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class HttpClientUtil { + + public static String doGet(String url, Map<String, String> param) { + + // 鍒涘缓Httpclient瀵硅薄 + CloseableHttpClient httpclient = HttpClients.createDefault(); + + String resultString = ""; + CloseableHttpResponse response = null; + try { + // 鍒涘缓uri + URIBuilder builder = new URIBuilder(url); + if (param != null) { + for (String key : param.keySet()) { + builder.addParameter(key, param.get(key)); + } + } + URI uri = builder.build(); + + // 鍒涘缓http GET璇锋眰 + HttpGet httpGet = new HttpGet(uri); + + // 鎵ц璇锋眰 + response = httpclient.execute(httpGet); + // 鍒ゆ柇杩斿洖鐘舵�佹槸鍚︿负200 + if (response.getStatusLine().getStatusCode() == 200) { + resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + httpclient.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return resultString; + } + + public static String doGet(String url) { + return doGet(url, null); + } + + public static String doPost(String url, Map<String, String> param) { + // 鍒涘缓Httpclient瀵硅薄 + CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 鍒涘缓Http Post璇锋眰 + HttpPost httpPost = new HttpPost(url); + // 鍒涘缓鍙傛暟鍒楄〃 + if (param != null) { + List<NameValuePair> paramList = new ArrayList<>(); + for (String key : param.keySet()) { + System.out.println(key); + System.out.println(param.get(key)); + paramList.add(new BasicNameValuePair(key, param.get(key))); + } + // 妯℃嫙琛ㄥ崟 + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); + httpPost.setEntity(entity); + } + // 鎵цhttp璇锋眰 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return resultString; + } + + public static String doPostUrl(String url, JSONObject jsonCode) { + // 鍒涘缓Httpclient瀵硅薄 + CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 鍒涘缓Http Post璇锋眰 + HttpPost httpPost = new HttpPost(url); + + String s = jsonCode.toJSONString(jsonCode); + + StringEntity entity=new StringEntity(s); + + httpPost.setEntity(entity); + // 鎵цhttp璇锋眰 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return resultString; + } + + public static String doPost(String url) { + return doPost(url, null); + } + + public static String doPostJson(String url, String json) { + // 鍒涘缓Httpclient瀵硅薄 + CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpResponse response = null; + String resultString = ""; + try { + // 鍒涘缓Http Post璇锋眰 + HttpPost httpPost = new HttpPost(url); + // 鍒涘缓璇锋眰鍐呭 + StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); + httpPost.setEntity(entity); + // 鎵цhttp璇锋眰 + response = httpClient.execute(httpPost); + resultString = EntityUtils.toString(response.getEntity(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return resultString; + } +} -- Gitblit v1.8.0