通过JAX-RS Client接口访问HTTP(完善版)-------------JAX-RS学习笔记之二

一、JAX-RS代码

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;
//
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class MainApp {

    public static void main(String[] args) {
        final ClientConfig clientConfig = new ClientConfig();
        clientConfig.connectorProvider(new ApacheConnectorProvider());
        final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(20000);
        cm.setDefaultMaxPerRoute(10000);
        clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, cm);
        Client client = ClientBuilder.newClient(clientConfig);
        WebTarget target = client.target("http://www.baidu.com");
        Invocation.Builder builder = target.request();
        Response response = builder.get();
        String str=response.readEntity(String.class);
        System.out.println(str);
    }

}

 

 

 

二、相关的maven的pom.xml


    javax.ws.rs
    javax.ws.rs-api
    2.0
    jar


    org.glassfish.jersey.core
    jersey-client
    2.25.1
    jar


    org.glassfish.jersey.connectors
    jersey-apache-connector
    2.25.1
    jar

 

你可能感兴趣的:(Java)