Commit 73715d80ac2a174b16d7298bd18267a3f71a21cc
1 parent
ceee5fa6
1.HttpClients支持https请求
Showing
1 changed file
with
16 additions
and
3 deletions
src/main/java/com/bsth/util/HttpClientUtils.java
| @@ -19,6 +19,8 @@ import java.io.IOException; | @@ -19,6 +19,8 @@ import java.io.IOException; | ||
| 19 | import java.io.InputStreamReader; | 19 | import java.io.InputStreamReader; |
| 20 | import java.security.cert.CertificateException; | 20 | import java.security.cert.CertificateException; |
| 21 | import java.security.cert.X509Certificate; | 21 | import java.security.cert.X509Certificate; |
| 22 | +import java.util.HashMap; | ||
| 23 | +import java.util.Map; | ||
| 22 | 24 | ||
| 23 | /** | 25 | /** |
| 24 | * Created by panzhao on 2017/8/2. | 26 | * Created by panzhao on 2017/8/2. |
| @@ -76,7 +78,7 @@ public class HttpClientUtils { | @@ -76,7 +78,7 @@ public class HttpClientUtils { | ||
| 76 | CloseableHttpResponse response = null; | 78 | CloseableHttpResponse response = null; |
| 77 | StringBuilder stringBuffer = null; | 79 | StringBuilder stringBuffer = null; |
| 78 | try { | 80 | try { |
| 79 | - httpClient = HttpClients.createDefault(); | 81 | + httpClient = defaultHttpClient(url); |
| 80 | HttpGet get = new HttpGet(url); | 82 | HttpGet get = new HttpGet(url); |
| 81 | //超时时间 | 83 | //超时时间 |
| 82 | RequestConfig requestConfig = RequestConfig.custom() | 84 | RequestConfig requestConfig = RequestConfig.custom() |
| @@ -104,21 +106,32 @@ public class HttpClientUtils { | @@ -104,21 +106,32 @@ public class HttpClientUtils { | ||
| 104 | * @return | 106 | * @return |
| 105 | */ | 107 | */ |
| 106 | public static StringBuilder post(String url, String data) throws Exception { | 108 | public static StringBuilder post(String url, String data) throws Exception { |
| 109 | + return post(url, data, new HashMap<>()); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + public static StringBuilder post(String url, String data, Map<String, Object> headers) throws Exception { | ||
| 107 | CloseableHttpClient httpClient = null; | 113 | CloseableHttpClient httpClient = null; |
| 108 | CloseableHttpResponse response = null; | 114 | CloseableHttpResponse response = null; |
| 109 | StringBuilder stringBuffer = null; | 115 | StringBuilder stringBuffer = null; |
| 110 | try { | 116 | try { |
| 111 | - httpClient = HttpClients.createDefault(); | 117 | + httpClient = defaultHttpClient(url); |
| 112 | HttpPost post = new HttpPost(url); | 118 | HttpPost post = new HttpPost(url); |
| 113 | 119 | ||
| 114 | post.setHeader("Accept", "application/json"); | 120 | post.setHeader("Accept", "application/json"); |
| 115 | post.setHeader("Content-Type", "application/json;charset=UTF-8"); | 121 | post.setHeader("Content-Type", "application/json;charset=UTF-8"); |
| 122 | + if (headers.size() > 0) { | ||
| 123 | + for (Map.Entry<String, Object> header : headers.entrySet()) { | ||
| 124 | + post.setHeader(header.getKey(), String.valueOf(header.getValue())); | ||
| 125 | + } | ||
| 126 | + } | ||
| 116 | //超时时间 | 127 | //超时时间 |
| 117 | RequestConfig requestConfig = RequestConfig.custom() | 128 | RequestConfig requestConfig = RequestConfig.custom() |
| 118 | .setConnectTimeout(2500).setConnectionRequestTimeout(2000) | 129 | .setConnectTimeout(2500).setConnectionRequestTimeout(2000) |
| 119 | .setSocketTimeout(3500).build(); | 130 | .setSocketTimeout(3500).build(); |
| 120 | post.setConfig(requestConfig); | 131 | post.setConfig(requestConfig); |
| 121 | - post.setEntity((new StringEntity(data, "UTF-8"))); | 132 | + if (data != null) { |
| 133 | + post.setEntity((new StringEntity(data, "UTF-8"))); | ||
| 134 | + } | ||
| 122 | 135 | ||
| 123 | response = httpClient.execute(post); | 136 | response = httpClient.execute(post); |
| 124 | stringBuffer = getResult(response.getEntity()); | 137 | stringBuffer = getResult(response.getEntity()); |