Parcourir la source

3.0.1-同步云阅卷bug修改

xiaof il y a 3 ans
Parent
commit
a5b659da17

+ 31 - 17
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/HttpKit.java

@@ -25,30 +25,31 @@ public class HttpKit {
     @SuppressWarnings("unused")
     public static String sendPost(String url, Map<String, String> params, Map<String, String> requestHeader) {
         OutputStreamWriter out = null;
-        BufferedReader in = null;
+        BufferedReader reader = null;
+        int responseCode = 200;
         StringBuilder result = new StringBuilder();
         try {
             URL realUrl = new URL(url);
-            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
+            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
             // 发送POST请求必须设置如下两行
-            conn.setDoOutput(true);
-            conn.setDoInput(true);
+            connection.setDoOutput(true);
+            connection.setDoInput(true);
             // 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) {
                 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.connect();
+            connection.connect();
             // 获取URLConnection对象对应的输出流
-            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
+            out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
             // 发送请求参数
             if (params != null) {
                 StringBuilder param = new StringBuilder();
@@ -65,13 +66,26 @@ public class HttpKit {
             // flush输出流的缓冲
             out.flush();
             // 定义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;
-            while ((line = in.readLine()) != null) {
+            while ((line = reader.readLine()) != null) {
                 result.append(line);
             }
         } catch (Exception e) {
-            log.error(SystemConstant.LOG_ERROR, e);
+            throw ExceptionResultEnum.ERROR.exception("调用接口异常:错误码[" + responseCode + "],错误原因[" + e.getMessage() + "]");
         }
         //使用finally块来关闭输出流、输入流
         finally {
@@ -79,8 +93,8 @@ public class HttpKit {
                 if (out != null) {
                     out.close();
                 }
-                if (in != null) {
-                    in.close();
+                if (reader != null) {
+                    reader.close();
                 }
             } catch (IOException ex) {
                 log.error(SystemConstant.LOG_ERROR, ex);