|
@@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
-import java.util.Date;
|
|
|
|
|
|
+import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
public class HttpKit {
|
|
public class HttpKit {
|
|
@@ -172,10 +172,10 @@ public class HttpKit {
|
|
public static String sendPost(String requestUrl, Map<String, String> requestHeader, Map<String, Object> formTexts, Map<String, String> files, String requestEncoding, String responseEncoding) {
|
|
public static String sendPost(String requestUrl, Map<String, String> requestHeader, Map<String, Object> formTexts, Map<String, String> files, String requestEncoding, String responseEncoding) {
|
|
OutputStream out = null;
|
|
OutputStream out = null;
|
|
BufferedReader reader = null;
|
|
BufferedReader reader = null;
|
|
- String result = "";
|
|
|
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
try {
|
|
try {
|
|
if (requestUrl == null || requestUrl.isEmpty()) {
|
|
if (requestUrl == null || requestUrl.isEmpty()) {
|
|
- return result;
|
|
|
|
|
|
+ return result.toString();
|
|
}
|
|
}
|
|
URL realUrl = new URL(requestUrl);
|
|
URL realUrl = new URL(requestUrl);
|
|
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
|
|
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
|
|
@@ -245,14 +245,29 @@ public class HttpKit {
|
|
}
|
|
}
|
|
out.flush();
|
|
out.flush();
|
|
out.close();
|
|
out.close();
|
|
-// out = null;
|
|
|
|
- reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), responseEncoding));
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
|
+ if (responseCode >= 400) {
|
|
|
|
+// reader = new BufferedReader(new InputStreamReader(connection.getErrorStream(), responseEncoding));
|
|
|
|
+ Map<String, List<String>> responseProperties = connection.getHeaderFields();
|
|
|
|
+ if (responseProperties.containsKey("error-info")) {
|
|
|
|
+ List<String> strings = responseProperties.get("error-info");
|
|
|
|
+ if (!strings.isEmpty()) {
|
|
|
|
+ String message = String.join(";", strings);
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(message);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("接口调用错误");
|
|
|
|
+ } else {
|
|
|
|
+ reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), responseEncoding));
|
|
|
|
+ }
|
|
String line;
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
while ((line = reader.readLine()) != null) {
|
|
- result += line;
|
|
|
|
|
|
+ result.append(line);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- throw ExceptionResultEnum.ERROR.exception("发送POST请求出现异常:" + e.getMessage());
|
|
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("调用异常:" + e.getMessage());
|
|
} finally {
|
|
} finally {
|
|
try {
|
|
try {
|
|
if (out != null) {
|
|
if (out != null) {
|
|
@@ -265,7 +280,7 @@ public class HttpKit {
|
|
log.error(SystemConstant.LOG_ERROR, ex);
|
|
log.error(SystemConstant.LOG_ERROR, ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return result;
|
|
|
|
|
|
+ return result.toString();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|