Jersey(1.19.1) - Client API, Proxy Configuration

 为 Jersey Client 设置代理,可以使用带有 ClientHandler 参数的构造方法创建 Client 实例。

public static void main(String[] args) {
    final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080));
    Client client = new Client(new URLConnectionClientHandler(new HttpURLConnectionFactory() {            
        public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
            return (HttpURLConnection) url.openConnection(proxy);
        }
    }));

    WebResource resource = client.resource("http://example.com");
    ClientResponse response = resource.get(ClientResponse.class);
    System.out.println(String.format("%s %s", 
            response.getStatusInfo().getStatusCode(), 
            response.getStatusInfo().getReasonPhrase()));
}

 

posted on 2016-07-31 00:01  huey2672  阅读(1197)  评论(0编辑  收藏  举报