<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

        HttpClient4.2FluentAPI學(xué)習(xí)

        來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-09 07:56:20
        文檔

        HttpClient4.2FluentAPI學(xué)習(xí)

        HttpClient4.2FluentAPI學(xué)習(xí):相比于HttpClient 之前的版本,HttpClient 4.2 提供了一組基于流接口(fluent interface)概念的更易使用的API,即Fluent API. 為了方便使用,F(xiàn)luent API只暴露了一些最基本的HttpClient功能。這樣,F(xiàn)luent API就將開發(fā)者從連接管理、資源釋放等
        推薦度:
        導(dǎo)讀HttpClient4.2FluentAPI學(xué)習(xí):相比于HttpClient 之前的版本,HttpClient 4.2 提供了一組基于流接口(fluent interface)概念的更易使用的API,即Fluent API. 為了方便使用,F(xiàn)luent API只暴露了一些最基本的HttpClient功能。這樣,F(xiàn)luent API就將開發(fā)者從連接管理、資源釋放等

        相比于HttpClient 之前的版本,HttpClient 4.2 提供了一組基于流接口(fluent interface)概念的更易使用的API,即Fluent API. 為了方便使用,F(xiàn)luent API只暴露了一些最基本的HttpClient功能。這樣,F(xiàn)luent API就將開發(fā)者從連接管理、資源釋放等繁雜的操作中解

        相比于HttpClient 之前的版本,HttpClient 4.2 提供了一組基于流接口(fluent interface)概念的更易使用的API,即Fluent API.

        為了方便使用,F(xiàn)luent API只暴露了一些最基本的HttpClient功能。這樣,F(xiàn)luent API就將開發(fā)者從連接管理、資源釋放等繁雜的操作中解放出來,從而更易進(jìn)行一些HttpClient的簡單操作。

        http://blog.csdn.net/vector_yi/article/details/24298629

        還是利用具體例子來說明吧。

        以下是幾個(gè)使用Fluent API的代碼樣例:

        一、最基本的http請(qǐng)求功能

        執(zhí)行Get、Post請(qǐng)求,不對(duì)返回的響應(yīng)作處理

        package com.vectoryi.fluent;
        
        import java.io.File;
        
        import org.apache.http.HttpHost;
        import org.apache.http.HttpVersion;
        import org.apache.http.client.fluent.Form;
        import org.apache.http.client.fluent.Request;
        import org.apache.http.entity.ContentType;
        
        
        public class FluentRequests {
        
         public static void main(String[] args)throws Exception {
         	//執(zhí)行一個(gè)GET請(qǐng)求,同時(shí)設(shè)置Timeout參數(shù)并將響應(yīng)內(nèi)容作為String返回
         Request.Get("http://blog.csdn.net/vector_yi")
         .connectTimeout(1000)
         .socketTimeout(1000)
         .execute().returnContent().asString();
        
         //以Http/1.1版本協(xié)議執(zhí)行一個(gè)POST請(qǐng)求,同時(shí)配置Expect-continue handshake達(dá)到性能調(diào)優(yōu),
         //請(qǐng)求中包含String類型的請(qǐng)求體并將響應(yīng)內(nèi)容作為byte[]返回
         Request.Post("http://blog.csdn.net/vector_yi")
         .useExpectContinue()
         .version(HttpVersion.HTTP_1_1)
         .bodyString("Important stuff", ContentType.DEFAULT_TEXT)
         .execute().returnContent().asBytes();
        
        
         //通過代理執(zhí)行一個(gè)POST請(qǐng)求并添加一個(gè)自定義的頭部屬性,請(qǐng)求包含一個(gè)HTML表單類型的請(qǐng)求體
         //將返回的響應(yīng)內(nèi)容存入文件
         Request.Post("http://blog.csdn.net/vector_yi")
         .addHeader("X-Custom-header", "stuff")
         .viaProxy(new HttpHost("myproxy", 8080))
         .bodyForm(Form.form().add("username", "vip").add("password", "secret").build())
         .execute().saveContent(new File("result.dump"));
         }
        
        }

        二、在后臺(tái)線程中異步執(zhí)行多個(gè)請(qǐng)求
        package com.vectoryi.fluent;
        
        import java.util.LinkedList;
        import java.util.Queue;
        import java.util.concurrent.ExecutionException;
        import java.util.concurrent.ExecutorService;
        import java.util.concurrent.Executors;
        import java.util.concurrent.Future;
        
        import org.apache.http.client.fluent.Async;
        import org.apache.http.client.fluent.Content;
        import org.apache.http.client.fluent.Request;
        import org.apache.http.concurrent.FutureCallback;
        
        
        public class FluentAsync {
        
         public static void main(String[] args)throws Exception {
         // 利用線程池
         ExecutorService threadpool = Executors.newFixedThreadPool(2);
         Async async = Async.newInstance().use(threadpool);
        
         Request[] requests = new Request[] {
         Request.Get("http://www.google.com/"),
         Request.Get("http://www.yahoo.com/"),
         Request.Get("http://www.apache.com/"),
         Request.Get("http://www.apple.com/")
         };
        
        
         Queue> queue = new LinkedList>();
         // 異步執(zhí)行GET請(qǐng)求
         for (final Request request: requests) {
         Future future = async.execute(request, new FutureCallback() {
        
         public void failed(final Exception ex) {
         System.out.println(ex.getMessage() + ": " + request);
         }
        
         public void completed(final Content content) {
         System.out.println("Request completed: " + request);
         }
        
         public void cancelled() {
         }
        
         });
         queue.add(future);
         }
        
         while(!queue.isEmpty()) {
         Future future = queue.remove();
         try {
         future.get();
         } catch (ExecutionException ex) {
         }
         }
         System.out.println("Done");
         threadpool.shutdown();
         }
        
        }

        三、更快速地啟動(dòng)請(qǐng)求
        package com.vectoryi.fluent;
        
        import org.apache.http.client.fluent.Form;
        import org.apache.http.client.fluent.Request;
        
        public class FluentQuickStart {
        
         public static void main(String[] args) throws Exception {
        
         Request.Get("http://targethost/homepage")
         .execute().returnContent();
         Request.Post("http://targethost/login")
         .bodyForm(Form.form().add("username", "vip").add("password", "secret").build())
         .execute().returnContent();
         }
        }

        四、處理Response

        在本例中是利用xmlparsers來解析返回的ContentType.APPLICATION_XML類型的內(nèi)容。

        package com.vectoryi.fluent;
        
        import java.io.IOException;
        import java.nio.charset.Charset;
        
        import javax.xml.parsers.DocumentBuilder;
        import javax.xml.parsers.DocumentBuilderFactory;
        import javax.xml.parsers.ParserConfigurationException;
        
        import org.apache.http.Consts;
        import org.apache.http.HttpEntity;
        import org.apache.http.HttpResponse;
        import org.apache.http.StatusLine;
        import org.apache.http.client.ClientProtocolException;
        import org.apache.http.client.HttpResponseException;
        import org.apache.http.client.ResponseHandler;
        import org.apache.http.client.fluent.Request;
        import org.apache.http.entity.ContentType;
        import org.w3c.dom.Document;
        import org.xml.sax.SAXException;
        
        
        public class FluentResponseHandling {
        
         public static void main(String[] args)throws Exception {
         Document result = Request.Get("http://www.baidu.com")
         .execute().handleResponse(new ResponseHandler() {
        
         public Document handleResponse(final HttpResponse response) throws IOException {
         StatusLine statusLine = response.getStatusLine();
         HttpEntity entity = response.getEntity();
         if (statusLine.getStatusCode() >= 300) {
         throw new HttpResponseException(
         statusLine.getStatusCode(),
         statusLine.getReasonPhrase());
         }
         if (entity == null) {
         throw new ClientProtocolException("Response contains no content");
         }
         DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
         try {
         DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
         ContentType contentType = ContentType.getOrDefault(entity);
         if (!contentType.equals(ContentType.APPLICATION_XML)) {
         throw new ClientProtocolException("Unexpected content type:" + contentType);
         }
         Charset charset = contentType.getCharset();
         if (charset == null) {
         charset = Consts.ISO_8859_1;
         }
         return docBuilder.parse(entity.getContent(), charset.name());
         } catch (ParserConfigurationException ex) {
         throw new IllegalStateException(ex);
         } catch (SAXException ex) {
         throw new ClientProtocolException("Malformed XML document", ex);
         }
         }
        
         });
         // 處理得到的result
         System.out.println(result);
         }
        
        }

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        HttpClient4.2FluentAPI學(xué)習(xí)

        HttpClient4.2FluentAPI學(xué)習(xí):相比于HttpClient 之前的版本,HttpClient 4.2 提供了一組基于流接口(fluent interface)概念的更易使用的API,即Fluent API. 為了方便使用,F(xiàn)luent API只暴露了一些最基本的HttpClient功能。這樣,F(xiàn)luent API就將開發(fā)者從連接管理、資源釋放等
        推薦度:
        標(biāo)簽: 學(xué)習(xí) API 4.2
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 免费人妻av无码专区| 日韩精品无码人妻免费视频| 亚洲一区爱区精品无码| 免费无码婬片aaa直播表情| 日韩视频免费一区二区三区| 男人的天堂av亚洲一区2区| 精品免费久久久久久成人影院| 亚洲女子高潮不断爆白浆| 精品久久久久久久免费加勒比| 亚洲av永久无码精品网址| 国产精品无码一区二区三区免费 | 国产亚洲精品欧洲在线观看| 精品久久久久久久免费人妻| 老子影院午夜伦不卡亚洲| 亚洲A丁香五香天堂网| 久久久久久久国产免费看| 色拍自拍亚洲综合图区| 日韩中文字幕精品免费一区| 亚洲字幕AV一区二区三区四区| 男女啪啪永久免费观看网站| 免费夜色污私人影院网站| 亚洲人成网亚洲欧洲无码久久| 免费在线中文日本| 91丁香亚洲综合社区| 又黄又爽一线毛片免费观看| a毛片免费观看完整| 亚洲日本国产精华液| 可以免费观看一级毛片黄a| 国产成人无码区免费网站| 亚洲人成人77777网站不卡| 日韩精品亚洲专区在线观看| 久久青草国产免费观看| 中文无码亚洲精品字幕| 亚洲精品夜夜夜妓女网| 三年片在线观看免费大全| 日韩毛片免费一二三| 亚洲午夜在线一区| 亚洲国产精品日韩| 免费视频专区一国产盗摄| 一级做受视频免费是看美女| 久久国产亚洲高清观看|