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 Francois Grandjean <FG...@transwide.com> on 2005/01/27 12:00:31 UTC

RE: Problem with PostMethod.getHostConfiguration().setHost() on v3beta1

Thanks for the help. What I'm actually trying to do is to use a custom
SecureProtocolSocketFactory because I have to connect to several hosts
through HTTPS with different keystores and truststores.

But even if I use an absolute URL in both cases, I have a different
SocketFactory used. 
Example 1 below uses the CustomProtocolSocketFactory as expected
Example 2 uses org.apache.commons.httpclient.protocol.
SSLProtocolSocketFactory

1/
Protocol httpsProtocol = new Protocol("https", new
CustomProtocolSocketFactory(), 443);
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost("https://somehost/", 443, httpsProtocol);
HttpClient httpClient = new HttpClient();
GetMethod method = new GetMethod("https://somehost/");
httpClient.executeMethod(hostConfig, method);

2/
Protocol httpsProtocol = new Protocol("https", new
CustomProtocolSocketFactory(), 443);
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost("https://somehost/", 443, httpsProtocol);
HttpClient httpClient = new HttpClient();
httpClient.setHostConfiguration(hostConfig); //ONLY DIFFERENCE
GetMethod method = new GetMethod("https://somehost/");
httpClient.executeMethod(hostConfig, method);

Note also that calling httpClient.executeMethod(method), i.e. without
passing the config, does not change anything.

Thanks a lot !!

Francois

-----Original Message-----
From: Oleg Kalnichevski [mailto:olegk@apache.org] 
Sent: mercredi 26 janvier 2005 21:13
To: HttpClient User Discussion
Subject: RE: Problem with PostMethod.getHostConfiguration().setHost() on
v3beta1

Francois,

If the target URL is absolute (explicitly defines a protocol and a
host), it always overrides the default host config. One should be using
relative URLs when expecting the host config to take effect.

I hope this explains it

Oleg

