Java 调用接口工具类并设置请求和传输超时时间
生活随笔
收集整理的這篇文章主要介紹了
Java 调用接口工具类并设置请求和传输超时时间
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
廢話不多說直接上代碼
/*** 接口調(diào)用工具類* @Author: MonsterTiny* @Date: 2020-07-23 10:06*/ public class HttpClientUtils {//請求超時時間(毫秒)@Value("${http.connetTimeout}")private int connetTimeout;//傳輸超時時間(毫秒)@Value("${http.socketTimeout}")private int socketTimeout;/*** 帶參數(shù)的get請求* * @param url* @param param* @return String*/public String doGet(String url, Map<String, String> param) {// 創(chuàng)建Httpclient對象CloseableHttpClient httpclient = HttpClients.createDefault();String resultString = "";CloseableHttpResponse response = null;try {// 創(chuàng)建uriURIBuilder builder = new URIBuilder(url);if (param != null) {for (String key : param.keySet()) {builder.addParameter(key, param.get(key));}}URI uri = builder.build();// 創(chuàng)建http GET請求HttpGet httpGet = new HttpGet(uri);// 執(zhí)行請求RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connetTimeout).build();//設置請求和傳輸超時時間httpGet.setConfig(requestConfig);response = httpclient.execute(httpGet);// 判斷返回狀態(tài)是否為200if (response.getStatusLine().getStatusCode() == 200) {resultString = EntityUtils.toString(response.getEntity(), "UTF-8");}} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpclient.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}/*** 不帶參數(shù)的get請求* * @param url* @return String*/public String doGet(String url) {return doGet(url, null);}/*** 帶參數(shù)的post請求* * @param url* @param param* @return String*/public String doPost(String url, Map<String, String> param) {// 創(chuàng)建Httpclient對象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 創(chuàng)建Http Post請求HttpPost httpPost = new HttpPost(url);// 創(chuàng)建參數(shù)列表if (param != null) {List<NameValuePair> paramList = new ArrayList<>();for (String key : param.keySet()) {paramList.add(new BasicNameValuePair(key, param.get(key)));}// 模擬表單UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);httpPost.setEntity(entity);}// 執(zhí)行http請求RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connetTimeout).build();//設置請求和傳輸超時時間httpPost.setConfig(requestConfig);response = httpClient.execute(httpPost);resultString = EntityUtils.toString(response.getEntity(), "utf-8");} catch (Exception e) {e.printStackTrace();} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}/*** 不帶參數(shù)的post請求* * @param url* @return String*/public String doPost(String url) {return doPost(url, null);}/*** 傳送json類型的post請求* * @param url* @param json* @return String*/public String doPostJson(String url, String json) {// 創(chuàng)建Httpclient對象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 創(chuàng)建Http Post請求HttpPost httpPost = new HttpPost(url);// 創(chuàng)建請求內(nèi)容StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);httpPost.setEntity(entity);// 執(zhí)行http請求RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connetTimeout).build();//設置請求和傳輸超時時間httpPost.setConfig(requestConfig);response = httpClient.execute(httpPost);resultString = EntityUtils.toString(response.getEntity(), "utf-8");} catch (Exception e) {e.printStackTrace();} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;} }總結(jié)
以上是生活随笔為你收集整理的Java 调用接口工具类并设置请求和传输超时时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pagehelper 不分页几种情况的解
- 下一篇: PowerDesigner如何将物理模型