Java HTTP客戶端 http4j
http4j是一個開源的Java HTTP客戶端,主要是源自對工作中的一個項目設計的不滿和擴展Apache HttpComponent比較麻煩。
不同于Apache HttpComponent (HttpClient)之處在于:
1. 專注于“客戶端”定位:省去很多不必要的抽象層以及功能,如解析HTTP請求等。
2. 立足于方便易用:從主頁的例子中可以看出使用http4j是極其方便的。
3. 原生的較完善的數據統計: DNS解析耗時,request發送耗時,等待耗時,response接收耗時,字節數等等。這個是為了方便關注網站性能或者流量開銷(購買云計算服務時會特別關注自己的成本)的應用。在Apache的項目中,使用者需要定義自己的Proxy才能實現此功能。示例代碼:
package com.google.code.http4j.example; import java.io.IOException; import java.net.URISyntaxException; import com.google.code.http4j.Client; import com.google.code.http4j.Response; import com.google.code.http4j.impl.BasicClient; import com.google.code.http4j.utils.Metrics; public class BasicExample { public static void main(String[] args) throws InterruptedException, IOException, URISyntaxException { Client client = new BasicClient(); Response response = client.get("http://code.google.com/p/http4j/"); Metrics metrics = response.getMetrics(); System.out.println("Bytes sent:" + metrics.getBytesSent()); System.out.println("Bytes received:" + metrics.getBytesReceived()); System.out.println("Blocking cost:" + metrics.getBlockingCost()); System.out.println("DNS lookup cost:" + metrics.getDnsLookupCost()); System.out.println("Connection establish cost:" + metrics.getConnectingCost()); System.out.println("Sending cost:" + metrics.getSendingCost()); System.out.println("Waiting cost:" + metrics.getWaitingCost()); System.out.println("Receiving cost:" + metrics.getReceivingCost()); System.out.println("SSL handshake cost:" + metrics.getSslHandshakeCost()); response.output(System.out); client.shutdown(); } }
本文由用戶 webphp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!