泽西客户端+设置代理

时间:2012-05-02 14:18:14

标签: java proxy client 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 个答案:

答案 0 :(得分:15)

如果您想在遗留项目中避免使用更多库并且不需要代理身份验证,那么有一种更简单的方法:

首先,您需要一个实现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

中自定义代理服务器的初始化

答案 1 :(得分:11)

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);

答案 2 :(得分:2)

你走了:

 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/");

答案 3 :(得分:2)

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

答案 4 :(得分: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);

答案 5 :(得分: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);

通过代理端口替换您的proxyHost和webProxy.getWebserviceProxyPort()的webProxy.getWeserviceHost。

这对我有用,它也适合你。请注意,我使用的是Jersey 1.8,但它也适用于Jersey 2

答案 6 :(得分:0)

SDolgy。我这样做了,在Jersey客户端实例中添加了3个功能: 启用S​​SL TLSv1.1(需要JVM&gt; = 1.7),配置conex。池。要增加连接,请设置系统代理。

 # 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;
     }

 }

答案 7 :(得分: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);
            }
        }));


答案 8 :(得分:0)

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

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