Jersey客户端+设置代理

16

嗨,我有一个jersey客户端,用于上传文件。我尝试在本地使用它,一切正常。但是在生产环境中,我必须设置代理。我浏览了几个页面,但没有找到确切的解决方案。有人可以帮我吗?

这是我的客户端代码:

File file = new File("e:\\test.zip");
FormDataMultiPart part = new FormDataMultiPart();
part.bodyPart(new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
WebResource resource = null;

if (proxy.equals("yes")) {
    // How do i configure client in this case?
} else {
    // this uses system proxy i guess
    resource = Client.create().resource(url);
}

String response = (String) resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
System.out.println(response);
9个回答

17
如果您想避免在旧项目中使用更多库,并且不需要代理身份验证,则有一种更简单的方法:
首先,您需要一个实现了“HttpURLConnectionFactory”的类:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory;


public class ConnectionFactory implements HttpURLConnectionFactory {

    Proxy proxy;

    private void initializeProxy() {
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("myproxy.com", 3128));
    }

    public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
        initializeProxy();
        return (HttpURLConnection) url.openConnection(proxy);
    }
}

第二步是实例化一个com.sun.jersey.client.urlconnection.URLConnectionHandler

URLConnectionClientHandler ch  = new URLConnectionClientHandler(new ConnectionFactory());

第三种方法是使用Client构造函数而不是Client.create
Client client = new Client(ch);

当然,您可以定制ConnectionFactory中代理的初始化。

13

luckyluke的回答应该能起作用。 这是我的版本:

ClientConfig config = new DefaultClientConfig();
Client client = new Client(new URLConnectionClientHandler(
        new HttpURLConnectionFactory() {
    Proxy p = null;
    @Override
    public HttpURLConnection getHttpURLConnection(URL url)
            throws IOException {
        if (p == null) {
            if (System.getProperties().containsKey("http.proxyHost")) {
                p = new Proxy(Proxy.Type.HTTP,
                        new InetSocketAddress(
                        System.getProperty("http.proxyHost"),
                        Integer.getInteger("http.proxyPort", 80)));
            } else {
                p = Proxy.NO_PROXY;
            }
        }
        return (HttpURLConnection) url.openConnection(p);
    }
}), config);

4
System.setProperty("http.proxyHost","your proxy url");
System.setProperty("http.proxyPort", "your proxy port");

如果您正在使用https,则相应的属性是(自然)https.proxyHost和https.proxyPort,根据https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html。 - Brian Agnew

4

给你:

 DefaultApacheHttpClient4Config config = new DefaultApacheHttpClient4Config();
      config.getProperties().put(
      ApacheHttpClient4Config.PROPERTY_PROXY_URI, 
      "PROXY_URL"
 );

 config.getProperties().put(
      ApacheHttpClient4Config.PROPERTY_PROXY_USERNAME, 
      "PROXY_USER"
 );

 config.getProperties().put(
      ApacheHttpClient4Config.PROPERTY_PROXY_PASSWORD, 
      "PROXY_PASS"
 );     

 Client c = ApacheHttpClient4.create(config);
 WebResource r = c.resource("https://www.google.com/");

0

SDolgy。我在Jersey客户端实例化中添加了3个功能:

启用SSL TLSv1.1(需要JVM>= 1.7),配置连接池以增加连接数,设置系统代理。

 # My props file    
 # CONFIGURAR EL CLIENTE
 #PROXY_URI=http://5.5.5.5:8080
 #SECURITY_PROTOCOL=TLSv1.2
 #POOLING_HTTP_CLIENT_CONNECTION_MANAGER.MAXTOTAL=200
 #POOLING_HTTP_CLIENT_CONNECTION_MANAGER.DEFAULTMAXPERROUTE=20

 import java.util.Properties;
 import javax.net.ssl.SSLContext;
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientBuilder;

 import org.apache.http.config.Registry;
 import org.apache.http.config.RegistryBuilder;
 import org.apache.http.conn.socket.ConnectionSocketFactory;
 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

 import org.glassfish.jersey.SslConfigurator;
 import org.glassfish.jersey.apache.connector.ApacheClientProperties;
 import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
 import org.glassfish.jersey.client.ClientConfig;
 import org.glassfish.jersey.client.ClientProperties;
 import org.glassfish.jersey.jackson.JacksonFeature;

 public class JerseyClientHelper {
     private static Client cliente;
     private static final Properties configuracion = SForceConfiguration.getInstance();

     public static synchronized Client getInstance() {
         if (cliente == null) {            
             SSLContext sslContext = SslConfigurator.newInstance().securityProtocol(configuracion.getProperty("SECURITY_PROTOCOL")).createSSLContext(); // Usar TLSv1.2

             SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
             Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
             .register("http", PlainConnectionSocketFactory.getSocketFactory())
             .register("https", socketFactory)
             .build();

             // Para configurar las conexiones simultaneas al servidor
             int maxTotal = Integer.parseInt(configuracion.getProperty("POOLING_HTTP_CLIENT_CONNECTION_MANAGER.MAXTOTAL"));
             int defaultMaxPerRoute = Integer.parseInt(configuracion.getProperty("POOLING_HTTP_CLIENT_CONNECTION_MANAGER.DEFAULTMAXPERROUTE"));
             PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
             connectionManager.setMaxTotal(maxTotal);
             connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);

             ClientConfig config = new ClientConfig();
             config.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
             config.connectorProvider(new ApacheConnectorProvider());
             config.property(ClientProperties.PROXY_URI, configuracion.getProperty("PROXY_URI")); // Debemos poner el PROXY del sistema


             cliente = ClientBuilder.newBuilder().sslContext(sslContext).withConfig(config).build();

         }        
         return cliente;
     }

 }

