import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
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.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.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpServiceUtils {
private static final Logger log = LoggerFactory.getLogger(HttpServiceUtils.class);
private static volatile CloseableHttpClient httpClient;
private static final String DEFAULT_ENCODING = "UTF-8";
public static CloseableHttpClient getHttpClient() {
if (httpClient == null) {
synchronized (HttpServiceUtils.class) {
if (httpClient == null) {
httpClient = HttpClientBuilder.create().build();
}
}
}
return httpClient;
}
/**
* post请求
*
* @param url
* @param content
* @return
*/
public static String postRequest(String url, String content) {
return postRequest(url, content, DEFAULT_ENCODING, DEFAULT_ENCODING);
}
/**
* post请求
*
* @param url
* @param content
* @param requestEncode
* @param responseEncode
* @return
*/
public static String postRequest(String url, String content, String requestEncode, String responseEncode) {
log.debug("Post request [" + url + "] with content [" + content + "]");
String result = null;
CloseableHttpResponse response = null;
try {
HttpPost post = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000)
.setConnectionRequestTimeout(30000).build();
post.setConfig(requestConfig);
post.setEntity(new StringEntity(content, ContentType.create("text/xml", requestEncode)));
CloseableHttpClient client = getHttpClient();
response = client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, responseEncode);
EntityUtils.consume(entity);
log.debug("request successful with content [" + content + "], response [" + result + "]");
} else {
log.debug("request failed with content [" + content + "], status code [" + statusCode + "]");
}
} catch (Exception e) {
log.debug("post request failed with content [" + content + "]!", e);
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
// ignore
}
}
return result;
}
/**
* post请求
*
* @param paramMap
* @param url
* @return
*/
public static String postRequest(String url, Map paramMap) {
return postRequest(url, paramMap, "UTF-8", "UTF-8");
}
/**
* post请求
*
* @param url
* @param paramMap
* @param requestEncode
* @param responseEncode
* @return
*/
public static String postRequest(String url, Map paramMap, String requestEncode,
String responseEncode) {
log.debug("Post request [" + url + "] with paramMap [" + paramMap + "]");
List pairList = new ArrayList();
for (Entry entry : paramMap.entrySet()) {
pairList.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
}
String result = null;
CloseableHttpResponse response = null;
try {
HttpPost post = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000)
.build();
post.setConfig(requestConfig);
post.setEntity(new UrlEncodedFormEntity(pairList, requestEncode));
CloseableHttpClient client = getHttpClient();
response = client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, responseEncode);
EntityUtils.consume(entity);
log.debug("request successful with paramMap [" + paramMap + "], response [" + result + "]");
} else {
log.debug("request failed with paramMap [" + paramMap + "], status code [" + statusCode + "]");
}
} catch (Exception e) {
log.debug("post request failed with paramMap [" + paramMap + "]!", e);
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
// ignore
}
}
return result;
}
public static void main(String[] args) {
}
}