ConnectMethod usage

View: New views
6 Messages — Rating Filter:   Alert me  

ConnectMethod usage

by Mike Cumings :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello all,

I've got a question as to the usage of the ConnectMethod.  From the API,
it appears that the only non-deprecated way to create a HTTP CONNECT
connection is to construct a ConnectMethod instance, passing in a
HostConfiguration object configured for the target/destination host and port.
I've modelled this in the test case below:

    public void testConnect() throws IOException {
        this.server.setHttpService(new EchoService());
        // Set default host
        this.client.getHostConfiguration().setHost(
                this.server.getLocalAddress(),
                this.server.getLocalPort(),
                Protocol.getProtocol("http"));

        HostConfiguration hConf = new HostConfiguration();
        hConf.setHost(
                this.server.getLocalAddress(),
                this.server.getLocalPort());
        ConnectMethod conn = new ConnectMethod(hConf);
        try {
            this.client.executeMethod(conn);
            assertEquals(HttpStatus.SC_OK, conn.getStatusCode());
        } finally {
            conn.releaseConnection();
        }
    }

At runtime the above fails with the following stack dump:

     [java] 1) testConnect(org.apache.commons.httpclient.TestConnectMethod)java.lang.IllegalStateException:
unsupported protocol: 'localhost'
     [java]     at
org.apache.commons.httpclient.protocol.Protocol.lazyRegisterProtocol(Unknown
Source)
     [java]     at
org.apache.commons.httpclient.protocol.Protocol.getProtocol(Unknown
Source)
     [java]     at
org.apache.commons.httpclient.HostConfiguration.setHost(Unknown
Source)
     [java]     at
org.apache.commons.httpclient.HostConfiguration.setHost(Unknown
Source)
     [java]     at
org.apache.commons.httpclient.HttpClient.executeMethod(Unknown Source)
     [java]     at
org.apache.commons.httpclient.HttpClient.executeMethod(Unknown Source)
     [java]     at
org.apache.commons.httpclient.TestConnectMethod.testConnect(Unknown
Source)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     [java]     at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

Basically, the hostname:port is being interpreted as an absolute URI
such as "localhost:1234"
where the scheme is "localhost" and the path is "1234", which then
breaks in HttpClient.executeMethod(...):

        URI uri = method.getURI();
        if (hostconfig == defaulthostconfig || uri.isAbsoluteURI()) {
            // make a deep copy of the host defaults
            hostconfig = (HostConfiguration) hostconfig.clone();
            if (uri.isAbsoluteURI()) {
                hostconfig.setHost(uri);
            }
        }

So my question is whether this is a bug or operator error?  Thanks in advance,

--
Mike Cumings

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@...
For additional commands, e-mail: httpclient-users-help@...


Re: ConnectMethod usage

by olegk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, 2008-07-03 at 15:12 -0700, Mike Cumings wrote:

> Hello all,
>
> I've got a question as to the usage of the ConnectMethod.  From the API,
> it appears that the only non-deprecated way to create a HTTP CONNECT
> connection is to construct a ConnectMethod instance, passing in a
> HostConfiguration object configured for the target/destination host and port.
> I've modelled this in the test case below:
>
>     public void testConnect() throws IOException {
>         this.server.setHttpService(new EchoService());
>         // Set default host
>         this.client.getHostConfiguration().setHost(
>                 this.server.getLocalAddress(),
>                 this.server.getLocalPort(),
>                 Protocol.getProtocol("http"));
>
>         HostConfiguration hConf = new HostConfiguration();
>         hConf.setHost(
>                 this.server.getLocalAddress(),
>                 this.server.getLocalPort());
>         ConnectMethod conn = new ConnectMethod(hConf);
>         try {
>             this.client.executeMethod(conn);
>             assertEquals(HttpStatus.SC_OK, conn.getStatusCode());
>         } finally {
>             conn.releaseConnection();
>         }
>     }
...
>
> So my question is whether this is a bug or operator error?  Thanks in advance,
>

Mike,

