com.tmsps.ne4Weixin.utils.HttpClient Maven / Gradle / Ivy
package com.tmsps.ne4Weixin.utils;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
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.ContentType;
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 HttpClient {
private static final Logger log = LoggerFactory.getLogger(HttpClient.class);
/**
* @author zhangwei [email protected]
* @param url
* @param params
* @return
*/
public static String httpGet(String url, Map params) {
String body = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
List pars = null;
if (null == params) {
pars = new ArrayList();
} else {
pars = new ArrayList(params.size());
for (Map.Entry entry : params.entrySet()) {
NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
pars.add(pair);
}
}
try {
String paraString = EntityUtils.toString(new UrlEncodedFormEntity(pars));
get.setURI(new URI(get.getURI().toString().concat("?").concat(paraString)));
log.debug("request method is GET and url is {}", get.getURI().toString().concat("?").concat(paraString));
HttpResponse httpResponse = httpClient.execute(get);
HttpEntity entity = httpResponse.getEntity();
body = EntityUtils.toString(entity, "UTF-8");
} catch (ParseException | IOException | URISyntaxException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
log.debug("get response is {}", body);
return body;
}
/**
* @author zhangwei [email protected]
* @param url
* @param params
* @return
*/
public static String httpPost(String url, Map params) {
String body = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(10000).build();
CloseableHttpResponse response = null;
Iterator> it = params.entrySet().iterator();
List pars = new ArrayList();
while (it.hasNext()) {
Entry entry = it.next();
pars.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
post.setEntity(new UrlEncodedFormEntity(pars, "UTF-8"));
post.setConfig(config);
response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
log.debug("get response is {}", body);
return body;
}
/**
* @author zhangwei [email protected]
* @param url
* @param json
* @return
*/
public static String postJson(String url, String json) {
String body = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(10000).build();
CloseableHttpResponse response = null;
post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
post.setConfig(config);
try {
response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
log.error(e.toString());
e.printStackTrace();
}
log.debug("get response is {}", body);
return body;
}
public static String postXML(String url, String data) {
String body = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(10000).build();
CloseableHttpResponse response = null;
post.setEntity(new StringEntity(data, ContentType.APPLICATION_XML));
post.setConfig(config);
try {
response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
log.error(e.toString());
e.printStackTrace();
}
log.debug("get response is {}", body);
return body;
}
}