package com.ad.ssp.engine.common;
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.Header;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.adwm.lib.http.HttpClient;
import com.adwm.lib.http.HttpResult;
/**
* 对HttpClient类的进一步封装,使之调用更方便。另外,此类管理唯一的HttpClient对象,支持线程池调用,效率更高
*
*
*/
public class HttpClientUtil {
private static final Logger logger = LogManager.getLogger(HttpClientUtil.class);
// private static HttpClient client = new HttpClient(600);
private static HttpClient client = new HttpClient();
public static HttpClient getInstance() {
return client;
}
/**
* form方式提交http post请求
*
* @param targetUrl
* 目标url地址
* @param headers
* header请求参数集合
* @param postParams
* body请求参数集合
* @param timeout
* 超时时间(单位为毫秒)
* @return 返回的http body体经utf8编码后的字符串
*/
public static String postKVParams1(String targetUrl, Map<String, String> headers, Map<String, Object> postParams,
int timeout) {
List<Header> headerList = getHeaderList(headers);
try {
HttpResult hr = client.post(targetUrl, headerList, postParams, timeout);
if (hr.getStatus() == 200)
return hr.getContentAsString();
else {
logger.warn("The request url is: {}, its reponse code is: {}", targetUrl, hr.getStatus());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static List<Header> getHeaderList(Map<String, String> headers) {
if (headers != null && headers.size() > 0) {
List<Header> headerList = new ArrayList<Header>();
for (Entry<String, String> entry : headers.entrySet()) {
headerList.add(new BasicHeader(entry.getKey(), entry.getValue()));
}
return headerList;
}
return null;
}
/**
* form方式提交http post请求
*
* @param targetUrl
* 目标url地址
* @param headers
* header请求参数集合
* @param postParams
* body请求参数集合
* @param timeout
* 超时时间(单位为毫秒)
* @return 未处理的HttpResult对象,供调用方自己解析和处理
*/
public static HttpResult postKVParams2(String targetUrl, Map<String, String> headers,
Map<String, Object> postParams, int timeout) {
List<Header> headerList = getHeaderList(headers);
try {
return client.post(targetUrl, headerList, postParams, timeout);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 用post方法提交json字符串参数
*
* @param targetUrl
* 目标url地址
* @param headers
* header请求参数集合
* @param jsonBody
* json字符串
* @param timeout
* 超时时间(单位为毫秒)
* @return 返回的http body体经utf8编码后的字符串
*/
public static String postJsonParams1(String targetUrl, Map<String, String> headers, String jsonBody, int timeout) throws Exception {
List<Header> headerList = getHeaderList(headers);
StringEntity entity = new StringEntity(jsonBody, "UTF-8");
entity.setContentType("application/json");
entity.setContentEncoding("UTF-8");
HttpResult httpRst = client.post(targetUrl, headerList, entity, timeout);
if(httpRst.getStatus() == ResponseCodeUtil.HTTP_STATUS_OK)
return httpRst.getContentAsString();
else {
logger.warn("The request url is: {}, its reponse code is: {}", targetUrl, httpRst.getStatus());
}
return null;
}
/**
* 用post方法提交json字符串参数
*
* @param targetUrl
* 目标url地址
* @param headers
* header请求参数集合
* @param jsonBody
* json字符串
* @param timeout
* 超时时间(单位为毫秒)
* @return 未处理的HttpResult对象,供调用方自己解析和处理
*/
public static HttpResult postJsonParams2(String targetUrl, Map<String, String> headers, String jsonBody,
int timeout) throws Exception {
List<Header> headerList = getHeaderList(headers);
StringEntity entity = new StringEntity(jsonBody, "UTF-8");
entity.setContentType("application/json");
return client.post(targetUrl, headerList, entity, timeout);
}
/**
* 通过POST方式发起protocol请求
* @param url
* @param headers
* @param content
* @param timeout
* @return
*/
public static byte[] postProtobuf(String url, Map<String, String> headers, byte[] content, int timeout) throws Exception {
List<Header> headerList = getHeaderList(headers);
ByteArrayEntity entity = new ByteArrayEntity(content, ContentType.APPLICATION_OCTET_STREAM);
HttpResult httpResult = client.post(url, headerList, entity, timeout);
if (httpResult.getStatus() == ResponseCodeUtil.HTTP_STATUS_OK){
return httpResult.getContent();
} else {
logger.warn("The request url is: {}, its reponse code is: {}", url, httpResult.getStatus());
}
return null;
}
/**
* 以get方法请求url
*
* @param targetUrl
* 目标url地址
* @param headers
* header请求参数集合
* @param timeout
* 超时时间(单位为毫秒)
* @return 返回的http body体经utf8编码后的字符串
*/
public static String get1(String targetUrl, Map<String, String> headers, int timeout) throws Exception {
List<Header> headerList = getHeaderList(headers);
HttpResult httpRst = client.get(targetUrl, headerList, timeout);
if(httpRst.getStatus() == ResponseCodeUtil.HTTP_STATUS_OK)
return httpRst.getContentAsString();
else {
logger.warn("The request url is: {}, its reponse code is: {}", targetUrl, httpRst.getStatus());
}
return null;
}
/**
* 以get方法请求url
*
* @param targetUrl
* 目标url地址
* @param headers
* header请求参数集合
* @param timeout
* 超时时间(单位为毫秒)
* @return 未处理的HttpResult对象,供调用方自己解析和处理
*/
public static HttpResult get2(String targetUrl, Map<String, String> headers, int timeout) throws Exception {
List<Header> headerList = getHeaderList(headers);
return client.get(targetUrl, headerList, timeout);
}
}
Http请求封装(对HttpClient类的进一步封装,使之调用更方便。另外,此类管理唯一的HttpClient对象,支持线程池调用,效率更高)
阅读(1721) 评论(0)