Commit f90e6a723e7b11c26f3e76507c0f2ef926025020
1 parent
951bef16f2
Exists in
master
修改 text/pain 过时用法
Showing 1 changed file with 42 additions and 36 deletions
core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java
View file @
f90e6a7
| ... | ... | @@ -18,19 +18,19 @@ |
| 18 | 18 | import org.apache.http.impl.client.CloseableHttpClient; |
| 19 | 19 | import org.apache.http.impl.client.HttpClients; |
| 20 | 20 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
| 21 | -import org.apache.http.protocol.HTTP; | |
| 22 | 21 | import org.apache.http.util.EntityUtils; |
| 23 | 22 | import org.slf4j.Logger; |
| 24 | 23 | import org.slf4j.LoggerFactory; |
| 24 | +import org.springframework.util.MimeTypeUtils; | |
| 25 | 25 | |
| 26 | 26 | import com.lyms.util.CharsetUtils; |
| 27 | 27 | |
| 28 | 28 | public abstract class AbsRemote { |
| 29 | 29 | |
| 30 | 30 | public Logger LOG = LoggerFactory.getLogger(getClass()); |
| 31 | - | |
| 31 | + | |
| 32 | 32 | public static PoolingHttpClientConnectionManager connectionManager; |
| 33 | - | |
| 33 | + | |
| 34 | 34 | /** |
| 35 | 35 | * 连接 |
| 36 | 36 | */ |
| 37 | 37 | |
| 38 | 38 | |
| 39 | 39 | |
| 40 | 40 | |
| 41 | 41 | |
| 42 | 42 | |
| 43 | 43 | |
| 44 | 44 | |
| 45 | 45 | |
| 46 | 46 | |
| 47 | 47 | |
| 48 | 48 | |
| ... | ... | @@ -64,49 +64,52 @@ |
| 64 | 64 | * socket超时时间 |
| 65 | 65 | */ |
| 66 | 66 | public final static int SO_TIMEOUT = 15000; |
| 67 | - | |
| 67 | + | |
| 68 | 68 | static { |
| 69 | - SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(CONNECT_TIMEOUT).setTcpNoDelay(false).setSoReuseAddress(true).setSoKeepAlive(true).build(); | |
| 69 | + SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(CONNECT_TIMEOUT) | |
| 70 | + .setTcpNoDelay(false).setSoReuseAddress(true).setSoKeepAlive(true).build(); | |
| 70 | 71 | |
| 71 | 72 | ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(BUFFER_SIZE).build(); |
| 72 | 73 | |
| 73 | - connectionManager = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory> create() | |
| 74 | + connectionManager = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create() | |
| 74 | 75 | .register("http", PlainConnectionSocketFactory.getSocketFactory()) |
| 75 | - .register("https", SSLConnectionSocketFactory.getSocketFactory()) | |
| 76 | - .build()); | |
| 77 | - | |
| 76 | + .register("https", SSLConnectionSocketFactory.getSocketFactory()).build()); | |
| 77 | + | |
| 78 | 78 | connectionManager.setDefaultSocketConfig(socketConfig); |
| 79 | 79 | connectionManager.setDefaultConnectionConfig(connectionConfig); |
| 80 | 80 | connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS); |
| 81 | 81 | connectionManager.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS); |
| 82 | - httpclient = HttpClients.custom().addInterceptorFirst(new GZIPRequestInterceptor()).addInterceptorFirst(new BrowserRequestInterceptor()) | |
| 83 | - .addInterceptorFirst(new GZIPResponseInterceptor()).setConnectionManager(connectionManager).build(); | |
| 82 | + httpclient = HttpClients.custom().addInterceptorFirst(new GZIPRequestInterceptor()) | |
| 83 | + .addInterceptorFirst(new BrowserRequestInterceptor()).addInterceptorFirst(new GZIPResponseInterceptor()) | |
| 84 | + .setConnectionManager(connectionManager).build(); | |
| 84 | 85 | } |
| 85 | - | |
| 86 | - | |
| 86 | + | |
| 87 | 87 | /** |
| 88 | 88 | * 返回UTF8编码字符串 |
| 89 | + * | |
| 89 | 90 | * @param entity |
| 90 | 91 | * @return |
| 91 | - * @throws IOException | |
| 92 | - * @throws ParseException | |
| 92 | + * @throws IOException | |
| 93 | + * @throws ParseException | |
| 93 | 94 | */ |
| 94 | 95 | public String responseUTF8(HttpEntity entity) throws ParseException, IOException { |
| 95 | - | |
| 96 | - if(entity == null) | |
| 96 | + | |
| 97 | + if (entity == null) | |
| 97 | 98 | return null; |
| 98 | - | |
| 99 | + | |
| 99 | 100 | return EntityUtils.toString(entity, CharsetUtils.UTF_8); |
| 100 | 101 | } |
| 101 | 102 | |
| 102 | 103 | /** |
| 103 | 104 | * post 请求参数构建 |
| 105 | + * | |
| 104 | 106 | * @param params |
| 105 | 107 | * @return |
| 106 | 108 | */ |
| 107 | - public HttpEntity postEntity(Map<String,String> params){ | |
| 109 | + public HttpEntity postEntity(Map<String, String> params) { | |
| 108 | 110 | MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create(); |
| 109 | - ContentType content = ContentType.create(HTTP.PLAIN_TEXT_TYPE, CharsetUtils.UTF_8); | |
| 111 | + | |
| 112 | + ContentType content = ContentType.create(MimeTypeUtils.TEXT_PLAIN_VALUE, CharsetUtils.UTF_8); | |
| 110 | 113 | for (Entry<String, String> entry : params.entrySet()) { |
| 111 | 114 | String key = entry.getKey(); |
| 112 | 115 | String value = entry.getValue(); |
| 113 | 116 | |
| 114 | 117 | |
| 115 | 118 | |
| 116 | 119 | |
| 117 | 120 | |
| 118 | 121 | |
| 119 | 122 | |
| 120 | 123 | |
| ... | ... | @@ -117,37 +120,40 @@ |
| 117 | 120 | |
| 118 | 121 | /** |
| 119 | 122 | * 拼接 get 请求参数 |
| 120 | - * <p>忽略请求地址携带参数的情况,例如 http://www.baidu.com?k1=123 | |
| 123 | + * <p> | |
| 124 | + * 忽略请求地址携带参数的情况,例如 http://www.baidu.com?k1=123 | |
| 125 | + * | |
| 121 | 126 | * @param params |
| 122 | 127 | * @return |
| 123 | 128 | */ |
| 124 | - public String handlerReqParams(String url,Map<String, String> params) { | |
| 125 | - if(MapUtils.isEmpty(params))return url; | |
| 126 | - return url+handler(params); | |
| 129 | + public String handlerReqParams(String url, Map<String, String> params) { | |
| 130 | + if (MapUtils.isEmpty(params)) | |
| 131 | + return url; | |
| 132 | + return url + handler(params); | |
| 127 | 133 | } |
| 128 | - | |
| 134 | + | |
| 129 | 135 | /** |
| 130 | 136 | * 参数拼接 |
| 137 | + * | |
| 131 | 138 | * @param params |
| 132 | 139 | * @return |
| 133 | 140 | */ |
| 134 | - private String handler(Map<String,String> params) { | |
| 141 | + private String handler(Map<String, String> params) { | |
| 135 | 142 | StringBuilder sb = new StringBuilder(); |
| 136 | - if(MapUtils.isNotEmpty(params)) | |
| 137 | - { | |
| 143 | + if (MapUtils.isNotEmpty(params)) { | |
| 138 | 144 | int tag = 0; |
| 139 | - for(Entry<String, String> entry : params.entrySet()) | |
| 140 | - { | |
| 141 | - String key = entry.getKey(),value = entry.getValue(); | |
| 142 | - if(tag==0)sb.append("?"); | |
| 143 | - if(tag>0)sb.append("&"); | |
| 145 | + for (Entry<String, String> entry : params.entrySet()) { | |
| 146 | + String key = entry.getKey(), value = entry.getValue(); | |
| 147 | + if (tag == 0) | |
| 148 | + sb.append("?"); | |
| 149 | + if (tag > 0) | |
| 150 | + sb.append("&"); | |
| 144 | 151 | sb.append(key).append("=").append(value); |
| 145 | - tag ++; | |
| 152 | + tag++; | |
| 146 | 153 | } |
| 147 | 154 | } |
| 148 | 155 | return sb.toString(); |
| 149 | 156 | } |
| 150 | - | |
| 151 | - | |
| 157 | + | |
| 152 | 158 | } |