import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; 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.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; @Slf4j public final class HttpUtil { private static final int TIMEOUT_NUMBER = 6000; private HttpUtil() { } /** * post method * * @param url url * @param headers headers * @param body body * @return response */ public static String doPost(String url, java.util.Map<String, String> headers, String body) { CloseableHttpClient client = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(url); for (java.util.Map.Entry<String, String> entry : headers.entrySet()) { httpPost.addHeader(entry.getKey(), entry.getValue()); } httpPost.setEntity(new StringEntity(body, "utf-8")); //3.设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig .custom() .setSocketTimeout(TIMEOUT_NUMBER) .setConnectTimeout(TIMEOUT_NUMBER) .build(); httpPost.setConfig(requestConfig); try { CloseableHttpResponse response = client.execute(httpPost); HttpEntity entity = response.getEntity(); if (null != entity) { return EntityUtils.toString(entity, "utf-8"); } } catch (Exception e) { log.error("异常:" + e); } return null; } /** * get 请求 * * @return */ public static String doHttpGet(String url, List<NameValuePair> params) { String result = null; //1.获取httpclient CloseableHttpClient httpClient = HttpClients.createDefault(); //接口返回结果 CloseableHttpResponse response = null; String paramStr = null; try { paramStr = EntityUtils.toString(new UrlEncodedFormEntity(params)); //拼接参数 StringBuilder sb = new StringBuilder(); sb.append(url); sb.append("?"); sb.append(paramStr); //2.创建get请求 HttpGet httpGet = new HttpGet(sb.toString()); //3.设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT_NUMBER).setConnectTimeout(TIMEOUT_NUMBER).build(); httpGet.setConfig(requestConfig); /*此处可以添加一些请求头信息,例如: httpGet.addHeader("content-type","text/xml");*/ //4.提交参数 response = httpClient.execute(httpGet); //5.得到响应信息 int statusCode = response.getStatusLine().getStatusCode(); //6.判断响应信息是否正确 if (HttpStatus.SC_OK != statusCode) { //终止并抛出异常 httpGet.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } //7.转换成实体类 HttpEntity entity = response.getEntity(); if (null != entity) { result = EntityUtils.toString(entity); } EntityUtils.consume(entity); } catch (UnsupportedEncodingException e) { log.error("非支持编码异常:" + e); } catch (ClientProtocolException e) { log.error("客户端协议异常:" + e); } catch (IOException e) { log.error("IO异常:" + e); } finally { //8.关闭所有资源连接 if (null != response) { try { response.close(); } catch (IOException e) { log.error("IO异常:" + e); } } if (null != httpClient) { try { httpClient.close(); } catch (IOException e) { log.error("IO异常:" + e); } } } return result; } /** * http post 请求 */ public static String doHttpPost(String url, List<NameValuePair> params) { String result = null; //1. 获取httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; try { //2. 创建post请求 HttpPost httpPost = new HttpPost(url); //3.设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig .custom() .setSocketTimeout(TIMEOUT_NUMBER) .setConnectTimeout(TIMEOUT_NUMBER) .build(); httpPost.setConfig(requestConfig); //4.提交参数发送请求 UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params); //此处可以设置传输时的编码格式,和数据格式 urlEncodedFormEntity.setContentEncoding("UTF-8"); urlEncodedFormEntity.setContentType("application/json"); httpPost.setEntity(urlEncodedFormEntity); response = httpClient.execute(httpPost); //5.得到响应信息 int statusCode = response.getStatusLine().getStatusCode(); //6. 判断响应信息是否正确 if (HttpStatus.SC_OK != statusCode) { //结束请求并抛出异常 httpPost.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } //7. 转换成实体类 HttpEntity entity = response.getEntity(); if (null != entity) { result = EntityUtils.toString(entity, "UTF-8"); } EntityUtils.consume(entity); } catch (UnsupportedEncodingException e) { log.error("非支持编码异常:" + e); } catch (ClientProtocolException e) { log.error("客户端协议异常:" + e); } catch (IOException e) { log.error("IO异常:" + e); } finally { //8. 关闭所有资源连接 if (null != response) { try { response.close(); } catch (IOException e) { log.error("IO异常:" + e); } } if (null != httpClient) { try { httpClient.close(); } catch (IOException e) { log.error("IO异常:" + e); } } } return result; } }
调用第三方接口工具类(dopost,doget)
最新推荐文章于 2024-03-28 10:29:06 发布