返回顶部

收藏

Java 的 HTTP 客户端 http4j 示例代码

更多

http4j是 一个开源的Java HTTP客户端,主要是源自对工作中的一个项目设计的不满和扩展Apache HttpComponent比较麻烦。

BasicExample.java

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 Exception {
                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();
        }
}

标签:HTTP,http4j,java

收藏

0人收藏

支持

0

反对

0

发表评论