java http请求线程池_Http请求封装(对HttpClient类的进一步封装,使之调用更方便。另外,此类管理唯一的HttpClient对象,支持线程池调用,效率更高)...

本文介绍了一个针对HTTPClient的封装类,旨在简化HTTP请求的发送过程并提高效率。该类支持POST和GET请求,能处理JSON及Protobuf等多种类型的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 headers, map postparams,

int timeout) {

list 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 getheaderlist(map headers) {

if (headers != null && headers.size() > 0) {

list headerlist = new arraylist();

for (entry 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 headers,

map postparams, int timeout) {

list 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 headers, string jsonbody, int timeout)  throws exception {

list 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 headers, string jsonbody,

int timeout)  throws exception {

list 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 headers, byte[] content, int timeout)  throws exception {

list 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 headers, int timeout) throws exception {

list 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 headers, int timeout) throws exception {

list headerlist = getheaderlist(headers);

return client.get(targeturl, headerlist, timeout);

}

}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值