java http请求线程池_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 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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值