|
@@ -38,8 +38,7 @@ public class HttpKit {
|
|
// 设置通用的请求属性
|
|
// 设置通用的请求属性
|
|
conn.setRequestProperty("accept", "*/*");
|
|
conn.setRequestProperty("accept", "*/*");
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
- conn.setRequestProperty("user-agent",
|
|
|
|
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
|
|
|
+ conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
if (requestHeader != null && requestHeader.size() > 0) {
|
|
if (requestHeader != null && requestHeader.size() > 0) {
|
|
for (Map.Entry<String, String> entry : requestHeader.entrySet()) {
|
|
for (Map.Entry<String, String> entry : requestHeader.entrySet()) {
|
|
@@ -66,8 +65,7 @@ public class HttpKit {
|
|
// flush输出流的缓冲
|
|
// flush输出流的缓冲
|
|
out.flush();
|
|
out.flush();
|
|
// 定义BufferedReader输入流来读取URL的响应
|
|
// 定义BufferedReader输入流来读取URL的响应
|
|
- in = new BufferedReader(
|
|
|
|
- new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
|
|
|
+ in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
String line;
|
|
String line;
|
|
while ((line = in.readLine()) != null) {
|
|
while ((line = in.readLine()) != null) {
|
|
result.append(line);
|
|
result.append(line);
|
|
@@ -101,31 +99,32 @@ public class HttpKit {
|
|
@SuppressWarnings("unused")
|
|
@SuppressWarnings("unused")
|
|
public static String sendPost(String url, String jsonData, Map<String, String> requestHeader) {
|
|
public static String sendPost(String url, String jsonData, Map<String, String> requestHeader) {
|
|
DataOutputStream out = null;
|
|
DataOutputStream out = null;
|
|
- BufferedReader in = null;
|
|
|
|
|
|
+ BufferedReader reader = null;
|
|
StringBuilder result = new StringBuilder();
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
+ int responseCode = 200;
|
|
try {
|
|
try {
|
|
URL realUrl = new URL(url);
|
|
URL realUrl = new URL(url);
|
|
- HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
|
|
|
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
|
|
// 发送POST请求必须设置如下两行
|
|
// 发送POST请求必须设置如下两行
|
|
- conn.setDoOutput(true);
|
|
|
|
- conn.setDoInput(true);
|
|
|
|
|
|
+ connection.setDoOutput(true);
|
|
|
|
+ connection.setDoInput(true);
|
|
// POST方法
|
|
// POST方法
|
|
- conn.setRequestMethod("POST");
|
|
|
|
- conn.setUseCaches(false);
|
|
|
|
- conn.setInstanceFollowRedirects(true);
|
|
|
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
|
+ connection.setUseCaches(false);
|
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
// 设置通用的请求属性
|
|
// 设置通用的请求属性
|
|
// conn.setRequestProperty("accept", "application/json, text/plain, */*");
|
|
// conn.setRequestProperty("accept", "application/json, text/plain, */*");
|
|
// conn.setRequestProperty("connection", "Keep-Alive");
|
|
// conn.setRequestProperty("connection", "Keep-Alive");
|
|
// conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.9 Safari/537.36");
|
|
// conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.9 Safari/537.36");
|
|
- conn.setRequestProperty("Content-Type", "application/json");
|
|
|
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json");
|
|
if (requestHeader != null && requestHeader.size() > 0) {
|
|
if (requestHeader != null && requestHeader.size() > 0) {
|
|
for (Map.Entry<String, String> entry : requestHeader.entrySet()) {
|
|
for (Map.Entry<String, String> entry : requestHeader.entrySet()) {
|
|
- conn.setRequestProperty(entry.getKey(), entry.getValue());
|
|
|
|
|
|
+ connection.setRequestProperty(entry.getKey(), entry.getValue());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- conn.connect();
|
|
|
|
|
|
+ connection.connect();
|
|
// 获取URLConnection对象对应的输出流
|
|
// 获取URLConnection对象对应的输出流
|
|
- out = new DataOutputStream(conn.getOutputStream());
|
|
|
|
|
|
+ out = new DataOutputStream(connection.getOutputStream());
|
|
// 发送请求参数
|
|
// 发送请求参数
|
|
if (StringUtils.isNotBlank(jsonData)) {
|
|
if (StringUtils.isNotBlank(jsonData)) {
|
|
out.writeChars(jsonData);
|
|
out.writeChars(jsonData);
|
|
@@ -133,14 +132,26 @@ public class HttpKit {
|
|
// flush输出流的缓冲
|
|
// flush输出流的缓冲
|
|
out.flush();
|
|
out.flush();
|
|
// 定义BufferedReader输入流来读取URL的响应
|
|
// 定义BufferedReader输入流来读取URL的响应
|
|
- in = new BufferedReader(
|
|
|
|
- new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
|
|
|
+ responseCode = connection.getResponseCode();
|
|
|
|
+ if (responseCode >= 400) {
|
|
|
|
+ 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(), "UTF-8"));
|
|
|
|
+ }
|
|
String line;
|
|
String line;
|
|
- while ((line = in.readLine()) != null) {
|
|
|
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
result.append(line);
|
|
result.append(line);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- log.error(SystemConstant.LOG_ERROR, e);
|
|
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("调用接口异常:错误码[" + responseCode + "],错误原因[" + e.getMessage() + "]");
|
|
}
|
|
}
|
|
//使用finally块来关闭输出流、输入流
|
|
//使用finally块来关闭输出流、输入流
|
|
finally {
|
|
finally {
|
|
@@ -148,8 +159,8 @@ public class HttpKit {
|
|
if (out != null) {
|
|
if (out != null) {
|
|
out.close();
|
|
out.close();
|
|
}
|
|
}
|
|
- if (in != null) {
|
|
|
|
- in.close();
|
|
|
|
|
|
+ if (reader != null) {
|
|
|
|
+ reader.close();
|
|
}
|
|
}
|
|
} catch (IOException ex) {
|
|
} catch (IOException ex) {
|
|
log.error(SystemConstant.LOG_ERROR, ex);
|
|
log.error(SystemConstant.LOG_ERROR, ex);
|