What is it exactly you are trying to achieve? CONNECT method is
supposed to be used  for one thing and one thing only: to establish a
tunnel through an HTTP proxy server. Why are you trying to execute this
method as an ordinary HTTP request?

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@...
For additional commands, e-mail: httpclient-users-help@...


Re: ConnectMethod usage

by Mike Cumings :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Oleg,

I'm well ware of what HTTP CONNECT is used for and how it works.  When I added
the explicit protocol selection in the snippet of code previously sent
out, I was
just checking to see if the scheme would e inherited from the host
configuration.
I had meant to cut that code out prior to sending out my email but it
looks like I
had forgotten do do that..

I'm just trying to get an HTTP CONNECT to work through an upstream proxy
to a site passed to my code in the form of a hostname and port authority
specification.  It seems like it's getting tripped up in httpclient
when the authority
specification is being parsed/interpreted as a URI.

Thanks in advance,

Mike

On Sat, Jul 5, 2008 at 5:02 AM, Oleg Kalnichevski <olegk@...> wrote:

> On Thu, 2008-07-03 at 15:12 -0700, Mike Cumings wrote:
>> Hello all,
>>
>> I've got a question as to the usage of the ConnectMethod.  From the API,
>> it appears that the only non-deprecated way to create a HTTP CONNECT
>> connection is to construct a ConnectMethod instance, passing in a
>> HostConfiguration object configured for the target/destination host and port.
>> I've modelled this in the test case below:
>>
>>     public void testConnect() throws IOException {
>>         this.server.setHttpService(new EchoService());
>>         // Set default host
>>         this.client.getHostConfiguration().setHost(
>>                 this.server.getLocalAddress(),
>>                 this.server.getLocalPort(),
>>                 Protocol.getProtocol("http"));
>>
>>         HostConfiguration hConf = new HostConfiguration();
>>         hConf.setHost(
>>                 this.server.getLocalAddress(),
>>                 this.server.getLocalPort());
>>         ConnectMethod conn = new ConnectMethod(hConf);
>>         try {
>>             this.client.executeMethod(conn);
>>             assertEquals(HttpStatus.SC_OK, conn.getStatusCode());
>>         } finally {
>>             conn.releaseConnection();
>>         }
>>     }
> ...
>>
>> So my question is whether this is a bug or operator error?  Thanks in advance,
>>
>
> Mike,
>
> What is it exactly you are trying to achieve? CONNECT method is
> supposed to be used  for one thing and one thing only: to establish a
> tunnel through an HTTP proxy server. Why are you trying to execute this
> method as an ordinary HTTP request?
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@...
> For additional commands, e-mail: httpclient-users-help@...
>
>



--
Mike Cumings

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@...
For additional commands, e-mail: httpclient-users-help@...


Re: ConnectMethod usage

by olegk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, 2008-07-05 at 12:56 -0700, Mike Cumings wrote:

> Hi Oleg,
>
> I'm well ware of what HTTP CONNECT is used for and how it works.  When I added
> the explicit protocol selection in the snippet of code previously sent
> out, I was
> just checking to see if the scheme would e inherited from the host
> configuration.
> I had meant to cut that code out prior to sending out my email but it
> looks like I
> had forgotten do do that..
>
> I'm just trying to get an HTTP CONNECT to work through an upstream proxy
> to a site passed to my code in the form of a hostname and port authority
> specification.  It seems like it's getting tripped up in httpclient
> when the authority
> specification is being parsed/interpreted as a URI.
>
> Thanks in advance,
>
> Mike
>

Mike,

Why don't you just use ProxyClient for that?

http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/examples/ProxyTunnelDemo.java

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@...
For additional commands, e-mail: httpclient-users-help@...


Re: ConnectMethod usage

by Mike Cumings :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Oleg,

