配置代理到Jersey客户端

时间:2012-04-30 14:19:31

标签: java proxy jersey

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

3 个答案:

答案 0 :(得分:11)

在Luca的帮助下,我完成了它:

  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,并在ClientHandler构造函数中提供Client。我的代码:

    URLConnectionClientHandler urlConnectionClientHandler = new URLConnectionClientHandler(new MyHttpURLConnectionFactory());
    _client = new Client(urlConnectionClientHandler);
    
  3. 希望有所帮助。

答案 1 :(得分:5)

首先我创建了这个类

    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

答案 2 :(得分:2)

尝试

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