技术经验分享:Javahttpclent请求httpclentUtils工具类

简介: 技术经验分享:Javahttpclent请求httpclentUtils工具类

第一种写法:


import java.io.IOException;


import java.io.InterruptedIOException;


import java.io.UnsupportedEncodingException;


import java.net.SocketTimeoutException;


import java.net.URLEncoder;


import java.net.UnknownHostException;


import java.nio.charset.Charset;


import java.security.KeyManagementException;


import java.security.KeyStoreException;


import java.security.NoSuchAlgorithmException;


import java.security.cert.CertificateException;


import java.security.cert.X509Certificate;


import java.util.ArrayList;


import java.util.List;


import java.util.Map;


import javax.activation.MimetypesFileTypeMap;


import javax.net.ssl.SSLContext;


import javax.net.ssl.SSLException;


import org.apache.commons.lang3.StringUtils;


import org.apache.http.Header;


import org.apache.http.HttpEntity;


import org.apache.http.HttpEntityEnclosingRequest;


import org.apache.http.HttpHost;


import org.apache.http.HttpRequest;


import org.apache.http.NameValuePair;


import org.apache.http.ParseException;


import org.apache.http.client.ClientProtocolException;


import org.apache.http.client.HttpRequestRetryHandler;


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


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


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


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


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


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


import org.apache.http.client.protocol.HttpClientContext;


import org.apache.http.conn.ConnectTimeoutException;


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


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


import org.apache.http.entity.ContentType;


import org.apache.http.entity.mime.MultipartEntityBuilder;


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


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


import org.apache.http.message.BasicNameValuePair;


import org.apache.http.protocol.HTTP;


import org.apache.http.protocol.HttpContext;


import org.apache.http.ssl.SSLContextBuilder;


import org.apache.http.util.EntityUtils;


import lombok.extern.slf4j.Slf4j;import net.sf.json.JSONObject;


import net.sf.json.JsonConfig;


import net.sf.json.util.PropertyFilter;


@Slf4j


public class HttpclientUtils {


private final static int CONNECT_TIMEOUT = 4000; // 连接超时毫秒


private final static int SOCKET_TIMEOUT = 30000; // 传输超时毫秒


private final static int REQUESTCONNECT_TIMEOUT = 3000; // 获取请求超时毫秒


private final static String ENCODE_CHARSET = "utf-8"; // 响应报文解码字符集


static class RetryHandler implements HttpRequestRetryHandler {


@Override


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


if (executionCount >= 3) {


// Do not retry if over max retry count


return false;


}


if (exception instanceof InterruptedIOException) {


// Timeout


return false;


}


if (exception instanceof UnknownHostException) {


// Unknown host


return false;


}


if (exception instanceof ConnectTimeoutException) {


// Connection refused


return false;


}


if (exception instanceof SSLException) {


// SSL handshake exception


return false;


}


HttpClientContext clientContext = HttpClientContext.adapt(context);


HttpRequest request = clientContext.getRequest();


boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);


if (idempotent) {


// Retry if the request is considered idempotent


return true;


}


return false;


}


};


public static CloseableHttpClient createSSLClientDefault(HttpHost proxy) {


try {


SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {


// 信任所有


@Override


public boolean isTrusted(X509Certificate【】 chain, String authType) throws CertificateException {


//代码效果参考:http://www.lyjsj.net.cn/wx/art_22942.html

return true;

}


}).build();


SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);


HttpRequestRetryHandler retryHandler = new RetryHandler();


Builder buider = RequestConfig.custom().setConnectionRequestTimeout(REQUESTCONNECT_TIMEOUT).setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT);


if (null //代码效果参考:http://www.lyjsj.net.cn/wz/art_22940.html

!= proxy) {

buider.setProxy(proxy);


}


RequestConfig requestConfig = buider.build();


HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(requestConfig).setRetryHandler(retryHandler).build();


} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {


log.error(e.getMessage(), e);


}


return HttpClients.createDefault();


}


public static String buildMap(Map map) {


StringBuffer sb = new StringBuffer();


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


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


sb.append(key + "=");


if (null == map.get(key)) {


sb.append("&");


} else {


//代码效果参考:http://www.lyjsj.net.cn/wz/art_22938.html

String value = String.valueOf(map.get(key));

try {


value = URLEncoder.encode(value, "UTF-8");


} catch (UnsupportedEncodingException e) {


log.error(e.getMessage(), e);


}


sb.append(value + "&");


}


}


}


return sb.toString();


}


/


发送HTTP_GET请求



@see 1)该方法会自动关闭连接,释放资源


@see 2)方法内设置了连接和读取超时时间,单位为毫秒,超时或发生其它异常时方法会自动返回"通信失败"字符串