Perhaps my last response was a bit misleading.  I'm not dealing exclusively with
HTTP CONNECTs.  This is a special purpose proxy which must handle multiple
HTTP methods.  I could use ProxyClient as a special case within the codebase
but would prefer to be able to leverage HttpClient.executeMethod(...)
to eliminate
special handling in the code.  The API suggests that this is possible since
executeMethod(...)  takes an HttpMethod argument, of which ConnectMethod
is an extension.  Is this a bad assumption?  If not, what in my code needs to
change to be able to successfully establish an HTTP CONNECT using
ConnectMethod via HttpClient? Thanks again for the assistance,

Mike

On Sat, Jul 5, 2008 at 4:14 PM, Oleg Kalnichevski <olegk@...> wrote:

> On Sat, 2008-07-05 at 12:56 -0700, Mike Cumings wrote:
>> Hi Oleg,
>>
>> I'm well ware of what HTTP CONNECT is used for and how it works.  When I added
>> the explicit protocol selection in the snippet of code previously sent
>> out, I was
>> just checking to see if the scheme would e inherited from the host
>> configuration.
>> I had meant to cut that code out prior to sending out my email but it
>> looks like I
>> had forgotten do do that..
>>
>> I'm just trying to get an HTTP CONNECT to work through an upstream proxy
>> to a site passed to my code in the form of a hostname and port authority
>> specification.  It seems like it's getting tripped up in httpclient
>> when the authority
>> specification is being parsed/interpreted as a URI.
>>
>> Thanks in advance,
>>
>> Mike
>>
>
> Mike,
>
> Why don't you just use ProxyClient for that?
>
> http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/examples/ProxyTunnelDemo.java
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@...
> For additional commands, e-mail: httpclient-users-help@...
>
>



--
Mike Cumings

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@...
For additional commands, e-mail: httpclient-users-help@...


Re: ConnectMethod usage

by olegk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, 2008-07-06 at 14:00 -0700, Mike Cumings wrote:

> Oleg,
>
> Perhaps my last response was a bit misleading.  I'm not dealing exclusively with
> HTTP CONNECTs.  This is a special purpose proxy which must handle multiple
> HTTP methods.  I could use ProxyClient as a special case within the codebase
> but would prefer to be able to leverage HttpClient.executeMethod(...)
> to eliminate
> special handling in the code.  The API suggests that this is possible since
> executeMethod(...)  takes an HttpMethod argument, of which ConnectMethod
> is an extension.  Is this a bad assumption?  If not, what in my code needs to
> change to be able to successfully establish an HTTP CONNECT using
> ConnectMethod via HttpClient? Thanks again for the assistance,
>

Mike,

I do not think this can be done with HttpClient 3.1. HTTP CONNECT method
has a different semantic than all other methods.  You should consider
building a custom HTTP client using components of HttpClient 4.0. The
4.0 API is significantly more modular and flexible than its predecessor.

Oleg  



> Mike
>
> On Sat, Jul 5, 2008 at 4:14 PM, Oleg Kalnichevski <olegk@...> wrote:
> > On Sat, 2008-07-05 at 12:56 -0700, Mike Cumings wrote:
> >> Hi Oleg,
> >>
> >> I'm well ware of what HTTP CONNECT is used for and how it works.  When I added
> >> the explicit protocol selection in the snippet of code previously sent
> >> out, I was
> >> just checking to see if the scheme would e inherited from the host
> >> configuration.
> >> I had meant to cut that code out prior to sending out my email but it
> >> looks like I
> >> had forgotten do do that..
> >>
> >> I'm just trying to get an HTTP CONNECT to work through an upstream proxy
> >> to a site passed to my code in the form of a hostname and port authority
> >> specification.  It seems like it's getting tripped up in httpclient
> >> when the authority
> >> specification is being parsed/interpreted as a URI.
> >>
> >> Thanks in advance,
> >>
> >> Mike
> >>
> >
> > Mike,
> >
> > Why don't you just use ProxyClient for that?
> >
> > http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/examples/ProxyTunnelDemo.java
> >
> > Oleg
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@...
> > For additional commands, e-mail: httpclient-users-help@...
> >
> >
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@...
For additional commands, e-mail: httpclient-users-help@...

LightInTheBox - Buy quality products at wholesale price