java发送Http请求
发送get请求
private static String REMOTE_SERVER;
public static String sendGet(String path, Map<String, String> params) throws Exception {
logger.info("sendGet path={}", path, params);
String url = null;
URIBuilder uriBuilder = new URIBuilder(REMOTE_SERVER);
uriBuilder.setPath(path);
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue());
}
}
url = uriBuilder.build().toASCIIString();
logger.info("url={}", url);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
String response = null;
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpget);
HttpEntity httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity, "utf-8");
EntityUtils.consume(httpEntity);
} catch (IOException e) {
logger.error("httputil sendGet error.", e);
throw new DataCenterException(500, "httputil sendJsonPost error.");
} finally {
try {
if (httpResponse != null) {
httpResponse.close();
}
} catch (IOException e) {
logger.error("httputil release httpClient error.", e);
throw new DataCenterException(500, "httputil release httpClient error.");
}
}
return response;
}
发送Post请求
public static String sendPost(String path, Map<String, String> params) throws Exception {
logger.info("sendPost path={}", path, params);
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(REMOTE_SERVER);
uriBuilder.setPath(path);
String url = uriBuilder.build().toASCIIString();
HttpPost httpPost = new HttpPost(url);
String encode = "UTF-8";
if (params.size() != 0) {
List<NameValuePair> paramList = Lists.newArrayList();
for (String key : params.keySet()) {
paramList.add(new BasicNameValuePair(key, params.get(key)));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, encode);
httpPost.setEntity(entity);
}
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
String response = null;
try {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
response = EntityUtils.toString(httpEntity, encode);
}
EntityUtils.consume(httpEntity);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (httpResponse != null) {
httpResponse.close();
}
} catch (IOException e) {
logger.error("release resouce error: " + e);
}
}
return response;
}
发送Json的Post请求
public static String sendJsonPost(String url, String json) throws Exception {
logger.info("sendJsonPost url={}", url, json);
URIBuilder uriBuilder = new URIBuilder(REMOTE_SERVER);
uriBuilder.setPath(url);
url = uriBuilder.build().toASCIIString();
CloseableHttpClient httpClient = HttpClients.createDefault();
System.out.println(url);
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
if (json != null){
httpPost.setEntity(new StringEntity(json, "UTF-8"));
}
String responseContent = null;
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null){
responseContent = EntityUtils.toString(entity, "UTF-8");
}
} catch (IOException e) {
logger.error("httputil sendJsonPost error.", e);
throw new DataCenterException(500, "httputil sendJsonPost error.");
} finally {
try {
if (response != null) response.close();
} catch (IOException e) {
logger.error("httputil release response error.", e);
throw new DataCenterException(500, "httputil release response error.");
}
try {
if (httpClient != null) httpClient.close();
} catch (IOException e) {
logger.error("httputil release httpClient error.", e);
throw new DataCenterException(500, "httputil release httpClient error.");
}
}
return responseContent;
}
发送Json参数的Put请求
public static String sendJsonPut(String url, String json) throws Exception {
logger.info("sendJsonPut url={}", url, json);
URIBuilder uriBuilder = new URIBuilder(REMOTE_SERVER);
uriBuilder.setPath(url);
url = uriBuilder.build().toASCIIString();
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
httpPut.addHeader("Content-Type", "application/json");
httpPut.setEntity(new StringEntity(json, "UTF-8"));
String responseContent = null;
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPut);
HttpEntity entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
logger.error("httputil release response error.", e);
throw new DataCenterException(500, "httputil sendJsonPost error.");
} finally {
try {
if (response != null) response.close();
} catch (IOException e) {
logger.error("httputil release response error.", e);
throw new DataCenterException(500, "httputil release response error.");
}
try {
if (httpClient != null) httpClient.close();
} catch (IOException e) {
logger.error("httputil release httpClient error.", e);
throw new DataCenterException(500, "httputil release httpClient error.");
}
}
return responseContent;
}
发送Delete请求
public static String sendDelete(String path, Map<String, String> params) throws Exception {
logger.info("sendDelete path={}", path, params);
URIBuilder uriBuilder = new URIBuilder(REMOTE_SERVER);
String url = null;
uriBuilder.setPath(path);
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue());
}
}
url = uriBuilder.build().toASCIIString();
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(url);
String response = null;
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpDelete);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
response = EntityUtils.toString(httpEntity, "utf-8");
}
EntityUtils.consume(httpEntity);
} catch (IOException e) {
logger.error("httputil sendGet error.", e);
throw new DataCenterException(500, "httputil sendJsonPost error.");
} finally {
try {
if (httpResponse != null) {
httpResponse.close();
}
} catch (IOException e) {
logger.error("httputil release httpClient error.", e);
throw new DataCenterException(500, "httputil release httpClient error.");
}
}
return response;
}