2012-04-30 3 views
6

저지 클라이언트에 프록시 서버를 구성하고 싶습니다.
(http.proxyHost와 같은 JVM 인수를 사용하여) 전체 응용 프로그램에 프록시를 구성하고 싶지 않고 Apache 클라이언트를 사용하지 않습니다.
here HttpUrlConnectionFactory를 통해 HttpUrlConnection 을 제공함으로써 옵션을 수행 할 수있는 옵션이 있지만 모든 코드 예제를 찾을 수 없습니다.
누구든지 어떻게 할 수 있는지 알고 있습니까?
감사합니다.저지 클라이언트에 프록시 구성

답변

10

시도는, 내가 완료있어 :

  1. 이 HttpURLConnectionFactory 구현 및 방법 'getHttpURLConnection'를 오버라이드 (override), 내 구현 (루카 덕분에)입니다 :
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128)); return new HttpURLConnection(url, proxy);

  2. 사이클로 작업하기 전에 Jersey Client를 인스턴스화하고 URLConnectionClientHandler을 새로 작성하고 해당 생성자에 HttpURLConnectionFactory을 제공하십시오. 그런 다음 새 Client를 작성하고 Client 생성자에서 ClientHandler를 제공하십시오. 내 코드 :
    URLConnectionClientHandler urlConnectionClientHandler = new URLConnectionClientHandler(new MyHttpURLConnectionFactory());
    _client = new Client(urlConnectionClientHandler);

희망의 도움이됩니다. 모든

+0

이것은 Java 6 이후 버전에서만 작동합니다. Proxy가있는 HttpURLConnection 생성자는 Java 5에서 사용할 수 없습니다. –

2

루카의 도움으로

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

현재 어떻게'Client.create (config)'를 사용하고 있는지 잘 모르겠습니다. 클라이언트를 생성 할 때'UrlConnection'을 사용하는 옵션을 찾을 수 없습니다. – danieln

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.getWebservi로 webProxy.getWeserviceHost를 대체하십시오. ceProxyPort() 프록시 포트로.

이것은 나를 위해 일했으며 또한 당신을 위해 작동해야합니다. 저지 1.8을 사용하고 있지만 저지 2에서도 작동해야합니다.

+0

덕분에이 기능이 저에게 효과적이었습니다. – whoami

관련 문제