1
0
mirror of synced 2025-12-20 15:48:01 +08:00

🎨 修复CloseableHttpClient相关的误用代码

This commit is contained in:
altusea
2025-05-22 15:03:27 +08:00
committed by GitHub
parent 3e1a38a696
commit 8bacc9425e
41 changed files with 240 additions and 443 deletions

View File

@@ -2,17 +2,17 @@ package me.chanjar.weixin.open.executor;
import lombok.Getter;
import me.chanjar.weixin.common.bean.CommonUploadData;
import me.chanjar.weixin.open.bean.CommonUploadMultiParam;
import me.chanjar.weixin.common.bean.CommonUploadParam;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.open.bean.CommonUploadMultiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
@@ -64,19 +64,15 @@ public class CommonUploadMultiRequestExecutorApacheImpl extends CommonUploadMult
httpPost.setEntity(entity.build());
}
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
if (responseContent == null || responseContent.isEmpty()) {
throw new WxErrorException(String.format("上传失败,服务器响应空 url:%s param:%s", uri, param));
}
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return responseContent;
} finally {
httpPost.releaseConnection();
String responseContent = requestHttp.getRequestHttpClient().execute(httpPost, Utf8ResponseHandler.INSTANCE);
if (StringUtils.isEmpty(responseContent)) {
throw new WxErrorException(String.format("上传失败,服务器响应空 url:%s param:%s", uri, param));
}
WxError error = WxError.fromJson(responseContent, wxType);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return responseContent;
}
/**

View File

@@ -131,11 +131,7 @@ public class GenericUploadRequestExecutor implements RequestExecutor<String, Inp
bodyRequest.setEntity(entity);
bodyRequest.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
try (CloseableHttpResponse response = getRequestHttp().getRequestHttpClient().execute(bodyRequest)) {
return Utf8ResponseHandler.INSTANCE.handleResponse(response);
} finally {
bodyRequest.releaseConnection();
}
return getRequestHttp().getRequestHttpClient().execute(bodyRequest, Utf8ResponseHandler.INSTANCE);
}
}

View File

@@ -61,8 +61,6 @@ public class MaQrCodeApacheHttpRequestExecutor extends MaQrCodeRequestExecutor<C
}
}
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
} finally {
httpGet.releaseConnection();
}
}
}