Commit f7ac73b182af0c1848863771bcdd007aa05ce7e6
1 parent
7ac2ae05
update...
Showing
2 changed files
with
103 additions
and
3 deletions
src/main/java/com/bsth/data/led_http/LedHttpPushHandler.java
| 1 | 1 | package com.bsth.data.led_http; |
| 2 | 2 | |
| 3 | -import com.bsth.data.utils.HttpClientUtils; | |
| 3 | +import com.bsth.data.utils.HttpClientUtils_tms; | |
| 4 | 4 | import com.bsth.entity.ac.CarInOutEntity; |
| 5 | 5 | import com.bsth.entity.real.RealCarPark; |
| 6 | 6 | import com.bsth.util.ConfigUtil; |
| ... | ... | @@ -41,7 +41,7 @@ public class LedHttpPushHandler { |
| 41 | 41 | |
| 42 | 42 | try { |
| 43 | 43 | logger.info("push http led out:" + params); |
| 44 | - HttpClientUtils.get(url); | |
| 44 | + HttpClientUtils_tms.get(url); | |
| 45 | 45 | } catch (Exception e) { |
| 46 | 46 | logger.error("", e); |
| 47 | 47 | } |
| ... | ... | @@ -53,7 +53,7 @@ public class LedHttpPushHandler { |
| 53 | 53 | |
| 54 | 54 | try { |
| 55 | 55 | logger.info("push http led in:" + params); |
| 56 | - HttpClientUtils.get(url); | |
| 56 | + HttpClientUtils_tms.get(url); | |
| 57 | 57 | } catch (Exception e) { |
| 58 | 58 | logger.error("", e); |
| 59 | 59 | } | ... | ... |
src/main/java/com/bsth/data/utils/HttpClientUtils_tms.java
0 → 100644
| 1 | +package com.bsth.data.utils; | |
| 2 | + | |
| 3 | +import org.apache.http.HttpEntity; | |
| 4 | +import org.apache.http.client.config.RequestConfig; | |
| 5 | +import org.apache.http.client.methods.CloseableHttpResponse; | |
| 6 | +import org.apache.http.client.methods.HttpGet; | |
| 7 | +import org.apache.http.client.methods.HttpPost; | |
| 8 | +import org.apache.http.entity.StringEntity; | |
| 9 | +import org.apache.http.impl.client.CloseableHttpClient; | |
| 10 | +import org.apache.http.impl.client.HttpClients; | |
| 11 | +import org.slf4j.Logger; | |
| 12 | +import org.slf4j.LoggerFactory; | |
| 13 | + | |
| 14 | +import java.io.BufferedReader; | |
| 15 | +import java.io.IOException; | |
| 16 | +import java.io.InputStreamReader; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * 快速超时,内网用 | |
| 20 | + * Created by panzhao on 2017/8/2. | |
| 21 | + */ | |
| 22 | +public class HttpClientUtils_tms { | |
| 23 | + | |
| 24 | + static Logger logger = LoggerFactory.getLogger(HttpClientUtils_tms.class); | |
| 25 | + | |
| 26 | + public static StringBuilder get(String url) throws Exception { | |
| 27 | + CloseableHttpClient httpClient = null; | |
| 28 | + CloseableHttpResponse response = null; | |
| 29 | + StringBuilder stringBuffer = null; | |
| 30 | + try { | |
| 31 | + httpClient = HttpClients.createDefault(); | |
| 32 | + HttpGet get = new HttpGet(url); | |
| 33 | + //超时时间 | |
| 34 | + RequestConfig requestConfig = RequestConfig.custom() | |
| 35 | + .setConnectTimeout(2500).setConnectionRequestTimeout(2000) | |
| 36 | + .setSocketTimeout(2500).build(); | |
| 37 | + get.setConfig(requestConfig); | |
| 38 | + get.addHeader("Content-Encoding", "gzip"); | |
| 39 | + | |
| 40 | + response = httpClient.execute(get); | |
| 41 | + stringBuffer = getResult(response.getEntity()); | |
| 42 | + } catch (Exception e) { | |
| 43 | + logger.error("", e); | |
| 44 | + } finally { | |
| 45 | + if (null != httpClient) | |
| 46 | + httpClient.close(); | |
| 47 | + if (null != response) | |
| 48 | + response.close(); | |
| 49 | + } | |
| 50 | + return stringBuffer; | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * raw post data | |
| 55 | + * @param url | |
| 56 | + * @param data | |
| 57 | + * @return | |
| 58 | + */ | |
| 59 | + public static StringBuilder post(String url, String data) throws Exception { | |
| 60 | + CloseableHttpClient httpClient = null; | |
| 61 | + CloseableHttpResponse response = null; | |
| 62 | + StringBuilder stringBuffer = null; | |
| 63 | + try { | |
| 64 | + httpClient = HttpClients.createDefault(); | |
| 65 | + HttpPost post = new HttpPost(url); | |
| 66 | + | |
| 67 | + post.setHeader("Accept", "application/json"); | |
| 68 | + post.setHeader("Content-Type", "application/json"); | |
| 69 | + //超时时间 | |
| 70 | + RequestConfig requestConfig = RequestConfig.custom() | |
| 71 | + .setConnectTimeout(2500).setConnectionRequestTimeout(2000) | |
| 72 | + .setSocketTimeout(2500).build(); | |
| 73 | + post.setConfig(requestConfig); | |
| 74 | + post.setEntity((new StringEntity(data, "UTF-8"))); | |
| 75 | + | |
| 76 | + response = httpClient.execute(post); | |
| 77 | + stringBuffer = getResult(response.getEntity()); | |
| 78 | + } catch (Exception e) { | |
| 79 | + logger.error("", e); | |
| 80 | + } finally { | |
| 81 | + if (null != httpClient) | |
| 82 | + httpClient.close(); | |
| 83 | + if (null != response) | |
| 84 | + response.close(); | |
| 85 | + } | |
| 86 | + return stringBuffer; | |
| 87 | + } | |
| 88 | + | |
| 89 | + private static StringBuilder getResult(HttpEntity entity) throws IOException { | |
| 90 | + StringBuilder stringBuffer = null; | |
| 91 | + if (null != entity) { | |
| 92 | + BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); | |
| 93 | + stringBuffer = new StringBuilder(); | |
| 94 | + String str = ""; | |
| 95 | + while ((str = br.readLine()) != null) | |
| 96 | + stringBuffer.append(str); | |
| 97 | + } | |
| 98 | + return stringBuffer; | |
| 99 | + } | |
| 100 | +} | ... | ... |