Java HTTP客户端 http4j

webphp 13年前
     <p>http4j是一个开源的Java HTTP客户端,主要是源自对工作中的一个项目设计的不满和扩展Apache HttpComponent比较麻烦。</p>    <p>不同于Apache HttpComponent (HttpClient)之处在于:</p>    <p>1. 专注于“客户端”定位:省去很多不必要的抽象层以及功能,如解析HTTP请求等。<br /> 2. 立足于方便易用:从主页的例子中可以看出使用http4j是极其方便的。<br /> 3. 原生的较完善的数据统计: DNS解析耗时,request发送耗时,等待耗时,response接收耗时,字节数等等。这个是为了方便关注网站性能或者流量开销(购买云计算服务时会特别关注自己的成本)的应用。在Apache的项目中,使用者需要定义自己的Proxy才能实现此功能。示例代码:</p>    <pre class="brush:java; toolbar: true; auto-links: false;">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();         } }</pre>    <p><strong>项目主页:</strong><a href="http://www.open-open.com/lib/view/home/1326029865250" target="_blank">http://www.open-open.com/lib/view/home/1326029865250</a></p>    <p></p>