@see 3)请求参数含中文时,经测试可直接传入中文,HttpClient会自动编码发给Server,应用时应根据实际效果决 定传入前是否转码


@see 4)该方法会自动获取到响应消息头中【Content-Type:text/html; charset=GBK】的charset值作为响应报文的 解码字符集


@see 5)若响应消息头中无Content-Type属性,则会使用HttpClient内部默认的ISO-8859-1作为响应报文的解码字符 集


@param reqURL 请求地址(含参数)


@return 远程主机响应正文 HttpHost proxy = new HttpHost("192.168.15.4", 3128);


/


public static JSONObject sendGetRequest(String reqURL, Map map, HttpHost proxy) {


String param = buildMap(map);


if (null != param) {


reqURL += "?" + param;


}


JSONObject respContent = new JSONObject(); // 响应内容


// reqURL = URLDecoder.decode(reqURL, ENCODE_CHARSET);


HttpGet httpget = new HttpGet(reqURL);


httpget.setHeader("Connection", "close");


CloseableHttpResponse response = null;


try {


response = createSSLClientDefault(proxy).execute(httpget, HttpClientContext.create()); // 执行GET请求


HttpEntity entity = response.getEntity(); // 获取响应实体


if (null != entity) {


Charset respCharset = Charset.forName(ENCODE_CHARSET);


respContent = JSONObject.fromObject(EntityUtils.toString(entity, respCharset), getJsonConfig());


log.info("发送get请求返回结果:{}", respContent);


EntityUtils.consume(entity);


} else {


log.error("发送get请求返回错误:{}", response);


}


} catch (ConnectTimeoutException cte) {


log.error("请求通信【" + reqURL + "】时连接超时,堆栈轨迹如下", cte);


respContent.put(CODE, -1000);


respContent.put(MSG, cte.getMessage());


} catch (SocketTimeoutException ste) {


log.error("请求通信【" + reqURL + "】时读取超时,堆栈轨迹如下", ste);


respContent.put(CODE, -1000);


respContent.put(MSG, ste.getMessage());


} catch (ClientProtocolException cpe) {


log.error("请求通信【" + reqURL + "】时协议异常,堆栈轨迹如下", cpe);


respContent.put(CODE, -1000);


respContent.put(MSG, cpe.getMessage());


} catch (ParseException pe) {


log.error("请求通信【" + reqURL + "】时解析异常,堆栈轨迹如下", pe);


respContent.put(CODE, -1000);


respContent.put(MSG, pe.getMessage());


} catch (IOException ioe) {


log.error("请求通信【" + reqURL + "】时网络异常,堆栈轨迹如下", ioe);


respContent.put(CODE, -1000);


respContent.put(MSG, ioe.getMessage());


} catch (Exception e) {


log.error("请求通信【" + reqURL + "】时偶遇异常,堆栈轨迹如下", e);


respContent.put(CODE, -1000);


respContent.put(MSG, e.getMessage());


} finally {


try {


if (response != null)


response.close();


} catch (IOException e) {


e.printStackTrace();


}


if (httpget != null) {


httpget.releaseConnection();


}


}


return respContent;


}


/


将null值的字段去掉



@return


/


private static JsonConfig getJsonConfig() {


JsonConfig jsonConfig = new JsonConfig();


jsonConfig.setJsonPropertyFilter(new PropertyFilter() {


@Override


public boolean apply(Object arg0, String arg1, Object arg2) {


return StringUtils.equals("null", String.valueOf(arg2));


}


});


return jsonConfig;


}


