博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
httpClientUtil
阅读量:5338 次
发布时间:2019-06-15

本文共 4203 字,大约阅读时间需要 14 分钟。

1.依赖jar

org.apache.httpcomponents
httpclient
4.5.3

2.代码

  

1 package com.hshc.util; 2  3 import com.hshc.common.constant.QueryAttrConstant; 4 import com.hshc.web.ucp.model.vo.HttpClientVo; 5 import lombok.extern.slf4j.Slf4j; 6 import org.apache.commons.lang3.StringUtils; 7 import org.apache.http.HttpEntity; 8 import org.apache.http.HttpResponse; 9 import org.apache.http.client.config.RequestConfig;10 import org.apache.http.client.methods.HttpPost;11 import org.apache.http.entity.StringEntity;12 import org.apache.http.impl.client.CloseableHttpClient;13 import org.apache.http.impl.client.HttpClients;14 import org.apache.http.util.EntityUtils;15 16 import java.io.IOException;17 18 /**19  * @author ldg20  * @Description: HttpClient工具类21  * @date 2018/9/29 上午10:3122  */23 @Slf4j24 public class HttpClientUtil {25 26     private static  final String APPID = PropertiesUtil.getProperty("detection.query.appId");27 28     private static final int RES_FAIL_CODE = 500; //失败响应码29     /**30    *post35      */36     public static  HttpClientVo postHttp(String url,String sign,String para){37         //创建httpClient对象38         CloseableHttpClient httpCilent = HttpClients.createDefault();39 40         //创建http配置对象41         RequestConfig requestConfig = RequestConfig.custom()42                 .setConnectTimeout(10000)   //连接目标超时43                 .setConnectionRequestTimeout(10000) // 从连接池获取连接超时44                 .setSocketTimeout(10000) //等待响应超时45                 .setRedirectsEnabled(true)//默认允许自动重定向46                 .build();47 48         //创建post请求对象49         HttpPost httpPost = new HttpPost(url);50         httpPost.setConfig(requestConfig);51         httpPost.addHeader("APPID",APPID );52         httpPost.addHeader("SIGN",sign);53 54         if(!StringUtils.isEmpty(para)){55 56         StringEntity stringEntity = new StringEntity(para, "UTF-8");//解决中文乱码问题57         stringEntity.setContentEncoding("UTF-8");58         stringEntity.setContentType("application/json");59         httpPost.setEntity(stringEntity);60         }61 62         httpPost.addHeader("Content-type", "application/json; charset=utf-8");63 64         //返回结果对象65         HttpClientVo httpClientVo = HttpClientVo.getHttpClientVo();66         //处理返回结果67         try {68             HttpResponse httpResponse = httpCilent.execute(httpPost);69 70             if(httpResponse.getStatusLine().getStatusCode() == QueryAttrConstant.HTTP_SUCCESS_CODE){71 72                 httpClientVo.setCode(QueryAttrConstant.HTTP_SUCCESS_CODE);73                 httpClientVo.setRusultStr(EntityUtils.toString(httpResponse.getEntity()));//获得返回的结果74 75             }else{76 77                 //第三方响应失败78                 httpClientVo.setCode(RES_FAIL_CODE);79                 log.error("http请求第三方响应失败");80 81             }82         } catch (IOException e) {83             httpClientVo.setCode(RES_FAIL_CODE);84             log.error("HttpClientUtil-getRulesbyHttp连接时异常",e);85         }finally {86             try {87                 //关闭链接88                 httpCilent.close();89             } catch (IOException e) {90                 log.error("HttpClientUtil-getRulesbyHttp关闭连接时异常",e);91             }92         }93 94         return httpClientVo;95     }96 97 //get
private String getHttpGetData(String url){
log.info("car300 url=[{}]",url); String result = StringUtils.EMPTY; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); httpGet.addHeader("kdj-token", token); httpGet.setConfig(RequestConfig.custom().setConnectTimeout(3000).build()); try {
CloseableHttpResponse response = httpClient.execute(httpGet); int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK){
HttpEntity responseEntity = response.getEntity(); result = EntityUtils.toString(responseEntity, "UTF-8"); } } catch (Exception e) {
log.error("car300 url=[{}], is error",url, e); } log.info("car300 url=[{}], result={}",url,result); return result; }
98 99 }

 

3.调用

  

 
 

转载于:https://www.cnblogs.com/beixiaoyi/p/11150841.html

你可能感兴趣的文章
Kafka TimeoutException: Batch Expired 问题排查
查看>>
RabbitMq(三)交换机类型
查看>>
基本矩张量与strike.dip.rake的对应
查看>>
Android EditText常用属性
查看>>
OpenCV training program, part 1: Official OpenCV Tutorial in C++
查看>>
pyCharm django 中新加app
查看>>
接口测试总结
查看>>
luogu 电车
查看>>
vijos 拓扑编号
查看>>
前端面试题目
查看>>
404. Sum of Left Leaves
查看>>
大小端以及字节序的问题
查看>>
[Leetcode 216]求给定和的数集合 Combination Sum III
查看>>
助教小结1
查看>>
[NOI2009]二叉查找树
查看>>
ASP.NET 配置文件加密
查看>>
JAVA设计模式之适配器模式
查看>>
P2672 推销员
查看>>
二分法查找
查看>>
全面解析Java注解
查看>>