1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
| import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Map;
import com.alibaba.fastjson.JSON; import org.apache.commons.httpclient.HttpStatus; import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.springframework.util.Assert;
public class HttpClientUtils {
private HttpClientUtils(){}
public static final int CONNECTION_TIMEOUT_MS = 5000;
public static final int SO_TIMEOUT_MS = 5000;
public static final String utf8 = "UTF-8";
public static final String application_json = "application/json";
public static final String gbk = "GBK";
public static String get(String url, Map<String, String> params) throws IOException, URISyntaxException { return get(url, params, utf8); }
public static String get(String url, Map<String, String> params, String charset) throws IOException, URISyntaxException { HttpClient client = buildHttpClient(true); HttpGet get = buildHttpGet(url, params, charset); HttpResponse response = client.execute(get); assertStatus(response);
HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity, charset); } return null; }
public static String post(String url, Map<String, String> params) throws URISyntaxException, IOException { return post(url, params, utf8); }
public static String postJSON(String url, Map<String, String> params) throws IOException, URISyntaxException { return postJSON(url, params, utf8); }
public static String post(String url, Map<String, String> params, String charset) throws URISyntaxException, IOException {
HttpClient client = buildHttpClient(true); HttpPost postMethod = buildHttpPost(url, params, charset); HttpResponse response = client.execute(postMethod); assertStatus(response); HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity, charset); }
return null; }
public static String postJSON(String url, Map params, String charset) throws URISyntaxException, IOException {
HttpClient client = buildHttpClient(true); HttpPost postMethod = buildHttpJSONPost(url, params, charset); HttpResponse response = client.execute(postMethod); assertStatus(response); HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity, charset); }
return null; }
public static HttpClient buildHttpClient(boolean isMultiThread) { CloseableHttpClient client; if (isMultiThread) client = HttpClientBuilder .create().setDefaultRequestConfig(buildRequestConfig()) .setRetryHandler(new DefaultHttpRequestRetryHandler()) .setConnectionManager( new PoolingHttpClientConnectionManager()).build(); else client = HttpClientBuilder.create().build(); return client; }
public static HttpPost buildHttpPost(String url, Map<String, String> params, String charset) throws UnsupportedEncodingException, URISyntaxException { Assert.notNull(url, "构建HttpPost时,url不能为null"); HttpPost post = new HttpPost(url); setCommonHttpMethod(post); if (params != null) { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> entry : params.entrySet()) { formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } HttpEntity he = new UrlEncodedFormEntity(formparams, charset); post.setEntity(he); } return post; }
public static HttpPost buildHttpJSONPost(String url, Map<String, String> params, String charset) throws UnsupportedEncodingException, URISyntaxException { Assert.notNull(url, "构建HttpPost时,url不能为null"); HttpPost post = new HttpPost(url); setJSONHttpMethod(post); if (params != null) { String json = JSON.toJSONString(params); System.out.println(json); StringEntity stringEntity = new StringEntity(json, utf8); stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, application_json)); post.setEntity(stringEntity); } return post; }
public static HttpGet buildHttpGet(String url, Map<String, String> params, String chatset) throws URISyntaxException { Assert.notNull(url, "构建HttpGet时,url不能为null"); return new HttpGet(buildGetUrl(url, params, chatset)); }
private static String buildGetUrl(String url, Map<String, String> params, String charset) { StringBuilder uriStr = new StringBuilder(url); if (params != null) { List<NameValuePair> ps = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) { ps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } uriStr.append("?"); uriStr.append(URLEncodedUtils.format(ps, charset)); } return uriStr.toString(); }
public static void setCommonHttpMethod(HttpRequestBase httpMethod) { httpMethod.setHeader(HTTP.CONTENT_ENCODING, utf8); }
public static void setJSONHttpMethod(HttpRequestBase httpMethod) { httpMethod.setHeader(HTTP.CONTENT_ENCODING, utf8); httpMethod.setHeader(HTTP.CONTENT_TYPE, application_json); }
public static void setContentLength(HttpRequestBase httpMethod, HttpEntity he) { if (he == null) { return; } httpMethod.setHeader(HTTP.CONTENT_LEN, String.valueOf(he.getContentLength())); }
public static RequestConfig buildRequestConfig() { return RequestConfig.custom() .setSocketTimeout(SO_TIMEOUT_MS) .setConnectTimeout(CONNECTION_TIMEOUT_MS).build(); }
static void assertStatus(HttpResponse res) throws IOException { Assert.notNull(res, "http响应对象为null"); Assert.notNull(res.getStatusLine(), "http响应对象的状态为null"); switch (res.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: break; default: throw new IOException("服务器响应状态异常,失败."); } } public static void main(String[] args) throws ClientProtocolException, IOException, URISyntaxException { System.out.println(get("http://www.baidu.com", new HashMap<String, String>())); } }
|