HttpClient實(shí)現(xiàn)調(diào)用外部項(xiàng)目接口工具類的示例
更新時(shí)間:2017年10月07日 10:09:06 作者:小曉峰
下面小編就為大家?guī)硪黄狧ttpClient實(shí)現(xiàn)調(diào)用外部項(xiàng)目接口工具類的示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
實(shí)例如下:
import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.HttpEntity; 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.conn.ssl.DefaultHostnameVerifier; import org.apache.http.conn.util.PublicSuffixMatcher; import org.apache.http.conn.util.PublicSuffixMatcherLoader; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class HttpUtils { private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000) .setConnectionRequestTimeout(15000).build(); public static String sendHttpGet(HttpGet httpGet) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 創(chuàng)建默認(rèn)的httpClient實(shí)例. httpClient = HttpClients.createDefault(); httpGet.setConfig(requestConfig); // 執(zhí)行請求 response = httpClient.execute(httpGet); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { // 關(guān)閉連接,釋放資源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; } /** * 發(fā)送 post請求 * @param httpUrl 地址 * @param maps 參數(shù) */ public static String sendHttpPost(String httpUrl, Map<String, String> maps) { HttpPost httpPost = new HttpPost(httpUrl);// 創(chuàng)建httpPost // 創(chuàng)建參數(shù)隊(duì)列 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); for (String key : maps.keySet()) { nameValuePairs.add(new BasicNameValuePair(key, maps.get(key))); } try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); } catch (Exception e) { e.printStackTrace(); } return sendHttpPost(httpPost); } public static String sendHttpPost(HttpPost httpPost) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 創(chuàng)建默認(rèn)的httpClient實(shí)例. httpClient = HttpClients.createDefault(); httpPost.setConfig(requestConfig); // 執(zhí)行請求 response = httpClient.execute(httpPost); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { // 關(guān)閉連接,釋放資源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; } /** * 發(fā)送Get請求Https * @param httpPost * @return */ public static String sendHttpsGet(HttpGet httpGet) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 創(chuàng)建默認(rèn)的httpClient實(shí)例. PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString())); DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher); httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build(); httpGet.setConfig(requestConfig); // 執(zhí)行請求 response = httpClient.execute(httpGet); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { // 關(guān)閉連接,釋放資源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; } }
以上這篇HttpClient實(shí)現(xiàn)調(diào)用外部項(xiàng)目接口工具類的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)郵件發(fā)送遇到的問題
本文給大家分享的是個(gè)人在項(xiàng)目過程中,使用Java實(shí)現(xiàn)郵件發(fā)送的時(shí)候所遇到的幾個(gè)問題以及解決方法,有需要的小伙伴可以參考下2016-09-09idea2023創(chuàng)建JavaWeb教程之右鍵沒有Servlet的問題解決
最近在寫一個(gè)javaweb項(xiàng)目,但是在IDEA中創(chuàng)建好項(xiàng)目后,在搭建結(jié)構(gòu)的時(shí)候創(chuàng)建servlet文件去沒有選項(xiàng),所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于idea2023創(chuàng)建JavaWeb教程之右鍵沒有Servlet問題的解決方法,需要的朋友可以參考下2023-10-10Java實(shí)現(xiàn)獲取行政區(qū)劃的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)獲取行政區(qū)劃的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)游戲2023-03-03教你一步解決java.io.FileNotFoundException:找不到文件異常
這篇文章主要給大家介紹了關(guān)于如何一步解決java.io.FileNotFoundException:找不到文件異常的相關(guān)資料,文中通過圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01