日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 调用接口工具类并设置请求和传输超时时间

發(fā)布時間:2024/10/6 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。