|
|
package com.monitor.common.util;
|
|
|
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
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.BasicHeader;
|
|
|
import org.apache.http.protocol.HTTP;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Base64;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 发送短信
|
|
|
*
|
|
|
* @author yoho
|
|
|
*
|
|
|
*/
|
|
|
public class HttpClientUtil {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
|
|
|
|
|
|
private final static Integer CONNECT_TIME_OUT = 10 * 1000;
|
|
|
|
|
|
private final static Integer CONNECT_REQUEST_TIME_OUT = 5 * 1000;
|
|
|
|
|
|
private final static Integer SOCKET_TIME_OUT = 30 * 1000;
|
|
|
|
|
|
private final static String ASK = "?";
|
|
|
|
|
|
private final static String AND = "&";
|
|
|
|
|
|
private final static String UTF8 = "UTF-8";
|
|
|
|
|
|
private final static String APPLICATION_JSON = "application/json; charset=UTF-8";
|
|
|
|
|
|
public static String doget(final String uri, final Map<String, String> content,final Map<String, String> header) {
|
|
|
String strResult = "";
|
|
|
RequestConfig defaultRequestConfig = RequestConfig
|
|
|
.custom()
|
|
|
.setSocketTimeout(SOCKET_TIME_OUT.intValue())
|
|
|
.setConnectTimeout(CONNECT_TIME_OUT.intValue())
|
|
|
.setConnectionRequestTimeout(CONNECT_REQUEST_TIME_OUT.intValue()).build();
|
|
|
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
|
|
|
HttpGet get = null;
|
|
|
try {
|
|
|
StringBuilder url = new StringBuilder(uri);
|
|
|
if(content != null){
|
|
|
url.append(ASK);
|
|
|
for (Map.Entry<String, String> entry : content.entrySet()) {
|
|
|
url.append(entry.getKey()+"="+entry.getValue()+AND);
|
|
|
}
|
|
|
get = new HttpGet(url.toString().substring(0, url.length()-1));
|
|
|
}else{
|
|
|
get = new HttpGet(url.toString());
|
|
|
}
|
|
|
if (header != null) {
|
|
|
for (Map.Entry<String, String> entry : header.entrySet()) {
|
|
|
get.setHeader(entry.getKey(), entry.getValue());
|
|
|
}
|
|
|
}
|
|
|
CloseableHttpResponse response = client.execute(get);
|
|
|
int statusCode = response.getStatusLine().getStatusCode();
|
|
|
if (200 == statusCode) {
|
|
|
strResult = EntityUtils.toString(response.getEntity()).trim();
|
|
|
} else {
|
|
|
logger.error("|HttpClientUtil|doGet|error status|", statusCode);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
logger.error("|HttpClientUtil|doGet|", e);
|
|
|
} finally {
|
|
|
if (get != null) {
|
|
|
get.releaseConnection();
|
|
|
}
|
|
|
if (client != null) {
|
|
|
try {
|
|
|
client.close();
|
|
|
} catch (IOException e) {
|
|
|
logger.error("|HttpClientUtil|doGet|", e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return strResult;
|
|
|
}
|
|
|
|
|
|
public static String doPost(final String uri, final String args, Map<String, String> header) {
|
|
|
String strResult = "";
|
|
|
RequestConfig defaultRequestConfig = RequestConfig.custom()
|
|
|
.setSocketTimeout(SOCKET_TIME_OUT.intValue())
|
|
|
.setConnectTimeout(CONNECT_TIME_OUT.intValue())
|
|
|
.setConnectionRequestTimeout(CONNECT_REQUEST_TIME_OUT.intValue())
|
|
|
.build();
|
|
|
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
|
|
|
HttpPost post = new HttpPost(uri);
|
|
|
if (header != null) {
|
|
|
for (Map.Entry<String, String> entry : header.entrySet()) {
|
|
|
post.setHeader(entry.getKey(), entry.getValue());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
StringEntity se = null;
|
|
|
try {
|
|
|
se = new StringEntity(args, UTF8);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("|HttpClientUtil|doPost|", e);
|
|
|
}
|
|
|
//se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
|
|
|
//se.setContentType(APPLICATION_JSON);
|
|
|
post.setEntity(se);
|
|
|
CloseableHttpResponse response = null;
|
|
|
try {
|
|
|
response = client.execute(post);
|
|
|
int statusCode = response.getStatusLine().getStatusCode();
|
|
|
if (200 == statusCode) {
|
|
|
strResult = new String(EntityUtils.toByteArray(response.getEntity()), UTF8);
|
|
|
} else {
|
|
|
logger.error("|HttpClientUtil|doPost|error status|", statusCode);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
logger.error("|HttpClientUtil|doPost|", e);
|
|
|
} finally {
|
|
|
if (post != null) {
|
|
|
post.releaseConnection();
|
|
|
}
|
|
|
if (client != null) {
|
|
|
try {
|
|
|
client.close();
|
|
|
} catch (IOException e) {
|
|
|
logger.error("|HttpClientUtil|doPost|", e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return strResult;
|
|
|
}
|
|
|
|
|
|
public static String opentsdbPut(final String uri, final String args) {
|
|
|
String strResult = "";
|
|
|
RequestConfig defaultRequestConfig = RequestConfig.custom()
|
|
|
.setSocketTimeout(SOCKET_TIME_OUT.intValue())
|
|
|
.setConnectTimeout(CONNECT_TIME_OUT.intValue())
|
|
|
.setConnectionRequestTimeout(CONNECT_REQUEST_TIME_OUT.intValue())
|
|
|
.build();
|
|
|
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
|
|
|
HttpPost post = new HttpPost(uri);
|
|
|
|
|
|
StringEntity se = null;
|
|
|
try {
|
|
|
se = new StringEntity(args, UTF8);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("|HttpClientUtil|opentsdbPut|", e);
|
|
|
}
|
|
|
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
|
|
|
se.setContentType(APPLICATION_JSON);
|
|
|
post.setEntity(se);
|
|
|
CloseableHttpResponse response = null;
|
|
|
try {
|
|
|
response = client.execute(post);
|
|
|
int statusCode = response.getStatusLine().getStatusCode();
|
|
|
if (204 != statusCode) {
|
|
|
logger.error("|HttpClientUtil|opentsdbPut|error status|", statusCode);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
logger.error("|HttpClientUtil|opentsdbPut|", e);
|
|
|
} finally {
|
|
|
if (post != null) {
|
|
|
post.releaseConnection();
|
|
|
}
|
|
|
if (client != null) {
|
|
|
try {
|
|
|
client.close();
|
|
|
} catch (IOException e) {
|
|
|
logger.error("|HttpClientUtil|opentsdbPut|", e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return strResult;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* main函数.
|
|
|
* @param args
|
|
|
* 启动参数
|
|
|
* @throws Exception
|
|
|
* Exception
|
|
|
*/
|
|
|
public static void main(String... args) throws Exception {
|
|
|
final String plainCreds = "yoho:yoho";
|
|
|
final byte[] plainCredsBytes = plainCreds.getBytes();
|
|
|
final byte[] base64CredsBytes = Base64.getEncoder().encode(plainCredsBytes);
|
|
|
final String base64Creds = new String(base64CredsBytes);
|
|
|
Map<String,String> map = new HashMap<String,String>();
|
|
|
map.put("Authorization", "Basic "+ base64Creds);
|
|
|
|
|
|
String result = doget("http://192.168.102.211:15672/api/vhosts", null, map);
|
|
|
System.out.println(result);
|
|
|
}
|
|
|
|
|
|
public static String getRemortIP(HttpServletRequest request) {
|
|
|
if (request.getHeader("x-forwarded-for") == null) {
|
|
|
return request.getRemoteAddr();
|
|
|
}
|
|
|
return request.getHeader("x-forwarded-for");
|
|
|
}
|
|
|
} |
...
|
...
|
|