You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by nitya vyas <ni...@gmail.com> on 2007/10/29 06:13:00 UTC

http/ https connection issue for HTTPClient 3.1

Hi,
    As Roland had previously suggested I took latest HTTPClient Version (3.1)
and used it in our desktop app which connects to server with https
(StrictSSLProtocolSocketFactory impl.) through Proxy server... To use proxy
i had to create my own socketfactory which will override createSocket()
method and it will set new Socket with Proxy.NO_PROXY.

heres my socetfactory implementation.

public class AsiteSocketFactory extends StrictSSLProtocolSocketFactory
implements ProtocolSocketFactory {

    public AsiteSocketFactory() throws
java.security.GeneralSecurityException, java.io.IOException{
        super(true);
    }

    public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
       return new Socket(host,port);
    }

    public Socket createSocket(String host, int port, InetAddress
localAddress, int localPort) throws IOException, UnknownHostException {
        return new Socket(host,port,localAddress,localPort);
    }

    public Socket createSocket(String host, int port, InetAddress
localAddress, int localPort, HttpConnectionParams params)
            throws IOException, UnknownHostException, ConnectException {
        Socket rval;
        if (params == null) {
            throw new IllegalArgumentException("Parameters may not be
null");
        }
        rval = new Socket(Proxy.NO_PROXY);
        SocketAddress localaddr = new
        InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        rval.bind(localaddr);
        rval.connect(remoteaddr);
        return rval;
    }
}


I also need StrictSSLProtocol impl so i extended that class in factory and
called Super(true) ....

I create HTTPClient object like this... httpClient is a static object...

MultiThreadedHttpConnectionManager connectionManager = new
MultiThreadedHttpConnectionManager();
httpClient = new HttpClient(connectionManager);
Protocol stricthttps = new Protocol("https", new AsiteSocketFactory(), 443);
Protocol.registerProtocol("http", stricthttps);
httpClient.getHostConfiguration().setHost(SystemConstants.connectionHostName,
443, stricthttps);
httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);

Now when i run this same code on Production Box (https configured) even if i
pass URL in Method (PostMethod postMethod = new PostMethod(requestUrl);)
like "http://bim.xyz.com" it works fine... I got an http request on server
log with this....

But if i run same thing on another server which is run on http only(internal
testing purpose),  i get
sun.security.provider.certpath.SunCertPathBuilderException: unable to find
valid certification path to requested target

does this mean that server is sending the certificate even if i connect by
URL "http" in PostMethod obect???

Do i need to create httpclient object differently for http requests??? then
I will also have to remove my sockectfactory  from the  Protocol constructor
because it is extending StrictSSLProtSockFactory.....

Please Help..

Thanks
Nitya

Re: http/ https connection issue for HTTPClient 3.1

Posted by nitya vyas <ni...@gmail.com>.
Sorry.. half message... below is the completed code... this now works for
http requests with/without proxy...

thanks.