/*


发送HTTP_POST请求 type: 默认是表单请求,



@see 1)该方法允许自定义任何格式和内容的HTTP请求报文体


@see 2)该方法会自动关闭连接,释放资源


@see 3)方法内设置了连接和读取超时时间,单位为毫秒,超时或发生其它异常时方法会自动返回"通信失败"字符串


@see 4)请求参数含中文等特殊字符时,可直接传入本方法,并指明其编码字符集encodeCharset参数,方法内部会自 动对其转码


@see 5)该方法在解码响应报文时所采用的编码,取自响应消息头中的【Content-Type:text/html; charset=GBK】的 charset值


@see 6)若响应消息头中未指定Content-Type属性,则会使用HttpClient内部默认的ISO-8859-1


@param reqURL 请求地址


@param param 请求参数,若有多个参数则应拼接为param11=value11&22=value22&33=value33的形式


@param type 编码字符集,编码请求数据时用之,此参数为必填项(不能为""或null)


@return 远程主机响应正文


/


public static JSONObject sendPostRequest(String reqURL, Map map, String type, HttpHost proxy) {


JSONObject respContent = new JSONObject();


// 设置请求和传输超时时间


HttpPost httpPost = new HttpPost(reqURL);


httpPost.setHeader("Connection", "close");


// 这就有可能会导致服务端接收不到POST过去的参数,比如运行在Tomcat6.0.36中的Servlet,所以我们手工指定CONTENT_TYPE头消息


if (type != null && type.length() > 0) {


httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=" + ENCODE_CHARSET);


} else {


httpPost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=" + ENCODE_CHARSET);


}


CloseableHttpResponse response = null;


try {


// 判断map不为空


if (map != null) {


// 声明存放参数的List集合


List params = new ArrayList();


// 遍历map,设置参数到list中


for (Map.Entry entry : map.entrySet()) {


if (null != entry.getValue()) {


params.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));


}


}


// 创建form表单对象


UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, ENCODE_CHARSET);


formEntity.setContentType("Content-Type:application/json");


// 把表单对象设置到httpPost中


httpPost.setEntity(formEntity);


}


// reqURL = URLDecoder.decode(reqURL, ENCODE_CHARSET);


response = createSSLClientDefault(proxy).execute(httpPost, HttpClientContext.create());


HttpEntity entity = response.getEntity();


if (null != entity) {


respContent = JSONObject.fromObje

相关文章
|
Java 开发者
java实现Http请求
java实现Http请求
145 0
|
Java
Java导入Excel
本文介绍如何用Java导入Excel。
913 0
Java导入Excel
|
SQL Java 测试技术
3、Mybatis-Plus 自定义sql语句
这篇文章介绍了如何在Mybatis-Plus框架中使用自定义SQL语句进行数据库操作。内容包括文档结构、编写mapper文件、mapper.xml文件的解释说明、在mapper接口中定义方法、在mapper.xml文件中实现接口方法的SQL语句,以及如何在单元测试中测试自定义的SQL语句,并展示了测试结果。
3、Mybatis-Plus 自定义sql语句
|
JSON Java 数据安全/隐私保护
java中的http请求的封装(GET、POST、form表单、JSON形式、SIGN加密形式)
java中的http请求的封装(GET、POST、form表单、JSON形式、SIGN加密形式)
963 1
|
Java API
Java时间戳教程
本文详细介绍Java中时间戳的处理方法,包括获取当前时间戳、使用`java.time`包、时间戳与日期的相互转换及格式化等。示例代码展示了如何利用`System.currentTimeMillis()`和`java.time.Instant`获取时间戳,以及如何通过`Date`和`ZonedDateTime`进行日期转换和时区处理。随着Java 8引入的`java.time`包,日期时间操作变得更加强大和便捷,推荐在新项目中优先采用。
754 8
|
11月前
|
API
天气预报-腾讯天气-7天-地址查询版免费API接口
这是一个免费的腾讯天气API接口,用于查询指定地址的7天天气预报。支持POST和GET请求方式。请求参数包括id、key、province、city、county等。返回参数包含日期、天气状况、温度等信息。 示例请求地址:https://cn.apihz.cn/api/tianqi/tengxun.php?id=88888888&key=88888888&province=四川省&city=绵阳市&county=。
|
12月前
|
安全 关系型数据库 MySQL
Linux下安装mysql8.0(以tar.xz包安装--编译安装)
通过上述步骤,您完成了从下载、编译、安装到配置MySQL 8.0的全过程。此过程虽然较为复杂,但提供了对MySQL安装环境的完全控制,有助于满足特定的部署需求。在实际操作中,根据具体的系统环境,可能还需调整部分步骤或解决未预见的依赖问题。始终参考官方文档和社区资源,保持安装过程与最新版本的兼容性。
4214 68
|
10月前
|
Java Maven
Maven编译报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile 解决方案
在执行Maven项目中的`install`命令时,遇到编译插件版本不匹配的错误。具体报错为:`maven-compiler-plugin:3.13.0`要求Maven版本至少为3.6.3。解决方案是将Maven版本升级到3.6.3或降低插件版本。本文详细介绍了如何下载、解压并配置Maven 3.6.3,包括环境变量设置和IDEA中的Maven配置,确保项目顺利编译。
11162 5
Maven编译报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile 解决方案
|
Java 测试技术 Maven
Maven打包使用多线程加速构建过程
Maven打包使用多线程加速构建过程
2041 0
|
11月前
|
安全 Java API
【三方服务集成】最新版 | 阿里云短信服务SMS使用教程(包含支持单双参数模板的工具类,拿来即用!)
阿里云短信服务提供API/SDK和控制台调用方式,支持验证码、通知、推广等短信类型。需先注册阿里云账号并实名认证,然后在短信服务控制台申请资质、签名和模板,并创建AccessKey。最后通过Maven引入依赖,使用工具类发送短信验证码。
4670 3
【三方服务集成】最新版 | 阿里云短信服务SMS使用教程(包含支持单双参数模板的工具类,拿来即用!)