From e3fd31c762968e58817c47b8fe7c3a68b5ec79be Mon Sep 17 00:00:00 2001 From: egzosn Date: Wed, 20 Dec 2017 19:59:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=B7=A5=E5=85=B7=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=AF=B7=E6=B1=82=E5=AE=9E=E4=BD=93=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=20HttpStringEntity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pay/common/http/ClientHttpRequest.java | 6 +- .../pay/common/http/HttpStringEntity.java | 55 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpStringEntity.java diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java index 5cdec5e..dfcdf1a 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java @@ -154,14 +154,16 @@ public class ClientHttpRequest 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); } diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpStringEntity.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpStringEntity.java new file mode 100644 index 0000000..56fed0e --- /dev/null +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpStringEntity.java @@ -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 request, ContentType contentType) throws UnsupportedCharsetException { + super(getMapToParameters(request), contentType); + } + + public HttpStringEntity(Map request, String charset) throws UnsupportedCharsetException { + super(getMapToParameters(request), charset); + } + + public HttpStringEntity(Map request, Charset charset) { + super(getMapToParameters(request), charset); + } + + public HttpStringEntity(Map 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); + } + + +}