本篇文章給大家分享的是有關如何正確的使用HttpClient方法,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
1.簡介
HttpClient是Apache Jakarta Common下的子項目,用來提供高效的、新的、功能豐富的支持HTTP協(xié)議的客戶端編程工具包,并且它支持HTTP協(xié)議新的版本和建議。HttpClient已經(jīng)應用在很多的項目中,比如Apache Jakarta上很著名的另外兩個開源項目Cactus和HTMLUnit都使用了HttpClient。
HttpClient相比傳統(tǒng)JDK自帶的URLConnection,增加了易用性和靈活性,它不僅使客戶端發(fā)送Http請求變得容易,而且也方便開發(fā)人員測試接口(基于Http協(xié)議的),提高了開發(fā)的效率,也方便提高代碼的健壯性。
基于標準、純凈的java語言。實現(xiàn)了Http1.0和Http1.1
以可擴展的面向?qū)ο蟮慕Y(jié)構實現(xiàn)了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。
支持HTTPS協(xié)議。
通過Http代理建立透明的連接。
利用CONNECT方法通過Http代理建立隧道的https連接。
Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos認證方案。
插件式的自定義認證方案。
便攜可靠的套接字工廠使它更容易的使用第三方解決方案。
連接管理器支持多線程應用。支持設置較大連接數(shù),同時支持設置每個主機的較大連接數(shù),發(fā)現(xiàn)并關閉過期的連接。
自動處理Set-Cookie中的Cookie。
插件式的自定義Cookie策略。
Request的輸出流可以避免流中內(nèi)容直接緩沖到socket服務器。
Response的輸入流可以有效的從socket服務器直接讀取相應內(nèi)容。
在http1.0和http1.1中利用KeepAlive保持持久連接。
直接獲取服務器發(fā)送的response code和 headers。
設置連接超時的能力。
實驗性的支持http1.1 response caching。
源代碼基于Apache License 可免費獲取。
創(chuàng)建HttpClient對象。
創(chuàng)建請求方法的實例,并指定請求URL。如果需要發(fā)送GET請求,創(chuàng)建HttpGet對象;如果需要發(fā)送POST請求,創(chuàng)建HttpPost對象。
如果需要發(fā)送請求參數(shù),可調(diào)用HttpGet、HttpPost共同的setParams(HttpParams params)方法來添加請求參數(shù);對于HttpPost對象而言,也可調(diào)用setEntity(HttpEntity entity)方法來設置請求參數(shù)。
調(diào)用HttpClient對象的execute(HttpUriRequest request)發(fā)送請求,該方法返回一個HttpResponse。
調(diào)用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭;調(diào)用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器的響應內(nèi)容。程序可通過該對象獲取服務器的響應內(nèi)容。
釋放連接。無論執(zhí)行方法是否成功,都必須釋放連接
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wo</groupId> <artifactId>HttpClient_test</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencies> </project>
@RequestMapping("findAll") public String findAll() throws Exception{ //獲得Http客戶端 CloseableHttpClient build = HttpClientBuilder.create().build(); //創(chuàng)建get請求 HttpGet httpGet = new HttpGet("http://localhost:8088/lunbo/findAll"); //執(zhí)行請求 CloseableHttpResponse execute = build.execute(httpGet); //解析返回值 StatusLine statusLine = execute.getStatusLine(); //獲取到返回狀態(tài)碼 System.out.println("狀態(tài)碼為:"+statusLine.getStatusCode()); String s = EntityUtils.toString(execute.getEntity()); build.close(); execute.close(); return s; }
//post路徑傳參 @RequestMapping("/findAllPost/{page}/{size}") public String findAll(@PathVariable("page") int page,@PathVariable("size") int size) throws Exception { //獲得Http客戶端 CloseableHttpClient build = HttpClientBuilder.create().build(); //創(chuàng)建post請求 HttpPost httpPost = new HttpPost("http://localhost:8088/position/findAll/"+page+"/"+size); //執(zhí)行請求 CloseableHttpResponse execute = build.execute(httpPost); //解析返回值 StatusLine statusLine = execute.getStatusLine(); //獲取到返回狀態(tài)碼 System.out.println("狀態(tài)碼為:"+statusLine.getStatusCode()); String s = EntityUtils.toString(execute.getEntity()); build.close(); execute.close(); return s; } //post map傳參 @RequestMapping("findById") public String findById(@RequestParam("id") Integer id)throws Exception{ //創(chuàng)建httpclicent請求對象 CloseableHttpClient build = HttpClientBuilder.create().build(); //聲明請求方式 HttpPost httpPost = new HttpPost("http://localhost:8088/position/findById"); //聲明攜帶參數(shù) Map map=new HashMap<>(); map.put("id",id); //將map轉(zhuǎn)換為json格式 Object o = JSONObject.toJSON(map); //設置請求 參數(shù)的編碼格式 StringEntity stringEntity = new StringEntity(o.toString(), "utf-8"); //將參數(shù)設置到請求對象中 httpPost.setEntity(stringEntity); //設置content-Type httpPost.setHeader("Content-Type","application/json"); //執(zhí)行請求 CloseableHttpResponse execute = build.execute(httpPost); //解析返回值 StatusLine statusLine = execute.getStatusLine(); //獲取到返回狀態(tài)碼 System.out.println("狀態(tài)碼為:"+statusLine.getStatusCode()); String s = EntityUtils.toString(execute.getEntity()); build.close(); execute.close(); return s; }
以上就是如何正確的使用HttpClient方法,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站題目:如何正確的使用HttpClient方法-創(chuàng)新互聯(lián)
當前地址:http://chinadenli.net/article38/ddgesp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、網(wǎng)站設計、App設計、商城網(wǎng)站、營銷型網(wǎng)站建設、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容