On Wed, 2005-01-26 at 19:23 +0100, Francois Grandjean wrote:
> Hi,
> 
> I had exactly the same problem. The proposed solution works but I'm
> still surprised that the following code (setting the configuration
also
> on the client level) leads to the same problem:
> 
> 
> HostConfiguration hostconfig = new HostConfiguration("somehost",
15555,
> MySslProtocolObject);
> HttpClient httpclient = new HttpClient(); 
> Httpclient. setHostConfiguration(hostconfig) //ADDED LINE
> httpclient.executeMethod(hostconfig, httpget);
> 
> 
> I looked at the code, and I think the problem comes from lines
382-386:
> 
> HostConfiguration defaulthostconfig = getHostConfiguration();
> if (hostConfiguration == null || hostConfiguration ==
defaulthostconfig)
> {
>     // make a deep copy of the host defaults
>     hostConfiguration = new HostConfiguration(defaulthostconfig);
>     URI uri = method.getURI(); 
>     if (uri.isAbsoluteURI()) {
>         hostConfiguration.setHost(uri);
>     }
> }
> 
> Having the configuration set at the client level makes it enter the if
> statement and clone the config.
> But the call to 
> hostConfiguration.setHost(uri); 
> overwrites the hostConfiguration.protocol with the default protocol
> registered (Protocol.getProtocol(uri.getScheme())).
> 
> Thanks for your feedback.
> 
> Francois
> 
> 
> -----Original Message-----
> From: Riad Souissi [mailto:rsouissi.c@stc.com.sa] 
> Sent: lundi 17 janvier 2005 13:20
> To: HttpClient User Discussion
> Subject: RE: Problem with PostMethod.getHostConfiguration().setHost()
on
> v3 beta1
> 
> 
> Thanks, works exactly as expected!
> 
> Regards,
> riad
> 
> 
> -----Original Message-----
> From: Oleg Kalnichevski [mailto:olegk@apache.org]
> 
> Sent: Monday, January 17, 2005 2:13 PM
> To: httpclient-user@jakarta.apache.org
> Subject: Re: Problem with PostMethod.getHostConfiguration().setHost()
on
> v3 beta1
> 
> On Mon, Jan 17, 2005 at 01:14:34PM +0300, Riad Souissi wrote:
> >
> 
> >
> 
> >
> 
> > Hi,
> >
> 
> > Before, using version 2 or version3_alpha2, the following code was
> > working:
> >
> 
> > ----
> > myPostMethod.getHostConfiguration().setHost("somehost", "15555",
> > mySslProtocolObject)
> > ----
> >
> 
> > The reason I was setting mySslProtocolObject on the method level is
> 
> > because I do not want to force it on the whole host:port. I need to
> 
> > have a granular control.
> >
> 
> > Then I switched to version3_beta1, and it stopped to work. It does
not
> 
> > seem to use any of the SSL classes defined within
mySslProtocolObject
> 
> > (the object which I derived from ProtocolSocketFactory). I used
> 
> > println calls to find out that sslProtocolObject was not used when I
> 
> > was sending an HTTPS request to host:port using the myPostMethod
> defined above.
> >
> 
> > Do you have an idea how to get it back to work ? I know that
> > getHostConfiguration().setHost() is deprecated, but I found no other
> 
> > way to do the same thing with this granularity (at the PostMethod
> level).
> >
> 
> Hi Riad,
> 
> Try this
> 
> HostConfiguration hostconfig = new HostConfiguration("somehost",
15555,
>   MySslProtocolObject);
> HttpClient httpclient = new HttpClient(); ...
> 
> httpclient.executeMethod(hostconfig, httpget);
>  
> 
> Hope this helps
> 
> Oleg
> 
> > Regards,
> > riad
> >
> 
> > DiscalimerThe information in this email and in any files transmitted
> 
> > with it, is intended only for the addressee and may contain
> confidential and/or privileged material.
> > Access to this email by anyone else is unauthorized. If you receive
> 
> > this in error, please contact the sender immediately and delete the
> material from any computer.
> > If you are not the intended recipient, any disclosure, copying,
> 
> > distribution or any action taken or omitted to be taken in reliance
on
> it, is strictly prohibited.
> > Statement and opinions expressed in this e-mail are those of the
> 
> > sender, and do not necessarily reflect those of STC.
> >
> 
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
httpclient-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> 
> > httpclient-user-help@jakarta.apache.org
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
httpclient-user-help@jakarta.apache.org
> 
> 
> DiscalimerThe information in this email and in any files transmitted
> with it,
> is intended only for the addressee and may contain confidential and/or
> privileged material.
> Access to this email by anyone else is unauthorized. If you receive
this
> in error,
> please contact the sender immediately and delete the material from any
> computer.
> If you are not the intended recipient, any disclosure, copying,
> distribution or
> any action taken or omitted to be taken in reliance on it, is strictly
> prohibited.
> Statement and opinions expressed in this e-mail are those of the
sender,
> and do not
> necessarily reflect those of STC.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
httpclient-user-help@jakarta.apache.org
> 


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



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


Re: Problem with PostMethod.getHostConfiguration().setHost() on v3beta1

Posted by Oleg Kalnichevski <ol...@apache.org>.
Okay. I.ll try to explain it using an example 

Consider the following scenario:

HostConfiguration hostconfig = new HostConfiguration();
Hostconfig.setHost("thishost");

GetMethod httpget = new GetMethod("http://thathost/stuff");
httpclient.executeMethod(hostconfig, httpget); // -> ???

Which host do you expect HttpClient to send the request to? I believe
the targer host in this case should be "thathost"

In order to avoid ambiguity please use relative URLs when overriding the
default host config:

HostConfiguration hostconfig = new HostConfiguration();
Hostconfig.setHost("thishost");

GetMethod httpget = new GetMethod("/stuff");
httpclient.executeMethod(hostconfig, httpget); // -> thishost