On 10/30/07, nitya vyas <ni...@gmail.com> wrote:
>
> Hi Julius,
>
> I think this Protocol.registerProtocol("https", stricthttps); will not
> work with httpClient 3.1 if i m not mistaken.. because open() method of
> HTTPConnection has this code...
>
> if (isSecure() && isProxied()) {
>         Protocol defaultprotocol = Protocol.getProtocol("http");
>          socketFactory = defaultprotocol.getSocketFactory();
>  } else {
>        socketFactory = this.protocolInUse.getSocketFactory();
>  }
> Thers no code that gets the socketfactory from ID "https"...
>
> Anyways i found another solution for the problem .. i dont know if its
> proper... I removed the extends StrictSSL
>
>
> public class AsiteSocketFactory implements ProtocolSocketFactory {
>
>     public AsiteSocketFactory(boolean isHttps) throws
> java.security.GeneralSecurityException, java.io.IOException{
>
>           if(isHttps){
>
                 new StrictSSLProtocolSocketFactory(true);
          }else{

>               new DefaultProtocolSocketFactory();
>     }
>
>     public Socket createSocket(String host, int port) throws IOException,
> UnknownHostException {
>        return new Socket(host,port);
>     }
>
>     public Socket createSocket(String host, int port, InetAddress
> localAddress, int localPort) throws IOException, UnknownHostException {
>         return new Socket(host,port,localAddress,localPort);
>     }
>
>     public Socket createSocket(String host, int port, InetAddress
> localAddress, int localPort, HttpConnectionParams params)
>             throws IOException, UnknownHostException, ConnectException {
>         Socket rval;
>         if (params == null) {
>             throw new IllegalArgumentException("Parameters may not be
> null");
>         }
>         rval = new Socket(Proxy.NO_PROXY);
>         SocketAddress localaddr = new
>         InetSocketAddress(localAddress, localPort);
>         SocketAddress remoteaddr = new InetSocketAddress(host, port);
>         rval.bind(localaddr);
>         rval.connect(remoteaddr);
>         return rval;
>     }
> }
>
>
> On 10/29/07, Julius Davies <ju...@gmail.com> wrote:
> >
> > I would recommend replacing this:
> > Protocol.registerProtocol("http", stricthttps);
> >
> >
> > With this:
> > Protocol.registerProtocol("https", stricthttps);
> >
> >
> > --
> > yours,
> >
> > Julius Davies
> > 250-592-2284 (Home)
> > 250-893-4579 (Mobile)
> > http://juliusdavies.ca/
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> >
> >
>

Re: http/ https connection issue for HTTPClient 3.1

Posted by nitya vyas <ni...@gmail.com>.
Hi Julius,

I think this Protocol.registerProtocol("https", stricthttps); will not work
with httpClient 3.1 if i m not mistaken.. because open() method of
HTTPConnection has this code...

if (isSecure() && isProxied()) {
        Protocol defaultprotocol = Protocol.getProtocol("http");
         socketFactory = defaultprotocol.getSocketFactory();
 } else {
       socketFactory = this.protocolInUse.getSocketFactory();
 }
Thers no code that gets the socketfactory from ID "https"...

Anyways i found another solution for the problem .. i dont know if its
proper... I removed the extends StrictSSL


public class AsiteSocketFactory implements ProtocolSocketFactory {

    public AsiteSocketFactory(boolean isHttps) throws
java.security.GeneralSecurityException, java.io.IOException{

          if(isHttps){
          }else{

    }

    public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
       return new Socket(host,port);
    }

    public Socket createSocket(String host, int port, InetAddress
localAddress, int localPort) throws IOException, UnknownHostException {
        return new Socket(host,port,localAddress,localPort);
    }

    public Socket createSocket(String host, int port, InetAddress
localAddress, int localPort, HttpConnectionParams params)
            throws IOException, UnknownHostException, ConnectException {
        Socket rval;
        if (params == null) {
            throw new IllegalArgumentException("Parameters may not be
null");
        }
        rval = new Socket(Proxy.NO_PROXY);
        SocketAddress localaddr = new
        InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        rval.bind(localaddr);
        rval.connect(remoteaddr);
        return rval;
    }
}


On 10/29/07, Julius Davies <ju...@gmail.com> wrote:
>
> I would recommend replacing this:
> Protocol.registerProtocol("http", stricthttps);
>
>
> With this:
> Protocol.registerProtocol("https", stricthttps);
>
>
> --
> yours,
>
> Julius Davies
> 250-592-2284 (Home)
> 250-893-4579 (Mobile)
> http://juliusdavies.ca/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>

Re: http/ https connection issue for HTTPClient 3.1

Posted by Julius Davies <ju...@gmail.com>.
I would recommend replacing this:
Protocol.registerProtocol("http", stricthttps);


With this:
Protocol.registerProtocol("https", stricthttps);


-- 
yours,

Julius Davies
250-592-2284 (Home)
250-893-4579 (Mobile)
http://juliusdavies.ca/

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org