From 76b630351b2db4b3dbc5c427f5d8edb5d488872c Mon Sep 17 00:00:00 2001 From: ChenJiaHe <763432473@qq.com> Date: 星期五, 21 五月 2021 10:55:28 +0800 Subject: [PATCH] 月增加时间处理 --- src/main/java/com/hx/util/HttpUtil.java | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 95 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/hx/util/HttpUtil.java b/src/main/java/com/hx/util/HttpUtil.java index f64532c..a043cf0 100644 --- a/src/main/java/com/hx/util/HttpUtil.java +++ b/src/main/java/com/hx/util/HttpUtil.java @@ -3,6 +3,7 @@ import net.sf.json.JSONException; import net.sf.json.JSONObject; import org.apache.commons.io.IOUtils; +import org.bouncycastle.util.encoders.UTF8; import org.springframework.web.multipart.MultipartFile; import javax.activation.MimetypesFileTypeMap; @@ -11,6 +12,8 @@ import java.io.*; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -109,6 +112,98 @@ return result; } + /** + * 甯eader鐨刾ost璇锋眰 + * @param generalUrl + * @param header + * @param params + * @return + * @throws Exception + */ + public static String post(String generalUrl, Map<String, String> header, String params) + throws Exception { + URL url = new URL(generalUrl); + // 鎵撳紑鍜孶RL涔嬮棿鐨勮繛鎺� + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + // 璁剧疆閫氱敤鐨勮姹傚睘鎬� + connection.setRequestProperty("Connection", "Keep-Alive"); + if(header != null) + { + for(String key : header.keySet()) + { + connection.setRequestProperty(key, header.get(key)); + } + } + + connection.setUseCaches(false); + connection.setDoOutput(true); + connection.setDoInput(true); + + // 寰楀埌璇锋眰鐨勮緭鍑烘祦瀵硅薄 + DataOutputStream out = new DataOutputStream(connection.getOutputStream()); + out.write(params.getBytes("UTF-8")); + out.flush(); + out.close(); + + // 寤虹珛瀹為檯鐨勮繛鎺� + connection.connect(); + // 瀹氫箟 BufferedReader杈撳叆娴佹潵璇诲彇URL鐨勫搷搴� + BufferedReader in = null; + in = new BufferedReader( + new InputStreamReader(connection.getInputStream(), "UTF-8")); + String result = ""; + String getLine; + while ((getLine = in.readLine()) != null) { + result += getLine; + } + in.close(); + + return result; + } + + /** + * 甯eader鐨刧et璇锋眰 + * @param generalUrl + * @param header + * @return + * @throws Exception + */ + public static String get(String generalUrl, Map<String, String> header) + throws Exception { + URL url = new URL(generalUrl); + // 鎵撳紑鍜孶RL涔嬮棿鐨勮繛鎺� + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + // 璁剧疆閫氱敤鐨勮姹傚睘鎬� + connection.setRequestProperty("Connection", "Keep-Alive"); + if(header != null) + { + for(String key : header.keySet()) + { + connection.setRequestProperty(key, header.get(key)); + } + } + + connection.setUseCaches(false); + connection.setDoOutput(true); + connection.setDoInput(true); + + // 寤虹珛瀹為檯鐨勮繛鎺� + connection.connect(); + // 瀹氫箟 BufferedReader杈撳叆娴佹潵璇诲彇URL鐨勫搷搴� + BufferedReader in = null; + in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); + String result = ""; + String getLine; + while ((getLine = in.readLine()) != null) { + result += getLine; + } + in.close(); + + return result; + } + /** 璇锋眰http鍗忚 鑾峰彇淇℃伅宸ュ叿 **/ public static JSONObject HttpURLUtil(String url, String data) { HttpURLConnection con = null; -- Gitblit v1.8.0