【问题标题】:Configure proxy to Jersey client为 Jersey 客户端配置代理
【发布时间】:2012-04-30 14:19:31
【问题描述】:

我想为我的 Jersey 客户端配置一个代理服务器。
我不想为整个应用程序配置代理(使用 JVM 参数,例如 http.proxyHost),我宁愿不使用 Apache 客户端。
我读到here 有一个选项可以通过提供 HttpUrlConnection 通过 HttpUrlConnectionFactory,但我找不到任何代码示例。
有谁知道我该怎么做?
谢谢!

【问题讨论】:

    标签: java proxy jersey


    【解决方案1】:

    在卢卡的帮助下,我完成了:

    1. 实现HttpURLConnectionFactory,并覆盖方法getHttpURLConnection,我的实现是(感谢Luca):

      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128));
      return new HttpURLConnection(url, proxy);
      
    2. 在实例化 Jersey 客户端之前,创建一个新的 URLConnectionClientHandler,并在其构造函数中提供您的 HttpURLConnectionFactory。然后创建一个新的Client,并在Client 构造函数中提供您的ClientHandler。我的代码:

      URLConnectionClientHandler urlConnectionClientHandler = new URLConnectionClientHandler(new MyHttpURLConnectionFactory());
      _client = new Client(urlConnectionClientHandler);
      

    希望对您有所帮助。

    【讨论】:

    • 请注意,这仅适用于 Java 6 及更高版本 - 带有代理的 HttpURLConnection 构造函数在 Java 5 中不可用。
    【解决方案2】:

    首先我创建了这个类

        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

    【讨论】:

      【解决方案3】:

      试试

      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
      conn = new URL(url).openConnection(proxy);
      

      【讨论】:

      • 我不确定如何,目前我正在使用Client.create(config)。创建客户端时找不到任何使用 UrlConnection 的选项。
      猜你喜欢
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      相关资源
      最近更新 更多