// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.sprucetec.datacenter.util; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import java.util.Set; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HttpClientUtil { private static Logger log = LoggerFactory.getLogger(HttpClientUtil.class); public static final int DEFAULT_SOCKET_TIMEOUT = 5000; public static final int DEFAULT_CONNECT_TIMEOUT = 5000; public static final int DEFAULT_CONNECTION_REQUEST_TIMEOUT = 5000; private static CloseableHttpClient httpClient = HttpClients.createDefault(); public HttpClientUtil() { } public static String post(String url) { return post(url, (Map)null); } public static String post(String url, Map<String, String> param) { return post(url, param, RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).build()); } public static String post(String url, Map<String, String> param, RequestConfig requestConfig) { HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); CloseableHttpResponse response = null; Object responseString = null; if(null != param && param.size() > 0) { Set e = param.keySet(); ArrayList nvps = new ArrayList(); Iterator e1 = e.iterator(); while(e1.hasNext()) { String key = (String)e1.next(); nvps.add(new BasicNameValuePair(key, (String)param.get(key))); } try { log.info("set utf-8 form entity to httppost"); httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8.name())); } catch (UnsupportedEncodingException var18) { throw new RuntimeException(var18.getMessage(), var18); } } try { response = httpClient.execute(httpPost); HttpEntity e2 = response.getEntity(); if(e2 != null) { String nvps1 = EntityUtils.toString(e2); return nvps1; } } catch (IOException var20) { throw new RuntimeException(var20.getMessage(), var20); } finally { try { if(response != null) { response.close(); } } catch (IOException var19) { log.warn("response is not closed"); throw new RuntimeException(var19.getMessage(), var19); } } return (String)responseString; } public static String postJson(String url) { return postJson(url, (String)null); } public static String postJson(String url, String jsonEntity) { return postJson(url, jsonEntity, RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).build()); } public static String postJson(String url, String jsonEntity, RequestConfig requestConfig) { HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); httpPost.addHeader("Content-Type", "application/json;charset=utf-8"); httpPost.addHeader("Connection", "Keep-Alive"); httpPost.addHeader("user-agent", "MEICAI/WMC"); CloseableHttpResponse response = null; Object responseString = null; if(jsonEntity != null) { httpPost.setEntity(new StringEntity(jsonEntity, Consts.UTF_8.name())); } try { response = httpClient.execute(httpPost); HttpEntity e = response.getEntity(); if(e != null) { String var7 = EntityUtils.toString(e); return var7; } } catch (IOException var17) { throw new RuntimeException(var17.getMessage(), var17); } finally { try { if(response != null) { response.close(); } } catch (IOException var16) { log.warn("response is not closed"); throw new RuntimeException(var16.getMessage(), var16); } } return (String)responseString; } public static String get(String url) { String result = ""; try { HttpGet e = new HttpGet(url); CloseableHttpResponse httpResponse = null; httpResponse = httpClient.execute(e); try { HttpEntity entity = httpResponse.getEntity(); if(null != entity) { result = EntityUtils.toString(entity); } } finally { httpResponse.close(); } } catch (Exception var9) { var9.printStackTrace(); } return result; } }
HttpClientUtil
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.