diff --git a/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java b/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java index 3bf1c28..78f1382 100644 --- a/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java +++ b/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java @@ -18,19 +18,19 @@ import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.util.MimeTypeUtils; import com.lyms.util.CharsetUtils; public abstract class AbsRemote { public Logger LOG = LoggerFactory.getLogger(getClass()); - + public static PoolingHttpClientConnectionManager connectionManager; - + /** * 连接 */ @@ -64,49 +64,52 @@ public abstract class AbsRemote { * socket超时时间 */ public final static int SO_TIMEOUT = 15000; - + static { - SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(CONNECT_TIMEOUT).setTcpNoDelay(false).setSoReuseAddress(true).setSoKeepAlive(true).build(); + SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(CONNECT_TIMEOUT) + .setTcpNoDelay(false).setSoReuseAddress(true).setSoKeepAlive(true).build(); ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(BUFFER_SIZE).build(); - connectionManager = new PoolingHttpClientConnectionManager(RegistryBuilder. create() + connectionManager = new PoolingHttpClientConnectionManager(RegistryBuilder.create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) - .register("https", SSLConnectionSocketFactory.getSocketFactory()) - .build()); - + .register("https", SSLConnectionSocketFactory.getSocketFactory()).build()); + connectionManager.setDefaultSocketConfig(socketConfig); connectionManager.setDefaultConnectionConfig(connectionConfig); connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS); connectionManager.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS); - httpclient = HttpClients.custom().addInterceptorFirst(new GZIPRequestInterceptor()).addInterceptorFirst(new BrowserRequestInterceptor()) - .addInterceptorFirst(new GZIPResponseInterceptor()).setConnectionManager(connectionManager).build(); + httpclient = HttpClients.custom().addInterceptorFirst(new GZIPRequestInterceptor()) + .addInterceptorFirst(new BrowserRequestInterceptor()).addInterceptorFirst(new GZIPResponseInterceptor()) + .setConnectionManager(connectionManager).build(); } - - + /** * 返回UTF8编码字符串 + * * @param entity * @return - * @throws IOException - * @throws ParseException + * @throws IOException + * @throws ParseException */ public String responseUTF8(HttpEntity entity) throws ParseException, IOException { - - if(entity == null) + + if (entity == null) return null; - + return EntityUtils.toString(entity, CharsetUtils.UTF_8); } /** * post 请求参数构建 + * * @param params * @return */ - public HttpEntity postEntity(Map params){ + public HttpEntity postEntity(Map params) { MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create(); - ContentType content = ContentType.create(HTTP.PLAIN_TEXT_TYPE, CharsetUtils.UTF_8); + + ContentType content = ContentType.create(MimeTypeUtils.TEXT_PLAIN_VALUE, CharsetUtils.UTF_8); for (Entry entry : params.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); @@ -117,36 +120,39 @@ public abstract class AbsRemote { /** * 拼接 get 请求参数 - *

忽略请求地址携带参数的情况,例如 http://www.baidu.com?k1=123 + *

+ * 忽略请求地址携带参数的情况,例如 http://www.baidu.com?k1=123 + * * @param params * @return */ - public String handlerReqParams(String url,Map params) { - if(MapUtils.isEmpty(params))return url; - return url+handler(params); + public String handlerReqParams(String url, Map params) { + if (MapUtils.isEmpty(params)) + return url; + return url + handler(params); } - + /** * 参数拼接 + * * @param params * @return */ - private String handler(Map params) { + private String handler(Map params) { StringBuilder sb = new StringBuilder(); - if(MapUtils.isNotEmpty(params)) - { + if (MapUtils.isNotEmpty(params)) { int tag = 0; - for(Entry entry : params.entrySet()) - { - String key = entry.getKey(),value = entry.getValue(); - if(tag==0)sb.append("?"); - if(tag>0)sb.append("&"); + for (Entry entry : params.entrySet()) { + String key = entry.getKey(), value = entry.getValue(); + if (tag == 0) + sb.append("?"); + if (tag > 0) + sb.append("&"); sb.append(key).append("=").append(value); - tag ++; + tag++; } } return sb.toString(); } - - + }