mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-19 07:31:36 +08:00
http请求默认响应编码设置
This commit is contained in:
@@ -21,6 +21,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.egzosn.pay.common.http.UriVariables.getMapToParameters;
|
||||
@@ -42,6 +43,10 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
|
||||
* http请求方式 get pos
|
||||
*/
|
||||
private MethodType method;
|
||||
/**
|
||||
* 默认使用的响应编码
|
||||
*/
|
||||
private Charset defaultCharset = Consts.UTF_8;
|
||||
/**
|
||||
* 响应类型
|
||||
*/
|
||||
@@ -59,6 +64,30 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
|
||||
public ClientHttpRequest() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据请求地址 请求方法,请求内容对象
|
||||
* @param uri 请求地址
|
||||
* @param method 请求方法
|
||||
* @param request 请求内容
|
||||
* @param defaultCharset 默认使用的响应编码
|
||||
*/
|
||||
public ClientHttpRequest(URI uri, MethodType method, Object request, String defaultCharset) {
|
||||
this(uri, method);
|
||||
setDefaultCharset( Charset.forName(defaultCharset));
|
||||
setParameters(request);
|
||||
}
|
||||
/**
|
||||
* 根据请求地址 请求方法,请求内容对象
|
||||
* @param uri 请求地址
|
||||
* @param method 请求方法
|
||||
* @param request 请求内容
|
||||
* @param defaultCharset 默认使用的响应编码
|
||||
*/
|
||||
public ClientHttpRequest(URI uri, MethodType method, Object request, Charset defaultCharset) {
|
||||
this(uri, method);
|
||||
setDefaultCharset(defaultCharset);
|
||||
setParameters(request);
|
||||
}
|
||||
/**
|
||||
* 根据请求地址 请求方法,请求内容对象
|
||||
* @param uri 请求地址
|
||||
@@ -132,6 +161,17 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
|
||||
return method.name();
|
||||
}
|
||||
|
||||
public Charset getDefaultCharset() {
|
||||
if (null == defaultCharset) {
|
||||
defaultCharset = Consts.UTF_8;
|
||||
}
|
||||
return defaultCharset;
|
||||
}
|
||||
|
||||
public void setDefaultCharset(Charset defaultCharset) {
|
||||
this.defaultCharset = defaultCharset;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置代理
|
||||
* @param httpProxy http代理配置信息
|
||||
@@ -247,12 +287,12 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
|
||||
private T toBean(HttpEntity entity, String[] contentType) throws IOException {
|
||||
//判断内容类型是否为文本类型
|
||||
if (isText(contentType[0])) {
|
||||
String charset = "UTF-8";
|
||||
/* String charset = "UTF-8";
|
||||
if (null != contentType && 2 == charset.length()) {
|
||||
charset = contentType[1].substring(contentType[1].indexOf("=") + 1);
|
||||
}
|
||||
}*/
|
||||
//获取响应的文本内容
|
||||
String result = EntityUtils.toString(entity, charset);
|
||||
String result = EntityUtils.toString(entity, defaultCharset);
|
||||
if (LOG.isDebugEnabled()){
|
||||
LOG.debug("请求响应内容:\r\n" + result);
|
||||
}
|
||||
@@ -271,7 +311,11 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
|
||||
}
|
||||
//xml类型
|
||||
if (isXml(contentType[0], first)) {
|
||||
return XML.toJSONObject(result).toJavaObject(responseType);
|
||||
try {
|
||||
return XML.toJSONObject(result).toJavaObject(responseType);
|
||||
}catch (Exception e){
|
||||
;
|
||||
}
|
||||
}
|
||||
throw new PayErrorException(new PayException("failure", "类型转化异常,contentType:" + entity.getContentType().getValue(), result));
|
||||
}
|
||||
|
||||
@@ -15,19 +15,19 @@ public class HttpConfigStorage {
|
||||
/**
|
||||
* http代理地址
|
||||
*/
|
||||
protected String httpProxyHost;
|
||||
private String httpProxyHost;
|
||||
/**
|
||||
* 代理端口
|
||||
*/
|
||||
protected int httpProxyPort;
|
||||
private int httpProxyPort;
|
||||
/**
|
||||
* 请求授权用户名
|
||||
*/
|
||||
protected String authUsername;
|
||||
private String authUsername;
|
||||
/**
|
||||
* 请求授权密码
|
||||
*/
|
||||
protected String authPassword;
|
||||
private String authPassword;
|
||||
|
||||
/**
|
||||
* @see #keystore 是否为https请求所需的证书(PKCS12)的地址,默认为地址,否则为证书信息串
|
||||
@@ -51,6 +51,10 @@ public class HttpConfigStorage {
|
||||
* 默认的每个路由的最大连接数
|
||||
*/
|
||||
private int defaultMaxPerRoute = 0;
|
||||
/**
|
||||
* 默认使用的响应编码
|
||||
*/
|
||||
private String charset;
|
||||
|
||||
/**
|
||||
* http代理地址
|
||||
@@ -250,4 +254,12 @@ public class HttpConfigStorage {
|
||||
public void setDefaultMaxPerRoute(int defaultMaxPerRoute) {
|
||||
this.defaultMaxPerRoute = defaultMaxPerRoute;
|
||||
}
|
||||
|
||||
public String getCharset() {
|
||||
return charset;
|
||||
}
|
||||
|
||||
public void setCharset(String charset) {
|
||||
this.charset = charset;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +200,7 @@ public class HttpRequestTemplate {
|
||||
|
||||
if (null != configStorage && StringUtils.isNotBlank(configStorage.getHttpProxyHost())) {
|
||||
//http代理地址设置
|
||||
httpProxy = new HttpHost(configStorage.getHttpProxyHost(),configStorage.httpProxyPort);;
|
||||
httpProxy = new HttpHost(configStorage.getHttpProxyHost(),configStorage.getHttpProxyPort());;
|
||||
}
|
||||
|
||||
return this;
|
||||
@@ -329,7 +329,7 @@ public class HttpRequestTemplate {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug(String.format("uri:%s, httpMethod:%s ", uri, method.name()));
|
||||
}
|
||||
ClientHttpRequest<T> httpRequest = new ClientHttpRequest(uri ,method, request);
|
||||
ClientHttpRequest<T> httpRequest = new ClientHttpRequest(uri ,method, request, configStorage.getCharset());
|
||||
//判断是否有代理设置
|
||||
if (null == httpProxy){
|
||||
httpRequest.setProxy(httpProxy);
|
||||
|
||||
Reference in New Issue
Block a user