| | |
| | | |
| | | log.info("推送应付账单审核日志到TMS: {}", jsonBody); |
| | | |
| | | String response = HttpUtils.sendPost(url, jsonBody); |
| | | String response = sendJsonPost(url, jsonBody); |
| | | log.info("TMS应付账单审核日志推送响应: {}", response); |
| | | |
| | | return true; |
| | |
| | | |
| | | log.info("推送应收账单审核日志到TMS: {}", jsonBody); |
| | | |
| | | String response = HttpUtils.sendPost(url, jsonBody); |
| | | String response = sendJsonPost(url, jsonBody); |
| | | log.info("TMS应收账单审核日志推送响应: {}", response); |
| | | |
| | | return true; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 发送JSON格式的POST请求 |
| | | * @param url 请求URL |
| | | * @param jsonBody JSON请求体 |
| | | * @return 响应内容 |
| | | */ |
| | | private String sendJsonPost(String url, String jsonBody) { |
| | | java.net.HttpURLConnection connection = null; |
| | | try { |
| | | java.net.URL realUrl = new java.net.URL(url); |
| | | connection = (java.net.HttpURLConnection) realUrl.openConnection(); |
| | | |
| | | // 设置请求属性 |
| | | connection.setRequestMethod("POST"); |
| | | connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); |
| | | connection.setRequestProperty("Accept", "application/json"); |
| | | connection.setRequestProperty("User-Agent", "Mozilla/5.0"); |
| | | |
| | | // 设置连接参数 |
| | | connection.setDoOutput(true); |
| | | connection.setDoInput(true); |
| | | connection.setUseCaches(false); |
| | | |
| | | // 设置超时时间 |
| | | connection.setConnectTimeout(10000); // 10秒连接超时 |
| | | connection.setReadTimeout(30000); // 30秒读取超时 |
| | | |
| | | // 发送请求体 |
| | | try (java.io.OutputStream os = connection.getOutputStream()) { |
| | | os.write(jsonBody.getBytes(java.nio.charset.StandardCharsets.UTF_8)); |
| | | os.flush(); |
| | | } |
| | | |
| | | // 获取响应 |
| | | int responseCode = connection.getResponseCode(); |
| | | if (responseCode == java.net.HttpURLConnection.HTTP_OK) { |
| | | try (java.io.BufferedReader in = new java.io.BufferedReader( |
| | | new java.io.InputStreamReader(connection.getInputStream(), java.nio.charset.StandardCharsets.UTF_8))) { |
| | | StringBuilder response = new StringBuilder(); |
| | | String line; |
| | | while ((line = in.readLine()) != null) { |
| | | response.append(line); |
| | | } |
| | | return response.toString(); |
| | | } |
| | | } else { |
| | | // 处理错误响应 |
| | | try (java.io.BufferedReader in = new java.io.BufferedReader( |
| | | new java.io.InputStreamReader(connection.getErrorStream(), java.nio.charset.StandardCharsets.UTF_8))) { |
| | | StringBuilder errorResponse = new StringBuilder(); |
| | | String line; |
| | | while ((line = in.readLine()) != null) { |
| | | errorResponse.append(line); |
| | | } |
| | | throw new RuntimeException("HTTP请求失败,状态码: " + responseCode + ", 响应: " + errorResponse.toString()); |
| | | } |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error("发送JSON POST请求失败,URL: {}", url, e); |
| | | throw new RuntimeException("发送JSON POST请求失败: " + e.getMessage(), e); |
| | | } finally { |
| | | if (connection != null) { |
| | | connection.disconnect(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断应付账单是否来自TMS系统 |
| | | * @param billNo 账单编号 |
| | | * @return 是否来自TMS系统 |