no message
parent
68a277643a
commit
42547d103c
|
|
@ -1,42 +1,75 @@
|
|||
package com.youchain.utils;
|
||||
|
||||
import org.apache.commons.compress.utils.IOUtils;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
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 static String sendPostReq(String api_url, String request) {
|
||||
InputStream instr = null;
|
||||
String str = null;
|
||||
public static String sendPostReq(String apiUrl, String jsonRequest) {
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
URL url = new URL(api_url);
|
||||
URLConnection urlCon = url.openConnection();
|
||||
urlCon.setConnectTimeout(3000);
|
||||
byte[] xmlData = request.getBytes();
|
||||
urlCon.setDoOutput(true);
|
||||
urlCon.setDoInput(true);
|
||||
urlCon.setUseCaches(false);
|
||||
urlCon.setRequestProperty("content-Type", "application/json");
|
||||
urlCon.setRequestProperty("charset", "UTF-8");
|
||||
urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length));
|
||||
DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());
|
||||
printout.write(xmlData);
|
||||
printout.flush();
|
||||
printout.close();
|
||||
instr = urlCon.getInputStream();
|
||||
byte[] bis = IOUtils.toByteArray(instr);
|
||||
String ResponseString = new String(bis, "UTF-8");
|
||||
if ((ResponseString == null) || ("".equals(ResponseString.trim()))) {
|
||||
System.out.println("返回空");
|
||||
// 创建URL对象并建立连接
|
||||
URL url = new URL(apiUrl);
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
// 配置连接参数
|
||||
connection.setConnectTimeout(5000);
|
||||
connection.setReadTimeout(10000);
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
connection.setUseCaches(false);
|
||||
connection.setRequestMethod("POST");
|
||||
|
||||
// 设置HTTP头
|
||||
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
|
||||
// 写入请求体
|
||||
byte[] requestData = jsonRequest.getBytes(StandardCharsets.UTF_8);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ public class KMReSController {
|
|||
kmReService.missionStateCallback(agvTask, missionStatus, containerCode, currentPosition);
|
||||
return new ResponseEntity<>(ApiResult.success(), HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -62,17 +62,12 @@ public class KMReSController {
|
|||
@AnonymousAccess
|
||||
public ResponseEntity<Object> submitMission(@RequestBody SubmitMission submitMission) {
|
||||
try {
|
||||
kmReService.sendAgvTaskToContainer(UrlApi.submitMission(),JSON.toJSONString(submitMission));
|
||||
kmReService.sendAgvTaskToContainer(UrlApi.submitMission(), JSON.toJSONString(submitMission));
|
||||
return new ResponseEntity<>(ApiResult.success(), HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ public class LesController {
|
|||
lesService.genAgvSchedulingTask(lesRequest);
|
||||
return new ResponseEntity<>(LesResult.success(id), HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(LesResult.fail(id, e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
return new ResponseEntity<>(LesResult.fail(id, e.getMessage()), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue