no message

main
HUOJIN\92525 2025-09-02 17:37:06 +08:00
parent 68a277643a
commit 42547d103c
3 changed files with 68 additions and 40 deletions

View File

@ -1,42 +1,75 @@
package com.youchain.utils; package com.youchain.utils;
import org.apache.commons.compress.utils.IOUtils; import java.io.IOException;
import java.io.DataOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.nio.charset.StandardCharsets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
@Slf4j
public class HttpPostUtil { public class HttpPostUtil {
public static String sendPostReq(String api_url, String request) { public static String sendPostReq(String apiUrl, String jsonRequest) {
InputStream instr = null; HttpURLConnection connection = null;
String str = null;
try { try {
URL url = new URL(api_url); // 创建URL对象并建立连接
URLConnection urlCon = url.openConnection(); URL url = new URL(apiUrl);
urlCon.setConnectTimeout(3000); connection = (HttpURLConnection) url.openConnection();
byte[] xmlData = request.getBytes();
urlCon.setDoOutput(true); // 配置连接参数
urlCon.setDoInput(true); connection.setConnectTimeout(5000);
urlCon.setUseCaches(false); connection.setReadTimeout(10000);
urlCon.setRequestProperty("content-Type", "application/json"); connection.setDoOutput(true);
urlCon.setRequestProperty("charset", "UTF-8"); connection.setDoInput(true);
urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length)); connection.setUseCaches(false);
DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream()); connection.setRequestMethod("POST");
printout.write(xmlData);
printout.flush(); // 设置HTTP头
printout.close(); connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
instr = urlCon.getInputStream(); connection.setRequestProperty("Accept", "application/json");
byte[] bis = IOUtils.toByteArray(instr);
String ResponseString = new String(bis, "UTF-8"); // 写入请求体
if ((ResponseString == null) || ("".equals(ResponseString.trim()))) { byte[] requestData = jsonRequest.getBytes(StandardCharsets.UTF_8);
System.out.println("返回空"); connection.setRequestProperty("Content-Length", String.valueOf(requestData.length));
try (OutputStream os = connection.getOutputStream()) {
os.write(requestData);
}
// 处理响应
int responseCode = connection.getResponseCode();
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
// 错误处理
try (InputStream errorStream = connection.getErrorStream()) {
String errorResponse = "";
if (errorStream != null) {
errorResponse = new String(IOUtils.toByteArray(errorStream), StandardCharsets.UTF_8);
}
log.error("HTTP请求失败: 状态码={}, 错误响应={}", responseCode, errorResponse);
return null;
}
}
// 读取正常响应
try (InputStream inputStream = connection.getInputStream()) {
String response = new String(IOUtils.toByteArray(inputStream), StandardCharsets.UTF_8);
if (response.trim().isEmpty()) {
log.warn("HTTP请求返回空响应");
}
return response;
}
} catch (IOException e) {
log.error("HTTP请求异常: {}", e.getMessage(), e);
return null;
} finally {
// 确保连接断开
if (connection != null) {
connection.disconnect();
} }
str = ResponseString;
} catch (Exception e) {
System.out.println("接口异常!");
// throw new Error(e.getMessage());
} }
return str;
} }
} }

View File

@ -51,7 +51,7 @@ public class KMReSController {
kmReService.missionStateCallback(agvTask, missionStatus, containerCode, currentPosition); kmReService.missionStateCallback(agvTask, missionStatus, containerCode, currentPosition);
return new ResponseEntity<>(ApiResult.success(), HttpStatus.OK); return new ResponseEntity<>(ApiResult.success(), HttpStatus.OK);
} catch (Exception e) { } catch (Exception e) {
return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.BAD_REQUEST); return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.OK);
} }
} }
@ -65,14 +65,9 @@ public class KMReSController {
kmReService.sendAgvTaskToContainer(UrlApi.submitMission(), JSON.toJSONString(submitMission)); kmReService.sendAgvTaskToContainer(UrlApi.submitMission(), JSON.toJSONString(submitMission));
return new ResponseEntity<>(ApiResult.success(), HttpStatus.OK); return new ResponseEntity<>(ApiResult.success(), HttpStatus.OK);
} catch (Exception e) { } catch (Exception e) {
return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.BAD_REQUEST); return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.OK);
} }
} }
} }

View File

@ -79,7 +79,7 @@ public class LesController {
lesService.genAgvSchedulingTask(lesRequest); lesService.genAgvSchedulingTask(lesRequest);
return new ResponseEntity<>(LesResult.success(id), HttpStatus.OK); return new ResponseEntity<>(LesResult.success(id), HttpStatus.OK);
} catch (Exception e) { } catch (Exception e) {
return new ResponseEntity<>(LesResult.fail(id, e.getMessage()), HttpStatus.BAD_REQUEST); return new ResponseEntity<>(LesResult.fail(id, e.getMessage()), HttpStatus.OK);
} }
} }