Exactly the same logic applies to socket factories

Hope this helps

Oleg


On Thu, Jan 27, 2005 at 12:00:31PM +0100, Francois Grandjean wrote:
> Thanks for the help. What I'm actually trying to do is to use a custom
> SecureProtocolSocketFactory because I have to connect to several hosts
> through HTTPS with different keystores and truststores.
> 
> But even if I use an absolute URL in both cases, I have a different
> SocketFactory used. 
> Example 1 below uses the CustomProtocolSocketFactory as expected
> Example 2 uses org.apache.commons.httpclient.protocol.
> SSLProtocolSocketFactory
> 
> 1/
> Protocol httpsProtocol = new Protocol("https", new
> CustomProtocolSocketFactory(), 443);
> HostConfiguration hostConfig = new HostConfiguration();
> hostConfig.setHost("https://somehost/", 443, httpsProtocol);
> HttpClient httpClient = new HttpClient();
> GetMethod method = new GetMethod("https://somehost/");
> httpClient.executeMethod(hostConfig, method);
> 
> 2/
> Protocol httpsProtocol = new Protocol("https", new
> CustomProtocolSocketFactory(), 443);
> HostConfiguration hostConfig = new HostConfiguration();
> hostConfig.setHost("https://somehost/", 443, httpsProtocol);
> HttpClient httpClient = new HttpClient();
> httpClient.setHostConfiguration(hostConfig); //ONLY DIFFERENCE
> GetMethod method = new GetMethod("https://somehost/");
> httpClient.executeMethod(hostConfig, method);
> 
> Note also that calling httpClient.executeMethod(method), i.e. without
> passing the config, does not change anything.
> 
> Thanks a lot !!
> 
> Francois
> 
> -----Original Message-----
> From: Oleg Kalnichevski [mailto:olegk@apache.org] 
> Sent: mercredi 26 janvier 2005 21:13
> To: HttpClient User Discussion
> Subject: RE: Problem with PostMethod.getHostConfiguration().setHost() on
> v3beta1
> 
> Francois,
> 
> If the target URL is absolute (explicitly defines a protocol and a
> host), it always overrides the default host config. One should be using
> relative URLs when expecting the host config to take effect.
> 
> I hope this explains it
> 
> Oleg
> 
> On Wed, 2005-01-26 at 19:23 +0100, Francois Grandjean wrote:
> > Hi,
> > 
> > I had exactly the same problem. The proposed solution works but I'm
> > still surprised that the following code (setting the configuration
> also
> > on the client level) leads to the same problem:
> > 
> > 
> > HostConfiguration hostconfig = new HostConfiguration("somehost",
> 15555,
> > MySslProtocolObject);
> > HttpClient httpclient = new HttpClient(); 
> > Httpclient. setHostConfiguration(hostconfig) //ADDED LINE
> > httpclient.executeMethod(hostconfig, httpget);
> > 
> > 
> > I looked at the code, and I think the problem comes from lines
> 382-386:
> > 
> > HostConfiguration defaulthostconfig = getHostConfiguration();
> > if (hostConfiguration == null || hostConfiguration ==
> defaulthostconfig)
> > {
> >     // make a deep copy of the host defaults
> >     hostConfiguration = new HostConfiguration(defaulthostconfig);
> >     URI uri = method.getURI(); 
> >     if (uri.isAbsoluteURI()) {
> >         hostConfiguration.setHost(uri);
> >     }
> > }
> > 
> > Having the configuration set at the client level makes it enter the if
> > statement and clone the config.
> > But the call to 
> > hostConfiguration.setHost(uri); 
> > overwrites the hostConfiguration.protocol with the default protocol
> > registered (Protocol.getProtocol(uri.getScheme())).
> > 
> > Thanks for your feedback.
> > 
> > Francois
> > 
> > 
> > -----Original Message-----
> > From: Riad Souissi [mailto:rsouissi.c@stc.com.sa] 
> > Sent: lundi 17 janvier 2005 13:20
> > To: HttpClient User Discussion
> > Subject: RE: Problem with PostMethod.getHostConfiguration().setHost()
> on
> > v3 beta1
> > 
> > 
> > Thanks, works exactly as expected!
> > 
> > Regards,
> > riad
> > 
> > 
> > -----Original Message-----
> > From: Oleg Kalnichevski [mailto:olegk@apache.org]
> > 
> > Sent: Monday, January 17, 2005 2:13 PM
> > To: httpclient-user@jakarta.apache.org
> > Subject: Re: Problem with PostMethod.getHostConfiguration().setHost()
> on
> > v3 beta1
> > 
> > On Mon, Jan 17, 2005 at 01:14:34PM +0300, Riad Souissi wrote:
> > >
> > 
> > >
> > 
> > >
> > 
> > > Hi,
> > >
> > 
> > > Before, using version 2 or version3_alpha2, the following code was
> > > working:
> > >
> > 
> > > ----
> > > myPostMethod.getHostConfiguration().setHost("somehost", "15555",
> > > mySslProtocolObject)
> > > ----
> > >
> > 
> > > The reason I was setting mySslProtocolObject on the method level is
> > 
> > > because I do not want to force it on the whole host:port. I need to
> > 
> > > have a granular control.
> > >
> > 
> > > Then I switched to version3_beta1, and it stopped to work. It does
> not
> > 
> > > seem to use any of the SSL classes defined within
> mySslProtocolObject
> > 
> > > (the object which I derived from ProtocolSocketFactory). I used
> > 
> > > println calls to find out that sslProtocolObject was not used when I
> > 
> > > was sending an HTTPS request to host:port using the myPostMethod
> > defined above.
> > >
> > 
> > > Do you have an idea how to get it back to work ? I know that
> > > getHostConfiguration().setHost() is deprecated, but I found no other
> > 
> > > way to do the same thing with this granularity (at the PostMethod
> > level).
> > >
> > 
> > Hi Riad,
> > 
> > Try this
> > 
> > HostConfiguration hostconfig = new HostConfiguration("somehost",
> 15555,
> >   MySslProtocolObject);
> > HttpClient httpclient = new HttpClient(); ...
> > 
> > httpclient.executeMethod(hostconfig, httpget);
> >  
> > 
> > Hope this helps
> > 
> > Oleg
> > 
> > > Regards,
> > > riad
> > >
> > 
> > > DiscalimerThe information in this email and in any files transmitted
> > 
> > > with it, is intended only for the addressee and may contain
> > confidential and/or privileged material.
> > > Access to this email by anyone else is unauthorized. If you receive
> > 
> > > this in error, please contact the sender immediately and delete the
> > material from any computer.
> > > If you are not the intended recipient, any disclosure, copying,
> > 
> > > distribution or any action taken or omitted to be taken in reliance
> on
> > it, is strictly prohibited.
> > > Statement and opinions expressed in this e-mail are those of the
> > 
> > > sender, and do not necessarily reflect those of STC.
> > >
> > 
> > >
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> httpclient-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail:
> > 
> > > httpclient-user-help@jakarta.apache.org
> > >
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> httpclient-user-help@jakarta.apache.org
> > 
> > 
> > DiscalimerThe information in this email and in any files transmitted
> > with it,
> > is intended only for the addressee and may contain confidential and/or
> > privileged material.
> > Access to this email by anyone else is unauthorized. If you receive
> this
> > in error,
> > please contact the sender immediately and delete the material from any
> > computer.
> > If you are not the intended recipient, any disclosure, copying,
> > distribution or
> > any action taken or omitted to be taken in reliance on it, is strictly
> > prohibited.
> > Statement and opinions expressed in this e-mail are those of the
> sender,
> > and do not
> > necessarily reflect those of STC.
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> httpclient-user-help@jakarta.apache.org
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 

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