java httputils_HttpUtils JAVA

package iih.custom.common.util;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.nio.charset.Charset;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import java.util.Set;

import org.apache.http.Header;

import org.apache.http.HttpEntity;

import org.apache.http.HttpHost;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.HttpClient;

import org.apache.http.client.HttpRequestRetryHandler;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.methods.HttpRequestBase;

import org.apache.http.config.Registry;

import org.apache.http.config.RegistryBuilder;

import org.apache.http.conn.routing.HttpRoute;

import org.apache.http.conn.socket.ConnectionSocketFactory;

import org.apache.http.conn.socket.PlainConnectionSocketFactory;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.protocol.HttpContext;

import org.apache.http.util.EntityUtils;

import iih.custom.common.log.CustomBizLogger;

/**

* httpclient 工具类

* @author tian.hq 2019.05.31

*

*/

public class HttpUtils {

private static final int timeOut = 100 * 1000;

/**

* 创建httpClient 对象

* @param ip

* @param port

* @return

*/

public static HttpClient getHttpClient(String ip,Integer port) {

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(getDefaultRegistry());

// 将最大连接数增加

cm.setMaxTotal(200);

cm.setDefaultMaxPerRoute(200);

HttpHost httpHost = new HttpHost(ip, port);

// 将目标主机的最大连接数增加

cm.setMaxPerRoute(new HttpRoute(httpHost), 200);

HttpClient httpClient = HttpClients.custom().setConnectionManager(cm)

// 设置重试次数

.setRetryHandler(new HttpRequestRetryHandler() {

@Override

public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {

if (executionCount > 3) {

CustomBizLogger.info("Maximum tries reached for client http pool");

return false;

}

// 服务器端没有返回的情况,一般是连接池里面的连接失效,需要重试

if (exception instanceof org.apache.http.NoHttpResponseException) {

CustomBizLogger.info("No response from server on " + executionCount + " call");

return true;

}

if (exception instanceof java.net.SocketException) {

// 客户端主动关闭连接的情况,一般是连接池里面的连接失效,需要重试

if (exception.getMessage().indexOf("Connection reset") >= 0) {

CustomBizLogger.info("java.net.SocketException Connection reset " + executionCount + " call");

return true;

}

}

if (exception instanceof java.net.SocketTimeoutException) {

// 连接或者响应超时,需要重试

CustomBizLogger.info("java.net.SocketTimeoutException: Read timed out " + executionCount + " call");

return true;

}

return false;

}

})

.build();

return httpClient;

}

private static Registry getDefaultRegistry() {

return RegistryBuilder.create()

.register("http", PlainConnectionSocketFactory.getSocketFactory())

.register("https", SSLConnectionSocketFactory.getSocketFactory())

.build();

}

/**

* httpClient 发送post请求

* @param client

* @param url

* @param params

* @return 返回

* @throws Exception

*/

public static String sendPost(HttpClient client,String url,Map params) throws Exception {

HttpPost httppost = new HttpPost(url);

config(httppost);

setPostParams(httppost, params);

HttpResponse response = null;

try {

response = client.execute(httppost);

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity, "utf-8");

EntityUtils.consume(entity);

return result;

} catch (Exception e) {

CustomBizLogger.error("httpClient 发送post请求异常",e);

throw e;

}

}

public static String sendPost(HttpClient client,String url,String data) throws Exception {

HttpPost httppost = new HttpPost(url);

config(httppost);

StringEntity stringEntity = new StringEntity(data,Charset.forName("UTF-8"));

httppost.setEntity(stringEntity);

HttpResponse response = null;

try {

response = client.execute(httppost);

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity, "UTF-8");

EntityUtils.consume(entity);

return result;

} catch (Exception e) {

CustomBizLogger.error("httpClient 发送post请求异常",e);

throw e;

}

}

/***

* 发送post 请求 ,附带header信息

* @param client

* @param url

* @param data

* @param headers

* @return

* @throws Exception

*/

public static String sendPost(HttpClient client,String url,String data,Map headers) throws Exception {

HttpPost httppost = new HttpPost(url);

config(httppost);

fillHttpPostHeader(httppost,headers);

StringEntity stringEntity = new StringEntity(data,Charset.forName("UTF-8"));

httppost.setEntity(stringEntity);

HttpResponse response = null;

try {

response = client.execute(httppost);

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity, "UTF-8");

EntityUtils.consume(entity);

return result;

} catch (Exception e) {

CustomBizLogger.error("httpClient 发送post请求异常",e);

throw e;

}

}

private static void setPostParams(HttpPost httpost, Map params) {

List nvps = new ArrayList();

Set keySet = params.keySet();

for (String key : keySet) {

nvps.add(new BasicNameValuePair(key, params.get(key).toString()));

}

try {

httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

CustomBizLogger.error("设置http post 参数异常",e);

}

}

private static void config(HttpRequestBase httpRequestBase) {

// 配置请求的超时设置

RequestConfig requestConfig = RequestConfig.custom()

.setConnectionRequestTimeout(timeOut)

.setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();

httpRequestBase.setConfig(requestConfig);

}

/**

* 为HttpPost添加header信息

* @param post

* @param headers

*/

private static void fillHttpPostHeader(HttpPost post,Map headers){

if(headers == null){

return ;

}

for(String key:headers.keySet()){

post.addHeader(key, headers.get(key));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值