日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

Spring+Http請求+HttpClient實(shí)現(xiàn)傳參

瀏覽:28日期:2023-09-15 09:28:31

一、HttpClient簡介

HTTP 協(xié)議可能是現(xiàn)在 Internet 上使用得最多、最重要的協(xié)議了,越來越多的 Java 應(yīng)用程序需要直接通過 HTTP 協(xié)議來訪問網(wǎng)絡(luò)資源。雖然在 JDK 的 java net包中已經(jīng)提供了訪問 HTTP 協(xié)議的基本功能,但是對于大部分應(yīng)用程序來說,JDK 庫本身提供的功能還不夠豐富和靈活。HttpClient 是 Apache Jakarta Common 下的子項(xiàng)目,用來提供高效的、最新的、功能豐富的支持 HTTP 協(xié)議的客戶端編程工具包,并且它支持 HTTP 協(xié)議最新的版本和建議。HTTP和瀏覽器有點(diǎn)像,但卻不是瀏覽器。很多人覺得既然HttpClient是一個(gè)HTTP客戶端編程工具,很多人把他當(dāng)做瀏覽器來理解,但是其實(shí)HttpClient不是瀏覽器,它是一個(gè)HTTP通信庫,因此它只提供一個(gè)通用瀏覽器應(yīng)用程序所期望的功能子集,最根本的區(qū)別是HttpClient中沒有用戶界面,瀏覽器需要一個(gè)渲染引擎來顯示頁面,并解釋用戶輸入,例如鼠標(biāo)點(diǎn)擊顯示頁面上的某處,有一個(gè)布局引擎,計(jì)算如何顯示HTML頁面,包括級聯(lián)樣式表和圖像。javascript解釋器運(yùn)行嵌入HTML頁面或從HTML頁面引用的javascript代碼。來自用戶界面的事件被傳遞到j(luò)avascript解釋器進(jìn)行處理。除此之外,還有用于插件的接口,可以處理Applet,嵌入式媒體對象(如pdf文件,Quicktime電影和Flash動(dòng)畫)或ActiveX控件(可以執(zhí)行任何操作)。HttpClient只能以編程的方式通過其API用于傳輸和接受HTTP消息。

HttpClient的主要功能:

實(shí)現(xiàn)了所有 HTTP 的方法(GET、POST、PUT、HEAD、DELETE、HEAD、OPTIONS 等) 支持 HTTPS 協(xié)議 支持代理服務(wù)器(Nginx等)等 支持自動(dòng)(跳轉(zhuǎn))轉(zhuǎn)向

二、Maven依賴

<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.9</version></dependency>

三、GET無參

