合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
HttpClient4.x工具获取使用以及Get和Post模拟请求类编写 方便我们后续进行模拟请求 首先加入依赖 ### ~~~ <!-- httpClient4.x相关依赖 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </dependency> <!-- gson工具,封装http的时候使用 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> ~~~ ### 我们再来封装工具类 利用httpclient去做get和post的模拟请求 ### ~~~ package net.xdclass.xdvideo.utils; import com.google.gson.Gson; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.config.RequestConfig; 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.HttpClients; import org.apache.http.util.EntityUtils; import java.util.HashMap; import java.util.Map; /* * 封装http get post * */ public class HttpUtils { private static final Gson gson = new Gson(); /* * get方法 * @param url * @return * */ public static Map<String,Object> doGet(String url){ Map<String,Object> map = new HashMap<>(); CloseableHttpClient httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000) //连接超时 .setConnectionRequestTimeout(5000)//请求超时 .setSocketTimeout(5000) .setRedirectsEnabled(true) //允许自动重定向 .build(); HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); try{ HttpResponse httpResponse = httpClient.execute(httpGet); if(httpResponse.getStatusLine().getStatusCode() == 200){ //在Java中,EntityUtils.toString(HttpEntity entity) 是 Apache HttpClient 库提供的一个实用方法,用于将 HttpEntity 对象转换为字符串 String jsonResult = EntityUtils.toString( httpResponse.getEntity()); map = gson.fromJson(jsonResult,map.getClass()); } }catch (Exception e){ e.printStackTrace(); }finally { try { httpClient.close(); }catch (Exception e){ e.printStackTrace(); } } return map; } /** * 封装post * @return */ public static String doPost(String url, String data,int timeout){ CloseableHttpClient httpClient = HttpClients.createDefault(); //超时设置 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout) //连接超时 .setConnectionRequestTimeout(timeout)//请求超时 .setSocketTimeout(timeout) .setRedirectsEnabled(true) //允许自动重定向 .build(); HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); httpPost.addHeader("Content-Type","text/html; chartset=UTF-8"); if(data != null && data instanceof String){ //使用字符串传参 StringEntity stringEntity = new StringEntity(data,"UTF-8"); httpPost.setEntity(stringEntity); } try{ CloseableHttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); if(httpResponse.getStatusLine().getStatusCode() == 200){ String result = EntityUtils.toString(httpEntity); return result; } }catch (Exception e){ e.printStackTrace(); }finally { try{ httpClient.close(); }catch (Exception e){ e.printStackTrace(); } } return null; } } ~~~ ### ![](https://img.kancloud.cn/62/f4/62f4c975edb1b6e3d3385f89b6e02692_2248x1686.png) ###