|
@@ -25,30 +25,31 @@ public class HttpKit {
|
|
@SuppressWarnings("unused")
|
|
@SuppressWarnings("unused")
|
|
public static String sendPost(String url, Map<String, String> params, Map<String, String> requestHeader) {
|
|
public static String sendPost(String url, Map<String, String> params, Map<String, String> requestHeader) {
|
|
OutputStreamWriter out = null;
|
|
OutputStreamWriter out = null;
|
|
- BufferedReader in = null;
|
|
|
|
|
|
+ BufferedReader reader = null;
|
|
|
|
+ int responseCode = 200;
|
|
StringBuilder result = new StringBuilder();
|
|
StringBuilder result = new StringBuilder();
|
|
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");
|
|
|
|
|
|
+ connection.setRequestMethod("POST");
|
|
// 设置通用的请求属性
|
|
// 设置通用的请求属性
|
|
- conn.setRequestProperty("accept", "*/*");
|
|
|
|
- conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
|
- 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");
|
|
|
|
|
|
+ connection.setRequestProperty("accept", "*/*");
|
|
|
|
+ connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
|
+ connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
|
+ connection.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()) {
|
|
- conn.setRequestProperty(entry.getKey(), entry.getValue());
|
|
|
|
|
|
+ connection.setRequestProperty(entry.getKey(), entry.getValue());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// conn.setRequestProperty("Authorization", authorization);
|
|
// conn.setRequestProperty("Authorization", authorization);
|
|
- conn.connect();
|
|
|
|
|
|
+ connection.connect();
|
|
// 获取URLConnection对象对应的输出流
|
|
// 获取URLConnection对象对应的输出流
|
|
- out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
|
|
|
|
|
|
+ out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
|
|
// 发送请求参数
|
|
// 发送请求参数
|
|
if (params != null) {
|
|
if (params != null) {
|
|
StringBuilder param = new StringBuilder();
|
|
StringBuilder param = new StringBuilder();
|
|
@@ -65,13 +66,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 {
|
|
@@ -79,8 +93,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);
|