Commit 9e05f9a044cc635c5cfb5b1fd1edc74bc03e2f24
1 parent
60bdd86233
Exists in
master
添加httpClient方法
Showing 10 changed files with 428 additions and 0 deletions
- core.sdk/pom.xml
- core.sdk/src/main/java/com/lyms/util/HttpUtils.java
- core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java
- core.sdk/src/main/java/com/lyms/util/remote/BrowserRequestInterceptor.java
- core.sdk/src/main/java/com/lyms/util/remote/GZIPRequestInterceptor.java
- core.sdk/src/main/java/com/lyms/util/remote/GZIPResponseInterceptor.java
- core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java
- core.sdk/src/main/java/com/lyms/util/remote/package-info.java
- core.sdk/src/test/java/com/core/sdk/utils/HttpUtilTest.java
- core.sdk/src/test/java/com/core/sdk/utils/package-info.java
core.sdk/pom.xml
View file @
9e05f9a
| ... | ... | @@ -7,5 +7,27 @@ |
| 7 | 7 | </parent> |
| 8 | 8 | <artifactId>core.sdk</artifactId> |
| 9 | 9 | <packaging>jar</packaging> |
| 10 | + | |
| 11 | + | |
| 12 | + <properties> | |
| 13 | + <httpclient.version>4.5.3</httpclient.version> | |
| 14 | + </properties> | |
| 15 | + <dependencies> | |
| 16 | + | |
| 17 | + <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> | |
| 18 | + <dependency> | |
| 19 | + <groupId>org.apache.httpcomponents</groupId> | |
| 20 | + <artifactId>httpclient</artifactId> | |
| 21 | + <version>${httpclient.version}</version> | |
| 22 | + </dependency> | |
| 23 | + | |
| 24 | + <dependency> | |
| 25 | + <groupId>org.apache.httpcomponents</groupId> | |
| 26 | + <artifactId>httpmime</artifactId> | |
| 27 | + <version>${httpclient.version}</version> | |
| 28 | + </dependency> | |
| 29 | + | |
| 30 | + </dependencies> | |
| 31 | + | |
| 10 | 32 | </project> |
core.sdk/src/main/java/com/lyms/util/HttpUtils.java
View file @
9e05f9a
| ... | ... | @@ -31,7 +31,9 @@ |
| 31 | 31 | import org.slf4j.Logger; |
| 32 | 32 | import org.slf4j.LoggerFactory; |
| 33 | 33 | |
| 34 | +import com.lyms.util.remote.HttpRemote; | |
| 34 | 35 | |
| 36 | + | |
| 35 | 37 | /** |
| 36 | 38 | * <p> |
| 37 | 39 | * HTTP工具类 |
| ... | ... | @@ -47,6 +49,12 @@ |
| 47 | 49 | |
| 48 | 50 | |
| 49 | 51 | /** |
| 52 | + * http远程调用 | |
| 53 | + */ | |
| 54 | + public static HttpRemote HTTP = HttpRemote.build(); | |
| 55 | + | |
| 56 | + | |
| 57 | + /** | |
| 50 | 58 | * |
| 51 | 59 | * 允许 JS 跨域设置 |
| 52 | 60 | * |
| ... | ... | @@ -404,5 +412,7 @@ |
| 404 | 412 | } |
| 405 | 413 | } |
| 406 | 414 | } |
| 415 | + | |
| 416 | + | |
| 407 | 417 | } |
core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java
View file @
9e05f9a
| 1 | +package com.lyms.util.remote; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import java.util.Map; | |
| 5 | +import java.util.Map.Entry; | |
| 6 | + | |
| 7 | +import org.apache.commons.collections.MapUtils; | |
| 8 | +import org.apache.http.HttpEntity; | |
| 9 | +import org.apache.http.ParseException; | |
| 10 | +import org.apache.http.config.ConnectionConfig; | |
| 11 | +import org.apache.http.config.RegistryBuilder; | |
| 12 | +import org.apache.http.config.SocketConfig; | |
| 13 | +import org.apache.http.conn.socket.ConnectionSocketFactory; | |
| 14 | +import org.apache.http.conn.socket.PlainConnectionSocketFactory; | |
| 15 | +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |
| 16 | +import org.apache.http.entity.ContentType; | |
| 17 | +import org.apache.http.entity.mime.MultipartEntityBuilder; | |
| 18 | +import org.apache.http.impl.client.CloseableHttpClient; | |
| 19 | +import org.apache.http.impl.client.HttpClients; | |
| 20 | +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | |
| 21 | +import org.apache.http.protocol.HTTP; | |
| 22 | +import org.apache.http.util.EntityUtils; | |
| 23 | +import org.slf4j.Logger; | |
| 24 | +import org.slf4j.LoggerFactory; | |
| 25 | + | |
| 26 | +import com.lyms.util.CharsetUtils; | |
| 27 | + | |
| 28 | +public abstract class AbsRemote { | |
| 29 | + | |
| 30 | + public Logger LOG = LoggerFactory.getLogger(getClass()); | |
| 31 | + | |
| 32 | + public static PoolingHttpClientConnectionManager connectionManager; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * 连接 | |
| 36 | + */ | |
| 37 | + public static CloseableHttpClient httpclient = null; | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * 最大连接数 | |
| 41 | + */ | |
| 42 | + public final static int MAX_TOTAL_CONNECTIONS = 800; | |
| 43 | + /** | |
| 44 | + * 获取连接的最大等待时间 | |
| 45 | + */ | |
| 46 | + public final static int WAIT_TIMEOUT = 60000; | |
| 47 | + /** | |
| 48 | + * 每个路由最大连接数 | |
| 49 | + */ | |
| 50 | + public final static int MAX_ROUTE_CONNECTIONS = 250; | |
| 51 | + /** | |
| 52 | + * 连接超时时间 | |
| 53 | + */ | |
| 54 | + public final static int CONNECT_TIMEOUT = 5000; | |
| 55 | + /** | |
| 56 | + * 读取超时时间 | |
| 57 | + */ | |
| 58 | + public final static int READ_TIMEOUT = 5000; | |
| 59 | + /** | |
| 60 | + * 请求缓存 | |
| 61 | + */ | |
| 62 | + public final static int BUFFER_SIZE = 4096; | |
| 63 | + /** | |
| 64 | + * socket超时时间 | |
| 65 | + */ | |
| 66 | + public final static int SO_TIMEOUT = 3000; | |
| 67 | + | |
| 68 | + static { | |
| 69 | + SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(CONNECT_TIMEOUT).setTcpNoDelay(false).setSoReuseAddress(true).setSoKeepAlive(true).build(); | |
| 70 | + | |
| 71 | + ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(BUFFER_SIZE).build(); | |
| 72 | + | |
| 73 | + connectionManager = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory> create() | |
| 74 | + .register("http", PlainConnectionSocketFactory.getSocketFactory()) | |
| 75 | + .register("https", SSLConnectionSocketFactory.getSocketFactory()) | |
| 76 | + .build()); | |
| 77 | + | |
| 78 | + connectionManager.setDefaultSocketConfig(socketConfig); | |
| 79 | + connectionManager.setDefaultConnectionConfig(connectionConfig); | |
| 80 | + connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS); | |
| 81 | + connectionManager.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS); | |
| 82 | + httpclient = HttpClients.custom().addInterceptorFirst(new GZIPRequestInterceptor()).addInterceptorFirst(new BrowserRequestInterceptor()) | |
| 83 | + .addInterceptorFirst(new GZIPResponseInterceptor()).setConnectionManager(connectionManager).build(); | |
| 84 | + } | |
| 85 | + | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * 返回UTF8编码字符串 | |
| 89 | + * @param entity | |
| 90 | + * @return | |
| 91 | + * @throws IOException | |
| 92 | + * @throws ParseException | |
| 93 | + */ | |
| 94 | + public String responseUTF8(HttpEntity entity) throws ParseException, IOException { | |
| 95 | + | |
| 96 | + if(entity == null) | |
| 97 | + return null; | |
| 98 | + | |
| 99 | + return EntityUtils.toString(entity, CharsetUtils.UTF_8); | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * post 请求参数构建 | |
| 104 | + * @param params | |
| 105 | + * @return | |
| 106 | + */ | |
| 107 | + public HttpEntity postEntity(Map<String,String> params){ | |
| 108 | + MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create(); | |
| 109 | + ContentType content = ContentType.create(HTTP.PLAIN_TEXT_TYPE, CharsetUtils.UTF_8); | |
| 110 | + for (Entry<String, String> entry : params.entrySet()) { | |
| 111 | + String key = entry.getKey(); | |
| 112 | + String value = entry.getValue(); | |
| 113 | + reqEntity.addTextBody(key, value, content); | |
| 114 | + } | |
| 115 | + return reqEntity.build(); | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * 拼接 get 请求参数 | |
| 120 | + * <p>忽略请求地址携带参数的情况,例如 http://www.baidu.com?k1=123 | |
| 121 | + * @param params | |
| 122 | + * @return | |
| 123 | + */ | |
| 124 | + public String handlerReqParams(String url,Map<String, String> params) { | |
| 125 | + if(MapUtils.isEmpty(params))return url; | |
| 126 | + return url+handler(params); | |
| 127 | + } | |
| 128 | + | |
| 129 | + /** | |
| 130 | + * 参数拼接 | |
| 131 | + * @param params | |
| 132 | + * @return | |
| 133 | + */ | |
| 134 | + private String handler(Map<String,String> params) { | |
| 135 | + StringBuilder sb = new StringBuilder(); | |
| 136 | + if(MapUtils.isNotEmpty(params)) | |
| 137 | + { | |
| 138 | + 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("&"); | |
| 144 | + sb.append(key).append("=").append(value); | |
| 145 | + tag ++; | |
| 146 | + } | |
| 147 | + } | |
| 148 | + return sb.toString(); | |
| 149 | + } | |
| 150 | + | |
| 151 | + | |
| 152 | +} |
core.sdk/src/main/java/com/lyms/util/remote/BrowserRequestInterceptor.java
View file @
9e05f9a
| 1 | +package com.lyms.util.remote; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | + | |
| 5 | +import org.apache.http.HttpException; | |
| 6 | +import org.apache.http.HttpRequest; | |
| 7 | +import org.apache.http.HttpRequestInterceptor; | |
| 8 | +import org.apache.http.protocol.HttpContext; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * | |
| 12 | + * 封装header | |
| 13 | + * | |
| 14 | + */ | |
| 15 | +public class BrowserRequestInterceptor implements HttpRequestInterceptor { | |
| 16 | + @Override | |
| 17 | + public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { | |
| 18 | +// if (!request.containsHeader("Accept")) { | |
| 19 | +// request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); | |
| 20 | +// request.addHeader("Accept-Language", "zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3"); | |
| 21 | +// request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0"); | |
| 22 | + request.addHeader("User-Agent","Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"); | |
| 23 | +// } | |
| 24 | + } | |
| 25 | + | |
| 26 | +} |
core.sdk/src/main/java/com/lyms/util/remote/GZIPRequestInterceptor.java
View file @
9e05f9a
| 1 | +package com.lyms.util.remote; | |
| 2 | + | |
| 3 | + | |
| 4 | +import java.io.IOException; | |
| 5 | + | |
| 6 | +import org.apache.http.HttpException; | |
| 7 | +import org.apache.http.HttpRequest; | |
| 8 | +import org.apache.http.HttpRequestInterceptor; | |
| 9 | +import org.apache.http.protocol.HttpContext; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * header压缩 | |
| 13 | + */ | |
| 14 | +public class GZIPRequestInterceptor implements HttpRequestInterceptor { | |
| 15 | + | |
| 16 | + @Override | |
| 17 | + public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { | |
| 18 | + if (!request.containsHeader("Accept-Encoding")) { | |
| 19 | + request.addHeader("Accept-Encoding", "gzip"); | |
| 20 | + } | |
| 21 | + } | |
| 22 | + | |
| 23 | +} |
core.sdk/src/main/java/com/lyms/util/remote/GZIPResponseInterceptor.java
View file @
9e05f9a
| 1 | +package com.lyms.util.remote; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | + | |
| 5 | +import org.apache.http.Header; | |
| 6 | +import org.apache.http.HeaderElement; | |
| 7 | +import org.apache.http.HttpEntity; | |
| 8 | +import org.apache.http.HttpException; | |
| 9 | +import org.apache.http.HttpResponse; | |
| 10 | +import org.apache.http.HttpResponseInterceptor; | |
| 11 | +import org.apache.http.client.entity.GzipDecompressingEntity; | |
| 12 | +import org.apache.http.protocol.HttpContext; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * response解压 | |
| 16 | + */ | |
| 17 | +public class GZIPResponseInterceptor implements HttpResponseInterceptor { | |
| 18 | + | |
| 19 | + public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { | |
| 20 | + | |
| 21 | + HttpEntity entity = response.getEntity(); | |
| 22 | + if (entity != null) { | |
| 23 | + Header ceheader = entity.getContentEncoding(); | |
| 24 | + if (ceheader != null) { | |
| 25 | + HeaderElement[] codecs = ceheader.getElements(); | |
| 26 | + for (int i = 0; i < codecs.length; i++) { | |
| 27 | + if (codecs[i].getName().equalsIgnoreCase("gzip")) { | |
| 28 | + response.setEntity(new GzipDecompressingEntity(response.getEntity())); | |
| 29 | + return; | |
| 30 | + } | |
| 31 | + } | |
| 32 | + } | |
| 33 | + } | |
| 34 | + } | |
| 35 | + | |
| 36 | +} |
core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java
View file @
9e05f9a
| 1 | +package com.lyms.util.remote; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import java.util.Map; | |
| 5 | + | |
| 6 | +import org.apache.http.HttpEntity; | |
| 7 | +import org.apache.http.client.ClientProtocolException; | |
| 8 | +import org.apache.http.client.methods.CloseableHttpResponse; | |
| 9 | +import org.apache.http.client.methods.HttpGet; | |
| 10 | +import org.apache.http.client.methods.HttpPost; | |
| 11 | +import org.apache.http.client.protocol.HttpClientContext; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * http,https 远程调用 | |
| 15 | + * @author maliang | |
| 16 | + * | |
| 17 | + */ | |
| 18 | +public class HttpRemote extends AbsRemote{ | |
| 19 | + | |
| 20 | + private HttpRemote(){} | |
| 21 | + | |
| 22 | + public static HttpRemote build(){ | |
| 23 | + return new HttpRemote(); | |
| 24 | + } | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * get 请求 | |
| 28 | + * <p>该方法参数只接受String类型参数 | |
| 29 | + * @param url | |
| 30 | + * @param params | |
| 31 | + * @return | |
| 32 | + */ | |
| 33 | + public String get(String url,Map<String,String> params) { | |
| 34 | + HttpClientContext httpContext = HttpClientContext.create(); | |
| 35 | + //处理参数,模拟http get 请求连接方式 | |
| 36 | + url = super.handlerReqParams(url,params); | |
| 37 | + LOG.debug("remote : {}",url); | |
| 38 | + HttpGet get = new HttpGet(url); | |
| 39 | + HttpEntity entity = null; | |
| 40 | + CloseableHttpResponse response = null; | |
| 41 | + try { | |
| 42 | + response = httpclient.execute(get, httpContext); | |
| 43 | + entity = response.getEntity(); | |
| 44 | + httpContext.getCookieStore().clear(); | |
| 45 | + return responseUTF8(entity); | |
| 46 | + } catch (ClientProtocolException e) { | |
| 47 | + e.printStackTrace(); | |
| 48 | + } catch (IOException e) { | |
| 49 | + e.printStackTrace(); | |
| 50 | + } finally { | |
| 51 | + try { | |
| 52 | + if (response != null) { | |
| 53 | + response.close(); | |
| 54 | + } | |
| 55 | + } catch (IOException e) { | |
| 56 | + e.printStackTrace(); | |
| 57 | + } | |
| 58 | + } | |
| 59 | + return null; | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * post 请求 | |
| 64 | + * <p>目前参数只接受String类型参数 | |
| 65 | + * @param url | |
| 66 | + * @param params | |
| 67 | + * @return | |
| 68 | + */ | |
| 69 | + public String post(String url,Map<String,String> params) { | |
| 70 | + HttpClientContext httpContext = HttpClientContext.create(); | |
| 71 | + HttpPost post = new HttpPost(url); | |
| 72 | + HttpEntity entity = null; | |
| 73 | + CloseableHttpResponse response = null; | |
| 74 | + try { | |
| 75 | + | |
| 76 | + entity = super.postEntity(params); | |
| 77 | + if(entity!=null) { | |
| 78 | + post.setEntity(entity); | |
| 79 | + } | |
| 80 | + | |
| 81 | + response = httpclient.execute(post, httpContext); | |
| 82 | + entity = response.getEntity(); | |
| 83 | + httpContext.getCookieStore().clear(); | |
| 84 | + return responseUTF8(entity); | |
| 85 | + } catch (ClientProtocolException e) { | |
| 86 | + e.printStackTrace(); | |
| 87 | + } catch (IOException e) { | |
| 88 | + e.printStackTrace(); | |
| 89 | + } finally { | |
| 90 | + try { | |
| 91 | + if (response != null) { | |
| 92 | + response.close(); | |
| 93 | + } | |
| 94 | + } catch (IOException e) { | |
| 95 | + e.printStackTrace(); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + return null; | |
| 99 | + } | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | +} |
core.sdk/src/main/java/com/lyms/util/remote/package-info.java
View file @
9e05f9a
core.sdk/src/test/java/com/core/sdk/utils/HttpUtilTest.java
View file @
9e05f9a
| 1 | +package com.core.sdk.utils; | |
| 2 | + | |
| 3 | +import java.util.HashMap; | |
| 4 | +import java.util.Map; | |
| 5 | + | |
| 6 | +import org.junit.Before; | |
| 7 | +import org.junit.Test; | |
| 8 | + | |
| 9 | +import com.lyms.util.HttpUtils; | |
| 10 | + | |
| 11 | +public class HttpUtilTest { | |
| 12 | + | |
| 13 | + private Map<String,String> params = new HashMap<String,String>(); | |
| 14 | + | |
| 15 | + @Before | |
| 16 | + public void init () | |
| 17 | + { | |
| 18 | + params.put("info", "hello"); | |
| 19 | + } | |
| 20 | + | |
| 21 | + | |
| 22 | + @Test | |
| 23 | + public void httpRemote() | |
| 24 | + { | |
| 25 | + | |
| 26 | + Map<String,String> params = new HashMap<>(); | |
| 27 | + params.put("k1", "fd"); | |
| 28 | + params.put("k5", "fd"); | |
| 29 | + params.put("k3", "fd"); | |
| 30 | + params.put("k2", "fd"); | |
| 31 | + | |
| 32 | + String content = HttpUtils.HTTP.get("https://www.baidu.com", params); | |
| 33 | + System.out.println(content); | |
| 34 | + } | |
| 35 | + | |
| 36 | + @Test | |
| 37 | + public void httpsRemote() { | |
| 38 | + | |
| 39 | + } | |
| 40 | + | |
| 41 | + | |
| 42 | +} |