diff --git a/mailbox-lan/src/main/java/com/biutag/lan/util/SmsLan.java b/mailbox-lan/src/main/java/com/biutag/lan/util/SmsLan.java index e5fd339..820c26f 100644 --- a/mailbox-lan/src/main/java/com/biutag/lan/util/SmsLan.java +++ b/mailbox-lan/src/main/java/com/biutag/lan/util/SmsLan.java @@ -5,10 +5,7 @@ import lombok.extern.slf4j.Slf4j; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; +import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; @@ -54,7 +51,6 @@ public class SmsLan { String jsonStr = JSON.toJSONString(dataMap); log.info("jsonStr: {}", jsonStr); String key = encrypt(jsonStr, PASSWORD); - log.info("key: {}", key); key = URLEncoder.encode(key, "UTF-8"); log.info("key: {}", key); String response = httpUtilPost(BASE_URL, "userid=" + USERNAME + "&key=" + key); @@ -88,7 +84,6 @@ public class SmsLan { String jsonStr = JSON.toJSONString(dataMap); log.info("jsonStr: {}", jsonStr); String key = encrypt(jsonStr, PASSWORD); - log.info("key: {}", key); key = URLEncoder.encode(key, "UTF-8"); log.info("key: {}", key); String response = httpUtilPost(BASE_URL, "userid=" + USERNAME + "&key=" + key); @@ -120,7 +115,6 @@ public class SmsLan { String jsonStr = JSON.toJSONString(dataMap); log.info("jsonStr: {}", jsonStr); String key = encrypt(jsonStr, PASSWORD); - log.info("key: {}", key); key = URLEncoder.encode(key, "UTF-8"); log.info("key: {}", key); String response = httpUtilPost(BASE_URL, "userid=" + USERNAME + "&key=" + key); @@ -133,42 +127,55 @@ public class SmsLan { /** * POST 请求 - * * @param urls - * @param params + * @param par * @return */ - public static String httpUtilPost(String urls, String params) { + public static String httpUtilPost(String urls, String par) { URL url; HttpURLConnection uConn = null; - String result = ""; + String sResult = ""; + BufferedReader in = null; + PrintWriter out = null; + String line = ""; try { + sResult = ""; url = new URL(urls); uConn = (HttpURLConnection) url.openConnection(); uConn.setRequestMethod("POST"); + System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(10000)); + System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(10000)); uConn.setDoInput(true); uConn.setDoOutput(true); uConn.connect(); - try (PrintWriter out = new PrintWriter(new OutputStreamWriter(uConn.getOutputStream(), "utf-8")); - BufferedReader in = new BufferedReader(new InputStreamReader(uConn.getInputStream(), "utf-8"))) { - out.write(params); - out.flush(); - StringBuilder resultBuilder = new StringBuilder(); - String line; - while ((line = in.readLine()) != null) { - if (line.length() > 0) - resultBuilder.append(line); - } - result = resultBuilder.toString(); + out = new PrintWriter(new OutputStreamWriter(uConn.getOutputStream(), "utf-8")); + out.write(par); + out.flush(); + out.close(); + in = new BufferedReader(new InputStreamReader(uConn.getInputStream(), "utf-8")); + while ((line = in.readLine()) != null) { + if (line.length() > 0) + sResult += line; } + in.close(); } catch (Exception e) { e.printStackTrace(); } finally { - if (uConn != null) { - uConn.disconnect(); + try { + if (null != in) + in.close(); + in = null; + if (null != out) + out.close(); + out = null; + if (null != uConn) + uConn.disconnect(); + uConn = null; + } catch (IOException e) { + e.printStackTrace(); } } - return result; + return sResult; } diff --git a/mailbox-lan/src/test/java/com/biutag/lan/SmsTest.java b/mailbox-lan/src/test/java/com/biutag/lan/SmsTest.java index dec0f31..61c9623 100644 --- a/mailbox-lan/src/test/java/com/biutag/lan/SmsTest.java +++ b/mailbox-lan/src/test/java/com/biutag/lan/SmsTest.java @@ -12,9 +12,6 @@ import java.util.Base64; public class SmsTest { - private static final String SENDURL = "AES/ECB/PKCS5PADDING"; - private static final String USERID = "UTF8"; - // AES密钥(长度应为16个字节) private static final String PWD = "fzzd12345678dxpt";