mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-28 12:37:55 +08:00
使用装饰模式,支持apache-http和jodd-http (#194)
This commit is contained in:
@ -1,8 +1,14 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
@ -12,6 +18,7 @@ import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -21,21 +28,74 @@ import java.util.regex.Pattern;
|
||||
/**
|
||||
* 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File
|
||||
* 视频文件不支持下载
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class MediaDownloadRequestExecutor implements RequestExecutor<File, String> {
|
||||
|
||||
private File tmpDirFile;
|
||||
|
||||
public MediaDownloadRequestExecutor() {
|
||||
}
|
||||
|
||||
public MediaDownloadRequestExecutor(File tmpDirFile) {
|
||||
this.tmpDirFile = tmpDirFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
public File execute(RequestHttp requestHttp, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, queryParam);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, queryParam);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String getFileNameJodd(HttpResponse response) throws WxErrorException {
|
||||
String content = response.header("Content-disposition");
|
||||
if (content == null || content.length() == 0) {
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
||||
Matcher m = p.matcher(content);
|
||||
if (m.matches()) {
|
||||
return m.group(1);
|
||||
}
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
private String getFileNameApache(CloseableHttpResponse response) throws WxErrorException {
|
||||
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
|
||||
if(contentDispositionHeader == null || contentDispositionHeader.length == 0){
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
||||
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
|
||||
if(m.matches()){
|
||||
return m.group(1);
|
||||
}
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* apache-http实现方式
|
||||
* @param httpclient
|
||||
* @param httpProxy
|
||||
* @param uri
|
||||
* @param queryParam
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
private File executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
@ -50,8 +110,8 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = httpclient.execute(httpGet);
|
||||
InputStream inputStream = InputStreamResponseHandler.INSTANCE
|
||||
.handleResponse(response)) {
|
||||
InputStream inputStream = InputStreamResponseHandler.INSTANCE
|
||||
.handleResponse(response)) {
|
||||
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
|
||||
@ -62,7 +122,7 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin
|
||||
}
|
||||
}
|
||||
|
||||
String fileName = getFileName(response);
|
||||
String fileName = getFileNameApache(response);
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
return null;
|
||||
}
|
||||
@ -76,18 +136,46 @@ public class MediaDownloadRequestExecutor implements RequestExecutor<File, Strin
|
||||
|
||||
}
|
||||
|
||||
private String getFileName(CloseableHttpResponse response) throws WxErrorException {
|
||||
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
|
||||
if(contentDispositionHeader == null || contentDispositionHeader.length == 0){
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
|
||||
/**
|
||||
* jodd-http实现方式
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param queryParam
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
private File executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
Pattern p = Pattern.compile(".*filename=\"(.*)\"");
|
||||
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
|
||||
if(m.matches()){
|
||||
return m.group(1);
|
||||
HttpRequest request = HttpRequest.get(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
}
|
||||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
|
||||
request.withConnectionProvider(provider);
|
||||
HttpResponse response = request.send();
|
||||
String contentType = response.header("Content-Type");
|
||||
if (contentType != null && contentType.startsWith("application/json")) {
|
||||
// application/json; encoding=utf-8 下载媒体文件出错
|
||||
throw new WxErrorException(WxError.fromJson(response.bodyText()));
|
||||
}
|
||||
|
||||
String fileName = getFileNameJodd(response);
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());
|
||||
String[] nameAndExt = fileName.split("\\.");
|
||||
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], this.tmpDirFile);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
@ -24,7 +29,35 @@ import java.io.IOException;
|
||||
public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUploadResult, File> {
|
||||
|
||||
@Override
|
||||
public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, IOException {
|
||||
public WxMediaUploadResult execute(RequestHttp requestHttp, String uri, File file) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, file);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, file);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* apache-http实现方式
|
||||
* @param httpclient
|
||||
* @param httpProxy
|
||||
* @param uri
|
||||
* @param file
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
private WxMediaUploadResult executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
@ -32,10 +65,10 @@ public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUpload
|
||||
}
|
||||
if (file != null) {
|
||||
HttpEntity entity = MultipartEntityBuilder
|
||||
.create()
|
||||
.addBinaryBody("media", file)
|
||||
.setMode(HttpMultipartMode.RFC6532)
|
||||
.build();
|
||||
.create()
|
||||
.addBinaryBody("media", file)
|
||||
.setMode(HttpMultipartMode.RFC6532)
|
||||
.build();
|
||||
httpPost.setEntity(entity);
|
||||
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
|
||||
}
|
||||
@ -51,4 +84,32 @@ public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUpload
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* jodd-http实现方式
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param file
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
private WxMediaUploadResult executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, File file) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
}
|
||||
request.withConnectionProvider(provider);
|
||||
request.form("media", file);
|
||||
HttpResponse response = request.send();
|
||||
String responseContent = response.bodyText();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return WxMediaUploadResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@ -15,13 +13,11 @@ import java.io.IOException;
|
||||
public interface RequestExecutor<T, E> {
|
||||
|
||||
/**
|
||||
* @param httpclient 传入的httpClient
|
||||
* @param httpProxy http代理对象,如果没有配置代理则为空
|
||||
* @param uri uri
|
||||
* @param data 数据
|
||||
* @param uri uri
|
||||
* @param data 数据
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
T execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws WxErrorException, IOException;
|
||||
T execute(RequestHttp requestHttp, String uri, E data) throws WxErrorException, IOException;
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,20 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
/**
|
||||
* Created by ecoolper on 2017/4/22.
|
||||
*/
|
||||
public interface RequestHttp {
|
||||
|
||||
/**
|
||||
* 返回httpClient
|
||||
* @return
|
||||
*/
|
||||
Object getRequestHttpClient();
|
||||
|
||||
/**
|
||||
* 返回httpProxy
|
||||
* @return
|
||||
*/
|
||||
Object getRequestHttpProxy();
|
||||
|
||||
}
|
||||
@ -1,7 +1,12 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@ -18,7 +23,34 @@ import java.io.IOException;
|
||||
public class SimpleGetRequestExecutor implements RequestExecutor<String, String> {
|
||||
|
||||
@Override
|
||||
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
public String execute(RequestHttp requestHttp, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, queryParam);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, queryParam);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* apache-http实现方式
|
||||
* @param httpclient
|
||||
* @param httpProxy
|
||||
* @param uri
|
||||
* @param queryParam
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
private String executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
@ -43,4 +75,37 @@ public class SimpleGetRequestExecutor implements RequestExecutor<String, String>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* jodd-http实现方式
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param queryParam
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
private String executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String queryParam) throws WxErrorException, IOException {
|
||||
if (queryParam != null) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
|
||||
}
|
||||
|
||||
HttpRequest request = HttpRequest.get(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
}
|
||||
request.withConnectionProvider(provider);
|
||||
HttpResponse response = request.send();
|
||||
String responseContent = response.bodyText();
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,7 +1,12 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
@ -10,10 +15,10 @@ import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 用装饰模式实现
|
||||
* 简单的POST请求执行器,请求的参数是String, 返回的结果也是String
|
||||
*
|
||||
* @author Daniel Qian
|
||||
@ -21,7 +26,33 @@ import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
public class SimplePostRequestExecutor implements RequestExecutor<String, String> {
|
||||
|
||||
@Override
|
||||
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, IOException {
|
||||
public String execute(RequestHttp requestHttp, String uri, String postEntity) throws WxErrorException, IOException {
|
||||
if (requestHttp.getRequestHttpClient() instanceof CloseableHttpClient) {
|
||||
CloseableHttpClient httpClient = (CloseableHttpClient) requestHttp.getRequestHttpClient();
|
||||
HttpHost httpProxy = (HttpHost) requestHttp.getRequestHttpProxy();
|
||||
return executeApache(httpClient, httpProxy, uri, postEntity);
|
||||
}
|
||||
if (requestHttp.getRequestHttpClient() instanceof HttpConnectionProvider) {
|
||||
HttpConnectionProvider provider = (HttpConnectionProvider) requestHttp.getRequestHttpClient();
|
||||
ProxyInfo proxyInfo = (ProxyInfo) requestHttp.getRequestHttpProxy();
|
||||
return executeJodd(provider, proxyInfo, uri, postEntity);
|
||||
} else {
|
||||
//这里需要抛出异常,需要优化
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* apache-http实现方式
|
||||
* @param httpclient
|
||||
* @param httpProxy
|
||||
* @param uri
|
||||
* @param postEntity
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
private String executeApache(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
|
||||
@ -37,8 +68,8 @@ public class SimplePostRequestExecutor implements RequestExecutor<String, String
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
if (responseContent.isEmpty()) {
|
||||
throw new WxErrorException(
|
||||
WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容")
|
||||
.build());
|
||||
WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容")
|
||||
.build());
|
||||
}
|
||||
|
||||
if (responseContent.startsWith("<xml>")) {
|
||||
@ -56,4 +87,46 @@ public class SimplePostRequestExecutor implements RequestExecutor<String, String
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* jodd-http实现方式
|
||||
* @param provider
|
||||
* @param proxyInfo
|
||||
* @param uri
|
||||
* @param postEntity
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @throws IOException
|
||||
*/
|
||||
private String executeJodd(HttpConnectionProvider provider, ProxyInfo proxyInfo, String uri, String postEntity) throws WxErrorException, IOException {
|
||||
HttpRequest request = HttpRequest.post(uri);
|
||||
if (proxyInfo != null) {
|
||||
provider.useProxy(proxyInfo);
|
||||
}
|
||||
request.withConnectionProvider(provider);
|
||||
if (postEntity != null) {
|
||||
request.bodyText(postEntity);
|
||||
}
|
||||
HttpResponse response = request.send();
|
||||
|
||||
String responseContent = response.bodyText();
|
||||
if (responseContent.isEmpty()) {
|
||||
throw new WxErrorException(
|
||||
WxError.newBuilder().setErrorCode(9999).setErrorMsg("无响应内容")
|
||||
.build());
|
||||
}
|
||||
|
||||
if (responseContent.startsWith("<xml>")) {
|
||||
//xml格式输出直接返回
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
WxError error = WxError.fromJson(responseContent);
|
||||
if (error.getErrorCode() != 0) {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.annotation.NotThreadSafe;
|
||||
@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
@ -1,4 +1,4 @@
|
||||
package me.chanjar.weixin.common.util.http;
|
||||
package me.chanjar.weixin.common.util.http.apache;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpEntity;
|
||||
Reference in New Issue
Block a user