请求工具增加请求实体类型 HttpStringEntity

This commit is contained in:
egzosn
2017-12-20 19:59:23 +08:00
parent 2e3badaffb
commit e3fd31c762
2 changed files with 59 additions and 2 deletions

View File

@@ -154,14 +154,16 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
if (null == request){
return this;
}
if (request instanceof Map) {
if (request instanceof HttpEntity){
setEntity((HttpEntity)request);
} else if (request instanceof Map) {
StringEntity entity = new StringEntity(getMapToParameters((Map) request), APPLICATION_FORM_URLENCODED_UTF_8);
setEntity(entity);
} else if (request instanceof String) {
StringEntity entity = new StringEntity((String) request, APPLICATION_FORM_URLENCODED_UTF_8);
setEntity(entity);
} else {
StringEntity entity = new StringEntity(JSON.toJSONString(request), APPLICATION_FORM_URLENCODED_UTF_8);
StringEntity entity = new StringEntity(JSON.toJSONString(request), ContentType.APPLICATION_JSON);
setEntity(entity);
}

View File

@@ -0,0 +1,55 @@
package com.egzosn.pay.common.http;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Map;
import static com.egzosn.pay.common.http.UriVariables.getMapToParameters;
/**
* 请求实体
*
* @author egan
* @email egzosn@gmail.com
* @date 2017/12/20
*/
public class HttpStringEntity extends StringEntity {
public HttpStringEntity(Map<String, Object> request, ContentType contentType) throws UnsupportedCharsetException {
super(getMapToParameters(request), contentType);
}
public HttpStringEntity(Map<String, Object> request, String charset) throws UnsupportedCharsetException {
super(getMapToParameters(request), charset);
}
public HttpStringEntity(Map<String, Object> request, Charset charset) {
super(getMapToParameters(request), charset);
}
public HttpStringEntity(Map<String, Object> request) throws UnsupportedEncodingException {
super(getMapToParameters(request));
}
public HttpStringEntity(String string, ContentType contentType) throws UnsupportedCharsetException {
super(string, contentType);
}
public HttpStringEntity(String string, String charset) throws UnsupportedCharsetException {
super(string, charset);
}
public HttpStringEntity(String string, Charset charset) {
super(string, charset);
}
public HttpStringEntity(String string) throws UnsupportedEncodingException {
super(string);
}
}