Http请求封装(对HttpClient类的进一步封装,使之调用更方便。另外,此类管理唯一的HttpClient对象,支持线程池调用,效率更高)
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);
}
}