|
@@ -58,13 +58,25 @@ public class HikHttpClient {
|
|
|
return handleResponse(response);
|
|
return handleResponse(response);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ public byte[] executeRequestByte(HttpRequestBase request, String username,
|
|
|
|
|
+ String password, int timeout) throws IOException {
|
|
|
|
|
+ CloseableHttpClient client = getHttpClient(username, password, timeout);
|
|
|
|
|
+ try (CloseableHttpResponse response = client.execute(request)) {
|
|
|
|
|
+ return handleResponseByte(response);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
public String doGet(String url, String username, String password,
|
|
public String doGet(String url, String username, String password,
|
|
|
int timeout, Map<String, String> headers) throws IOException {
|
|
int timeout, Map<String, String> headers) throws IOException {
|
|
|
HttpGet httpGet = new HttpGet(url);
|
|
HttpGet httpGet = new HttpGet(url);
|
|
|
setHeaders(httpGet, headers);
|
|
setHeaders(httpGet, headers);
|
|
|
return executeRequest(httpGet, username, password, timeout);
|
|
return executeRequest(httpGet, username, password, timeout);
|
|
|
}
|
|
}
|
|
|
|
|
+ public byte[] doGetByte(String url, String username, String password,
|
|
|
|
|
+ int timeout, Map<String, String> headers) throws IOException {
|
|
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
|
|
+ setHeaders(httpGet, headers);
|
|
|
|
|
+ return executeRequestByte(httpGet, username, password, timeout);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
public String doPost(String url, String body, String username, String password,
|
|
public String doPost(String url, String body, String username, String password,
|
|
|
int timeout, Map<String, String> headers) throws IOException {
|
|
int timeout, Map<String, String> headers) throws IOException {
|
|
@@ -110,7 +122,31 @@ public class HikHttpClient {
|
|
|
throw new IOException("HTTP Error: " + statusCode);
|
|
throw new IOException("HTTP Error: " + statusCode);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private byte[] handleResponseByte(HttpResponse response) throws IOException {
|
|
|
|
|
+ // 1. 获取响应状态码和响应体
|
|
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 直接读取二进制数据(核心改动:不再转String)
|
|
|
|
|
+ byte[] responseBytes = entity != null ? EntityUtils.toByteArray(entity) : new byte[0];
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 日志打印优化:二进制数据不直接打印(避免乱码/日志过大),仅打印长度
|
|
|
|
|
+ log.debug("HTTP Status: {}, Response Byte Length: {}", statusCode, responseBytes.length);
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 状态码校验
|
|
|
|
|
+ if (statusCode >= 200 && statusCode < 300) {
|
|
|
|
|
+ return responseBytes; // 直接返回byte[]
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 异常时:若有文本响应(如错误信息),可转String打印;无则打印长度
|
|
|
|
|
+ String errorMsg = entity != null ?
|
|
|
|
|
+ EntityUtils.toString(entity, StandardCharsets.UTF_8) :
|
|
|
|
|
+ "No response body (byte length: " + responseBytes.length + ")";
|
|
|
|
|
+ log.error("HTTP Error: {} - {}", statusCode, errorMsg);
|
|
|
|
|
+ throw new IOException("HTTP Error: " + statusCode + ", Message: " + errorMsg);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@PreDestroy
|
|
@PreDestroy
|
|
|
public void destroy() {
|
|
public void destroy() {
|
|
|
clientCache.values().forEach(client -> {
|
|
clientCache.values().forEach(client -> {
|