个人实现的一个Http连接的一个工具类,可以需要的时候使用。
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.Consts; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.StatusLine; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; 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.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; /** * @Author jerry * @Date 17-9-8 */ public class HttpUtil { private static Logger LOGGER = LoggerFactory.getLogger(HttpUtil.class); private static int connectTimeout; private static int socketTimeout; private DefaultHttpRequestRetryHandler retryHandler; // 当url无host port 信息时 必须设置该信息 private HttpHost proxy; private HttpHead head; public static HttpUtil build() { connectTimeout = 5000; socketTimeout = 5000; return new HttpUtil(); } public HttpUtil setConnectionTimeOut(int connectionTimeOut) { this.connectTimeout = connectionTimeOut; return this; } public HttpUtil setSocketTimeOut(int socketTimeOut) { this.socketTimeout = socketTimeOut; return this; } public DefaultHttpRequestRetryHandler getRetryHandler() { return retryHandler; } public HttpUtil setRetryHandler(DefaultHttpRequestRetryHandler retryHandler) { this.retryHandler = retryHandler; return this; } public HttpHost getProxy() { return proxy; } public HttpUtil setProxy(HttpHost proxy) { this.proxy = proxy; return this; } public HttpHead getHead() { return head; } public HttpUtil setHead(HttpHead head) { this.head = head; return this; } public String doGet(String url) { CloseableHttpClient client = buildHttpClient(); try { HttpGet httpGet = new HttpGet(url); long start = System.currentTimeMillis(); HttpResponse response = client.execute(httpGet); LOGGER.info("http lost time: {}", (System.currentTimeMillis() - start) / 1000); StatusLine status = response.getStatusLine(); if (status.getStatusCode() == HttpStatus.SC_OK) { String entity = EntityUtils.toString(response.getEntity(), Consts.UTF_8); return entity; } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { closeHttpClient(client); } return null; } public JSONObject doPost(String url, String jsonParameter) { CloseableHttpClient client = buildHttpClient(); try { HttpPost httpPost = new HttpPost(url); if (!StringUtils.isEmpty(jsonParameter)) { StringEntity se = new StringEntity(jsonParameter, ContentType.APPLICATION_JSON); httpPost.setEntity(se); } if (head != null) { httpPost.setHeaders(head.getAllHeaders()); } long start = System.currentTimeMillis(); HttpResponse response; if (proxy != null) { response = client.execute(proxy, httpPost); } else { response = client.execute(httpPost); } LOGGER.info("http lost time: {}", (System.currentTimeMillis() - start) / 1000); StatusLine status = response.getStatusLine(); if (status.getStatusCode() == HttpStatus.SC_OK) { String entity = EntityUtils.toString(response.getEntity(), Consts.UTF_8); LOGGER.info("URI:{}, METHOD:POST, RESPONSE:{}", url, entity); return JSON.parseObject(entity); } else { LOGGER.info("URI:{}, METHOD:POST, RESPONSE:{}", url, status.getStatusCode()); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { closeHttpClient(client); } return null; } public CloseableHttpClient buildHttpClient() { HttpClientBuilder clientBuilder = HttpClientBuilder.create().useSystemProperties(); clientBuilder.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(connectTimeout) .setConnectionRequestTimeout(connectTimeout).setSocketTimeout(socketTimeout).build()); if (retryHandler != null) { clientBuilder.setRetryHandler(retryHandler); } if (proxy != null) { clientBuilder.setProxy(proxy); } return clientBuilder.build(); } private static void closeHttpClient(CloseableHttpClient httpClient) { try { if (httpClient != null) { httpClient.close(); } } catch (Exception e) { LOGGER.error("httpclient close exception", e); } } }