0

我拿了user67871并稍作修改。这种方法的好处是它可以与Windows上的系统代理一起使用。如果您在Windows上配置了IE代理,那么此代码将会捕获到它。当您运行Fiddler时,它也会设置系统代理,因此这使得使用Jersey和Fiddler变得非常容易。

    Client client = new Client(new URLConnectionClientHandler(
            new HttpURLConnectionFactory() {
        Proxy p = null;

        @Override
        public HttpURLConnection getHttpURLConnection(URL url)
                throws IOException {
            try {
                if (p == null) {
                    List<Proxy> proxies = ProxySelector.getDefault().select(url.toURI());
                    if (proxies != null) {
                        // just use the first one, I don't know if we should sometimes use a different one
                        p = proxies.get(0);
                    }
                    if (p == null) {
                        p = Proxy.NO_PROXY;
                    }
                }
                return (HttpURLConnection) url.openConnection(p);
            } catch (URISyntaxException ex) {
                throw new IOException(ex);
            }
        }
    }), config);

0

首先,我创建了这个类

    import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.URL;
    import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

/**
 *
 * @author Aimable
 */
public class ConnectionFactory implements HttpURLConnectionFactory {

    Proxy proxy;

    String proxyHost;

    Integer proxyPort;

    SSLContext sslContext;

    public ConnectionFactory() {
    }

    public ConnectionFactory(String proxyHost, Integer proxyPort) {
        this.proxyHost = proxyHost;
        this.proxyPort = proxyPort;
    }

    private void initializeProxy() {
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
    }

    @Override
    public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
        initializeProxy();
        HttpURLConnection con = (HttpURLConnection) url.openConnection(proxy);
        if (con instanceof HttpsURLConnection) {
            System.out.println("The valus is....");
            HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection(proxy);
            httpsCon.setHostnameVerifier(getHostnameVerifier());
            httpsCon.setSSLSocketFactory(getSslContext().getSocketFactory());
            return httpsCon;
        } else {
            return con;
        }

    }

    public SSLContext getSslContext() {
        try {
            sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, new TrustManager[]{new SecureTrustManager()}, new SecureRandom());
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (KeyManagementException ex) {
            Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
        return sslContext;
    }

    private HostnameVerifier getHostnameVerifier() {
        return new HostnameVerifier() {
            @Override
            public boolean verify(String hostname,
                    javax.net.ssl.SSLSession sslSession) {
                return true;
            }
        };
    }

}

然后我还创建了另一个名为SecureTrustManager的类

    import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;

/**
 *
 * @author Aimable
 */
public class SecureTrustManager implements X509TrustManager {

    @Override
    public void checkClientTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0];
    }

    public boolean isClientTrusted(X509Certificate[] arg0) {
        return true;
    }

    public boolean isServerTrusted(X509Certificate[] arg0) {
        return true;
    }

}

创建完这个类之后,我会像这样调用客户端。
URLConnectionClientHandler cc = new URLConnectionClientHandler(new ConnectionFactory(webProxy.getWebserviceProxyHost(), webProxy.getWebserviceProxyPort()));
    client = new Client(cc);
    client.setConnectTimeout(2000000);

将webProxy.getWeserviceHost替换为您的proxyHost,将webProxy.getWebserviceProxyPort()替换为代理端口。

这对我有用,也应该对您有用。请注意,我正在使用Jersey 1.8,但它也适用于Jersey 2。


0

成功配置代理,覆盖了提供给 newClient 的客户端配置。这适用于版本 24。

return ClientBuilder.newClient(new ClientConfig().connectorProvider(new ConnectorProvider() {
    // figured this out from digging through jersey source code
    @Override
    public Connector getConnector(Client client, Configuration runtimeConfig) {
        HttpUrlConnectorProvider customConnProv = new HttpUrlConnectorProvider();
        customConnProv.connectionFactory(new ConnectionFactory() {
            @Override
            public HttpURLConnection getConnection(java.net.URL url) throws IOException {
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
                return (HttpURLConnection) url.openConnection(proxy);
            }
        });
        return customConnProv.getConnector(client, runtimeConfig);
    }
}));

0

这是其他人提出的简化版本:

Proxy proxy = ...;
Client client = ClientBuilder.newClient(new ClientConfig()
    .connectorProvider(new HttpUrlConnectorProvider()
        .connectionFactory(url -> (HttpURLConnection) url.openConnection(proxy))));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接