This discussion is archived
6 Replies Latest reply: May 15, 2010 7:38 PM by EJP RSS

[newbie]: SSLSocket for https via Proxy

843811 Newbie
Currently Being Moderated

I have to go through a proxy here:
I use the code below, does this provides "Basic:" Authentication w/ Base64 encoding?

May problem is, that I have a VB app (not made by me) which connects fine to the host, but mine fails to connect ... and it has something to do with proxy

Thx in advance


        System.setProperty("https.proxyHost", prxyhost);
        System.setProperty("proxyUser", prxyuid);
        System.setProperty("proxyPassword", prxypwd);

            SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
            Writer out = new OutputStreamWriter(socket.getOutputStream());
  • 1. Re: [newbie]: SSLSocket for https via Proxy
    843811 Newbie
    Currently Being Moderated
    check out this url
  • 2. Re: [newbie]: SSLSocket for https via Proxy
    843811 Newbie
    Currently Being Moderated
    Thanks a lot. In the meantime I found "another way around" but the article is quite cool....maybe I'll change my code....

    Thx again,

  • 3. Re: [newbie]: SSLSocket for https via Proxy
    843811 Newbie
    Currently Being Moderated
    I get an error message 'untrusted server cert chain' when i try to make an https request thru the proxy.

    I'm doing exactly what is mentioned in the article.

    Is there a way to use the SSLContext along with the solution mentioned in the article?? That way I can override the TrustManager.

    Any help would be appreciated. Thanx.
  • 4. Re: [newbie]: SSLSocket for https via Proxy
    843811 Newbie
    Currently Being Moderated
    It looks like you have solved the exact problem I am trying to solve. Any help would be greately appreciated.

    I am trying to open SSLConnection from a client to the server via proxy. Here is my code that is supposed to open a Socket.
      Socket getConnection(String remoteHost, int remotePort) throws Exception
        SSLSocket sslSocket;
        Socket socket = null;
        Proxy proxy = super.getProxy(remoteHost, remotePort); // This returns the proxy object
        InetSocketAddress inetSocketAddress = super.getProxyInetSocketAddress(proxy); // This returns proxy address
        char keystorepass[] = PASSWORD.toCharArray();
        KeyStore keyStore = KeyStore.getInstance(JAVA_KEYSTORE);
        keyStore.load(null, keystorepass);
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(SUN_X509);
        keyManagerFactory.init(keyStore, keystorepass);
        TrustManager[] trustManager = new TrustManager[] {new MyX509TrustManager()};
        SSLContext sslContext = SSLContext.getInstance(PROTOCOL_LEVEL);
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManager, null);
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        //InetSocketAddress inetSocketAddress = getProxyInetSocketAddress(host, port);
        if (super.isProxyDetected())
          System.out.println("connecting with Proxy");
          //getSocket(host, port, false) Creates a socket with 
          sslSocket = (SSLSocket) sslSocketFactory.createSocket(new Socket(proxy), remoteHost, remotePort, true);
          System.out.println("connecting without Proxy");
          sslSocket = (SSLSocket) sslSocketFactory.createSocket(remoteHost, remotePort);
        System.out.println("SSLSocket:" + sslSocket);
        if (sslSocket != null)
          socket = sslSocket;
        System.out.println("Socket:" + socket);
        return socket;
  • 5. Re: [newbie]: SSLSocket for https via Proxy
    843811 Newbie
    Currently Being Moderated
    cannot work! Please help me !!!!!!!!!

    SSLContext ctx;
    try {
    ctx = SSLContext.getInstance("SSLv3");
    // 创建密钥管理器
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    // 创建证书管理器
    TrustManagerFactory tmf = TrustManagerFactory
    // 用来存储密钥
    KeyStore ks = KeyStore.getInstance("JKS");
    // 用来存储证书
    KeyStore tks = KeyStore.getInstance("JKS");
    // 加载客户端密钥
    ks.load(new FileInputStream(Constants.CLIENT_KEYSTORE_PATH),
    // 加载服务端证书,由服务端密钥生成
    tks.load(new FileInputStream(Constants.CLIENT_TRUSTSTORE_PATH),
    // 使用密钥初始化密钥管理器
    kmf.init(ks, Constants.CLIENT_KEY_STORE_PASSWORD.toCharArray());
    // 使用证书初始化证书管理器
    ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

    String prxyhost = "";
    int prxyport = 1080;
    SocketAddress addr = new InetSocketAddress(prxyhost, Integer
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
    Socket socket = new Socket(proxy);
    sslSocket = ctx.getSocketFactory().createSocket(socket,Constants.DEFAULT_HOST,9500,false);

    EXCEPTION /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Underlying socket is not connected
    at<init>(Unknown Source)
    at Source)
    at com.aastocks.ssl.Client.init(
    at com.aastocks.ssl.Client.createSSLConn(
    at com.aastocks.ssl.Client.main(
    2010-05-13 09:51:21 [INFO] main [com.aastocks.ssl.Client] - Create sslSocket error
  • 6. Re: [newbie]: SSLSocket for https via Proxy
    EJP Guru
    Currently Being Moderated
    Socket socket = new Socket(proxy);
    socket.connect(); Underlying socket is not connected