mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
替换回httpclient,将httpclient配置独立出来提供更多可配置项目
This commit is contained in:
@@ -34,13 +34,16 @@ public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, S
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("media_id", materialId);
|
||||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
|
||||
CloseableHttpResponse response = httpclient.execute(httpPost);
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return true;
|
||||
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,14 +35,18 @@ public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMate
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("media_id", materialId);
|
||||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
|
||||
CloseableHttpResponse response = httpclient.execute(httpPost);
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
|
||||
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
|
||||
}
|
||||
}finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,13 +48,16 @@ public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMateri
|
||||
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
|
||||
}
|
||||
|
||||
CloseableHttpResponse response = httpclient.execute(httpPost);
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpMaterialUploadResult.fromJson(responseContent);
|
||||
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpMaterialUploadResult.fromJson(responseContent);
|
||||
}
|
||||
}finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,13 +34,16 @@ public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMat
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("media_id", materialId);
|
||||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
|
||||
CloseableHttpResponse response = httpclient.execute(httpPost);
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
|
||||
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
} else {
|
||||
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
|
||||
}
|
||||
}finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -44,22 +44,25 @@ public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExec
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("media_id", materialId);
|
||||
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
|
||||
CloseableHttpResponse response = httpclient.execute(httpPost);
|
||||
// 下载媒体文件出错
|
||||
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
|
||||
byte[] responseContent = IOUtils.toByteArray(inputStream);
|
||||
String responseContentString = new String(responseContent, "UTF-8");
|
||||
if (responseContentString.length() < 100) {
|
||||
try {
|
||||
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
|
||||
if (wxError.getErrorCode() != 0) {
|
||||
throw new WxErrorException(wxError);
|
||||
try(CloseableHttpResponse response = httpclient.execute(httpPost)){
|
||||
// 下载媒体文件出错
|
||||
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
|
||||
byte[] responseContent = IOUtils.toByteArray(inputStream);
|
||||
String responseContentString = new String(responseContent, "UTF-8");
|
||||
if (responseContentString.length() < 100) {
|
||||
try {
|
||||
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
|
||||
if (wxError.getErrorCode() != 0) {
|
||||
throw new WxErrorException(wxError);
|
||||
}
|
||||
} catch (com.google.gson.JsonSyntaxException ex) {
|
||||
return new ByteArrayInputStream(responseContent);
|
||||
}
|
||||
} catch (com.google.gson.JsonSyntaxException ex) {
|
||||
return new ByteArrayInputStream(responseContent);
|
||||
}
|
||||
return new ByteArrayInputStream(responseContent);
|
||||
}finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
return new ByteArrayInputStream(responseContent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -58,8 +58,9 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi
|
||||
}
|
||||
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
|
||||
|
||||
File localFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
return localFile;
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
}finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user