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.调用