/** * GET---無參測試 */ @Test public void doGetTestOne() { // 獲得Http客戶端(可以理解為:你得先有一個(gè)瀏覽器;注意:實(shí)際上HttpClient與瀏覽器是不一樣的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 創(chuàng)建Get請求 HttpGet httpGet = new HttpGet('http://localhost:12345/doGetControllerOne'); // 響應(yīng)模型 CloseableHttpResponse response = null; try { // 由客戶端執(zhí)行(發(fā)送)Get請求 response = httpClient.execute(httpGet); // 從響應(yīng)模型中獲取響應(yīng)實(shí)體 HttpEntity responseEntity = response.getEntity(); System.out.println('響應(yīng)狀態(tài)為:' + response.getStatusLine()); if (responseEntity != null) {System.out.println('響應(yīng)內(nèi)容長度為:' + responseEntity.getContentLength());System.out.println('響應(yīng)內(nèi)容為:' + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try {// 釋放資源if (httpClient != null) { httpClient.close();}if (response != null) { response.close();} } catch (IOException e) {e.printStackTrace(); } } }

四、GET有參

拼接

/** * GET---有參測試 (方式一:手動(dòng)在url后面加上參數(shù)) */ @Test public void doGetTestWayOne() { // 獲得Http客戶端(可以理解為:你得先有一個(gè)瀏覽器;注意:實(shí)際上HttpClient與瀏覽器是不一樣的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 參數(shù) StringBuffer params = new StringBuffer(); try { // 字符數(shù)據(jù)最好encoding以下;這樣一來,某些特殊字符才能傳過去(如:某人的名字就是“&”,不encoding的話,傳不過去) params.append('name=' + URLEncoder.encode('&', 'utf-8')); params.append('&'); params.append('age=24'); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } // 創(chuàng)建Get請求 HttpGet httpGet = new HttpGet('http://localhost:12345/doGetControllerTwo' + '?' + params); // 響應(yīng)模型 CloseableHttpResponse response = null; try { // 配置信息 RequestConfig requestConfig = RequestConfig.custom() // 設(shè)置連接超時(shí)時(shí)間(單位毫秒) .setConnectTimeout(5000) // 設(shè)置請求超時(shí)時(shí)間(單位毫秒) .setConnectionRequestTimeout(5000) // socket讀寫超時(shí)時(shí)間(單位毫秒) .setSocketTimeout(5000) // 設(shè)置是否允許重定向(默認(rèn)為true) .setRedirectsEnabled(true).build(); // 將上面的配置信息 運(yùn)用到這個(gè)Get請求里 httpGet.setConfig(requestConfig); // 由客戶端執(zhí)行(發(fā)送)Get請求 response = httpClient.execute(httpGet); // 從響應(yīng)模型中獲取響應(yīng)實(shí)體 HttpEntity responseEntity = response.getEntity(); System.out.println('響應(yīng)狀態(tài)為:' + response.getStatusLine()); if (responseEntity != null) {System.out.println('響應(yīng)內(nèi)容長度為:' + responseEntity.getContentLength());System.out.println('響應(yīng)內(nèi)容為:' + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try {// 釋放資源if (httpClient != null) { httpClient.close();}if (response != null) { response.close();} } catch (IOException e) {e.printStackTrace(); } } }

URI獲得HttpGet

/** * GET---有參測試 (方式二:將參數(shù)放入鍵值對類中,再放入U(xiǎn)RI中,從而通過URI得到HttpGet實(shí)例) */ @Test public void doGetTestWayTwo() { // 獲得Http客戶端(可以理解為:你得先有一個(gè)瀏覽器;注意:實(shí)際上HttpClient與瀏覽器是不一樣的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 參數(shù) URI uri = null; try { // 將參數(shù)放入鍵值對類NameValuePair中,再放入集合中 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair('name', '&')); params.add(new BasicNameValuePair('age', '18')); // 設(shè)置uri信息,并將參數(shù)集合放入uri; // 注:這里也支持一個(gè)鍵值對一個(gè)鍵值對地往里面放setParameter(String key, String value) uri = new URIBuilder().setScheme('http').setHost('localhost') .setPort(12345).setPath('/doGetControllerTwo') .setParameters(params).build(); } catch (URISyntaxException e1) { e1.printStackTrace(); } // 創(chuàng)建Get請求 HttpGet httpGet = new HttpGet(uri); // 響應(yīng)模型 CloseableHttpResponse response = null; try { // 配置信息 RequestConfig requestConfig = RequestConfig.custom() // 設(shè)置連接超時(shí)時(shí)間(單位毫秒) .setConnectTimeout(5000) // 設(shè)置請求超時(shí)時(shí)間(單位毫秒) .setConnectionRequestTimeout(5000) // socket讀寫超時(shí)時(shí)間(單位毫秒) .setSocketTimeout(5000) // 設(shè)置是否允許重定向(默認(rèn)為true) .setRedirectsEnabled(true).build(); // 將上面的配置信息 運(yùn)用到這個(gè)Get請求里 httpGet.setConfig(requestConfig); // 由客戶端執(zhí)行(發(fā)送)Get請求 response = httpClient.execute(httpGet); // 從響應(yīng)模型中獲取響應(yīng)實(shí)體 HttpEntity responseEntity = response.getEntity(); System.out.println('響應(yīng)狀態(tài)為:' + response.getStatusLine()); if (responseEntity != null) {System.out.println('響應(yīng)內(nèi)容長度為:' + responseEntity.getContentLength());System.out.println('響應(yīng)內(nèi)容為:' + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try {// 釋放資源if (httpClient != null) { httpClient.close();}if (response != null) { response.close();} } catch (IOException e) {e.printStackTrace(); } } }

五、POST無參

/** * POST---無參測試 */ @Test public void doPostTestOne() { // 獲得Http客戶端(可以理解為:你得先有一個(gè)瀏覽器;注意:實(shí)際上HttpClient與瀏覽器是不一樣的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 創(chuàng)建Post請求 HttpPost httpPost = new HttpPost('http://localhost:12345/doPostControllerOne'); // 響應(yīng)模型 CloseableHttpResponse response = null; try { // 由客戶端執(zhí)行(發(fā)送)Post請求 response = httpClient.execute(httpPost); // 從響應(yīng)模型中獲取響應(yīng)實(shí)體 HttpEntity responseEntity = response.getEntity(); System.out.println('響應(yīng)狀態(tài)為:' + response.getStatusLine()); if (responseEntity != null) {System.out.println('響應(yīng)內(nèi)容長度為:' + responseEntity.getContentLength());System.out.println('響應(yīng)內(nèi)容為:' + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try {// 釋放資源if (httpClient != null) { httpClient.close();}if (response != null) { response.close();} } catch (IOException e) {e.printStackTrace(); } } }

六、POST有參(普通參數(shù))

注:POST傳遞普通參數(shù)時(shí),方式與GET一樣即可,這里以直接在url后綴上參數(shù)的方式示例。

/** * POST---有參測試(普通參數(shù)) */ @Test public void doPostTestFour() { // 獲得Http客戶端(可以理解為:你得先有一個(gè)瀏覽器;注意:實(shí)際上HttpClient與瀏覽器是不一樣的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 參數(shù) StringBuffer params = new StringBuffer(); try { // 字符數(shù)據(jù)最好encoding以下;這樣一來,某些特殊字符才能傳過去(如:某人的名字就是“&”,不encoding的話,傳不過去) params.append('name=' + URLEncoder.encode('&', 'utf-8')); params.append('&'); params.append('age=24'); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } // 創(chuàng)建Post請求 HttpPost httpPost = new HttpPost('http://localhost:12345/doPostControllerFour' + '?' + params); // 設(shè)置ContentType(注:如果只是傳普通參數(shù)的話,ContentType不一定非要用application/json) httpPost.setHeader('Content-Type', 'application/json;charset=utf8'); // 響應(yīng)模型 CloseableHttpResponse response = null; try { // 由客戶端執(zhí)行(發(fā)送)Post請求 response = httpClient.execute(httpPost); // 從響應(yīng)模型中獲取響應(yīng)實(shí)體 HttpEntity responseEntity = response.getEntity(); System.out.println('響應(yīng)狀態(tài)為:' + response.getStatusLine()); if (responseEntity != null) {System.out.println('響應(yīng)內(nèi)容長度為:' + responseEntity.getContentLength());System.out.println('響應(yīng)內(nèi)容為:' + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try {// 釋放資源if (httpClient != null) { httpClient.close();}if (response != null) { response.close();} } catch (IOException e) {e.printStackTrace(); } } }

七、POST有參(對象參數(shù))

/** * POST---有參測試(對象參數(shù)) */ @Test public void doPostTestTwo() { // 獲得Http客戶端(可以理解為:你得先有一個(gè)瀏覽器;注意:實(shí)際上HttpClient與瀏覽器是不一樣的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 創(chuàng)建Post請求 HttpPost httpPost = new HttpPost('http://localhost:12345/doPostControllerTwo'); User user = new User(); user.setName('潘曉婷'); user.setAge(18); user.setGender('女'); user.setMotto('姿勢要優(yōu)雅~'); // 我這里利用阿里的fastjson,將Object轉(zhuǎn)換為json字符串; // (需要導(dǎo)入com.alibaba.fastjson.JSON包) String jsonString = JSON.toJSONString(user); StringEntity entity = new StringEntity(jsonString, 'UTF-8'); // post請求是將參數(shù)放在請求體里面?zhèn)鬟^去的;這里將entity放入post請求體中 httpPost.setEntity(entity); httpPost.setHeader('Content-Type', 'application/json;charset=utf8'); // 響應(yīng)模型 CloseableHttpResponse response = null; try { // 由客戶端執(zhí)行(發(fā)送)Post請求 response = httpClient.execute(httpPost); // 從響應(yīng)模型中獲取響應(yīng)實(shí)體 HttpEntity responseEntity = response.getEntity(); System.out.println('響應(yīng)狀態(tài)為:' + response.getStatusLine()); if (responseEntity != null) {System.out.println('響應(yīng)內(nèi)容長度為:' + responseEntity.getContentLength());System.out.println('響應(yīng)內(nèi)容為:' + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try {// 釋放資源if (httpClient != null) { httpClient.close();}if (response != null) { response.close();} } catch (IOException e) {e.printStackTrace(); } } }

八、POST有參(普通參數(shù) + 對象參數(shù))

注:POST傳遞普通參數(shù)時(shí),方式與GET一樣即可,這里以通過URI獲得HttpPost的方式為例。

/** * POST---有參測試(普通參數(shù) + 對象參數(shù)) */ @Test public void doPostTestThree() { // 獲得Http客戶端(可以理解為:你得先有一個(gè)瀏覽器;注意:實(shí)際上HttpClient與瀏覽器是不一樣的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 創(chuàng)建Post請求 // 參數(shù) URI uri = null; try { // 將參數(shù)放入鍵值對類NameValuePair中,再放入集合中 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair('flag', '4')); params.add(new BasicNameValuePair('meaning', '這是什么鬼?')); // 設(shè)置uri信息,并將參數(shù)集合放入uri; // 注:這里也支持一個(gè)鍵值對一個(gè)鍵值對地往里面放setParameter(String key, String value) uri = new URIBuilder().setScheme('http').setHost('localhost').setPort(12345) .setPath('/doPostControllerThree').setParameters(params).build(); } catch (URISyntaxException e1) { e1.printStackTrace(); } HttpPost httpPost = new HttpPost(uri); // HttpPost httpPost = new // HttpPost('http://localhost:12345/doPostControllerThree1'); // 創(chuàng)建user參數(shù) User user = new User(); user.setName('潘曉婷'); user.setAge(18); user.setGender('女'); user.setMotto('姿勢要優(yōu)雅~'); // 將user對象轉(zhuǎn)換為json字符串,并放入entity中 StringEntity entity = new StringEntity(JSON.toJSONString(user), 'UTF-8'); // post請求是將參數(shù)放在請求體里面?zhèn)鬟^去的;這里將entity放入post請求體中 httpPost.setEntity(entity); httpPost.setHeader('Content-Type', 'application/json;charset=utf8'); // 響應(yīng)模型 CloseableHttpResponse response = null; try { // 由客戶端執(zhí)行(發(fā)送)Post請求 response = httpClient.execute(httpPost); // 從響應(yīng)模型中獲取響應(yīng)實(shí)體 HttpEntity responseEntity = response.getEntity(); System.out.println('響應(yīng)狀態(tài)為:' + response.getStatusLine()); if (responseEntity != null) {System.out.println('響應(yīng)內(nèi)容長度為:' + responseEntity.getContentLength());System.out.println('響應(yīng)內(nèi)容為:' + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try {// 釋放資源if (httpClient != null) { httpClient.close();}if (response != null) { response.close();} } catch (IOException e) {e.printStackTrace(); } } }

到此這篇關(guān)于Spring+Http請求+HttpClient實(shí)現(xiàn)傳參的文章就介紹到這了,更多相關(guān)Spring+Http請求+HttpClient內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
91精品福利观看| 欧美精品高清| 午夜电影亚洲| 91精品电影| 日韩中文字幕亚洲一区二区va在线| 亚洲中字黄色| 一级成人国产| 欧美日韩视频免费看| 免费在线日韩av| 国产一区丝袜| 精品国产a一区二区三区v免费| 98精品久久久久久久| 日本а中文在线天堂| 影视先锋久久| 深夜福利一区| 久久精品天堂| 色综合www| 日韩视频免费| 蜜桃久久精品一区二区| 欧美天堂一区| 高清不卡一区| 在线视频精品| 青草综合视频| 精品一区二区三区视频在线播放| 日韩在线精品| 日韩视频在线一区二区三区 | 日日夜夜免费精品视频| 国产日韩精品视频一区二区三区| 高清久久一区| 久久av在线| 国产精品一区二区av日韩在线| 91免费精品| 免费观看在线色综合| 国产精品sss在线观看av| 99久久99久久精品国产片果冰 | 婷婷综合五月| 日韩av中文字幕一区| 国产一区二区三区四区| 美女精品一区| 97国产成人高清在线观看| 亚洲欧美日韩国产一区二区| 国产精品一二| 午夜国产精品视频| 免费在线日韩av| 首页国产欧美日韩丝袜| 黄色网一区二区| 亚洲视频播放| 欧美日韩精品一区二区三区在线观看| 精品国产第一福利网站| 蜜桃免费网站一区二区三区| 91一区二区三区四区| 亚洲精品视频一二三区| 国产精品精品国产一区二区| 蜜臀久久久99精品久久久久久| 国产精品99视频| 中文一区一区三区免费在线观 | 国产日韩欧美一区二区三区在线观看| 黑人精品一区| 日韩一区二区三区免费视频| 成人精品视频| 日韩精品中文字幕一区二区| 亚洲天堂1区| 欧美日韩一视频区二区| 99在线观看免费视频精品观看| 精品国产黄a∨片高清在线| 美女被久久久| 91精品国产自产在线观看永久∴ | 9999国产精品| 国产欧美日韩精品一区二区免费| 国产精品91一区二区三区| 精品国产中文字幕第一页| 亚洲精品韩国| 好看的av在线不卡观看| 中文字幕在线视频久| 日韩高清不卡在线| 91久久亚洲| 激情综合激情| 日韩在线观看| 97视频热人人精品免费| 欧美a在线观看| 国产私拍福利精品视频二区| 亚洲日本欧美| 欧美综合二区| 天堂成人国产精品一区| 午夜日韩在线| 午夜久久久久| 欧美日韩视频| 亚洲欧洲日本mm| 亚洲黄页一区| 国产成人精品一区二区三区免费| 欧美黄色精品| 精品伊人久久久| 国产一区二区三区探花| 精品视频国产| 精品91福利视频| 国产成人精品福利| 国产精品毛片久久| 黄毛片在线观看| 日韩综合一区| 国产成人精选| 一本大道色婷婷在线| 91av亚洲| 99久久视频| 狠狠色狠狠色综合日日tαg| 亚洲h色精品| 不卡在线一区| 亚洲一区二区三区高清不卡| 视频一区中文字幕| 欧美中文日韩| 日韩高清一区在线| 欧美午夜网站| 精品亚洲成人| 久久久水蜜桃av免费网站| 久久中文亚洲字幕| 国产模特精品视频久久久久| 免费视频最近日韩| 日韩二区三区在线观看| 国产精品亚洲欧美一级在线| 久久精品国产99国产| 国产不卡一区| 99精品在线观看| 视频在线观看91| 国产情侣一区| 岛国av在线网站| 蜜桃tv一区二区三区| 色爱综合网欧美| 红杏一区二区三区| 日韩伦理一区| 亚洲欧美激情诱惑| 91精品国产自产观看在线| 老鸭窝一区二区久久精品| 亚洲伦乱视频| 蜜桃免费网站一区二区三区 | 精品国产91| 欧美91视频| 日韩av一区二区在线影视| 国产欧美亚洲精品a| 色婷婷色综合| 美女久久一区| 麻豆精品蜜桃视频网站| 欧美成人国产| 国产日韩欧美三级| 日韩欧美一区二区三区在线观看| 视频一区在线播放| 麻豆精品在线| 亚洲一区二区三区高清不卡| 国产精品一国产精品k频道56| 在线一区视频观看| 欧美日韩国产在线观看网站 | 日韩中文影院| 日本三级亚洲精品| 久久青青视频| 日韩欧美高清一区二区三区| 日韩在线看片| 青青国产精品| 日韩精品一区二区三区免费观影 | 欧美一区成人| 亚洲精品一级二级| 国产亚洲高清一区| 久久网站免费观看| 国产日产一区| 日韩天堂av| 国产a亚洲精品| 婷婷精品久久久久久久久久不卡| 日本少妇一区| 欧美黄色一区| 视频一区视频二区中文字幕| 精品免费av一区二区三区| 99亚洲视频| 亚洲精品福利电影| 国产农村妇女精品一二区| 狂野欧美性猛交xxxx| 狠狠久久婷婷| 神马久久午夜| 国产精品v日韩精品v欧美精品网站 | 蜜臀久久精品| 久久国产精品色av免费看| 日韩亚洲在线| 亚洲女同av| 毛片不卡一区二区| 亚洲3区在线| 羞羞答答国产精品www一本 | 亚洲精选成人| 狠狠操综合网| 日韩高清中文字幕一区二区| 蜜桃久久久久| 国产探花在线精品| 视频一区中文字幕精品 | 乱一区二区av| 777久久精品| 婷婷五月色综合香五月| 91久久黄色| 亚洲天堂黄色| 国产网站在线| 国产精品videossex久久发布| 免费在线观看视频一区| 九九综合在线| 免费欧美一区| 欧美 日韩 国产一区二区在线视频 | 国产精品免费99久久久|