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 Wangbo126 <wa...@hotmail.com> on 2011/06/18 07:49:06 UTC

How to set cutomized header for httpclient proxy?

Hello,

I am trying to use httpclient 3.x or 4.x to communicate to a target host
through a proxy. The proxy requires a specific header to establish the
tunnel. How could I pass the required header to the proxy through
httpclient? I know we can set headers to post/get method to the target host.
But I am not sure we can set headers for proxy. I went through the internet
and cannot find any document on this.

Thank you very much for any suggestion.
Bo



-- 
View this message in context: http://old.nabble.com/How-to-set-cutomized-header-for-httpclient-proxy--tp31873710p31873710.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


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


Re: How to set cutomized header for httpclient proxy?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Sun, 2011-06-19 at 21:32 +0200, Bart Robeyns wrote:
> There's an example of how to tunnel with 4.1, somewhat hidden away at
> http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/conn/ManagerConnectProxy.java.
> At lines 106-107 a CONNECT-request is build and a header is added to it.
> 
>             HttpRequest connect = new BasicHttpRequest("CONNECT", authority,
> HttpVersion.HTTP_1_1);
>             connect.addHeader("Host", authority);
> 
> I believe this allows you to add your own headers to the connect method.
> 
> Note that 4.x is quite different from 3.x;  the set of ...Method-classes no
> longer exist, and has been replaced by HttpGet, HttpPost, ... The 3.x
> ConnectMethod has no counterpart in 4.x - but you can easily build it
> yourself as the example shows.
> 

With 4.x API the easiest way to add a header or a set of headers to all
outgoing requests is by using a cuastom protocol interceptor

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#protocol_interceptors

Oleg



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


Re: How to set cutomized header for httpclient proxy?

Posted by Bart Robeyns <ba...@gmail.com>.
There's an example of how to tunnel with 4.1, somewhat hidden away at
http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/conn/ManagerConnectProxy.java.
At lines 106-107 a CONNECT-request is build and a header is added to it.

            HttpRequest connect = new BasicHttpRequest("CONNECT", authority,
HttpVersion.HTTP_1_1);
            connect.addHeader("Host", authority);

I believe this allows you to add your own headers to the connect method.

Note that 4.x is quite different from 3.x;  the set of ...Method-classes no
longer exist, and has been replaced by HttpGet, HttpPost, ... The 3.x
ConnectMethod has no counterpart in 4.x - but you can easily build it
yourself as the example shows.

On Sun, Jun 19, 2011 at 9:27 AM, Wangbo126 <wa...@hotmail.com> wrote:

>
> Thank you, Bart. I am using httpclient 3.0. I ran debugger to trace into
> HttpMethodDirector class. The proxy tunnel is created in executeConnect()
> method. At the beginning of the method (line 475 in
> HttpMethodDirector.java), an instance of ConnectMethod class is created to
> build the tunnel. The headers of get method to the target host are not
> copied to the instance of ConnectMethod. So during the creationg of the
> tunnel, the proxy cannot see the request headers. After the tunnel is set
> up, the headers of get method to the target host is sent to the proxy as
> general data.
>
> The header I need to pass to the proxy is used to create the tunnel. From
> the source code, it seems 3.0 does not have such a capability. There are
> lots of changes in 4.0. Do you know httpclient4.x has this capability to
> pass a header to proxy to create the tunnel?
>
> Thanks,
> Bo
>
>
> Bart Robeyns wrote:
> >
> > The proxy also sees all request-headers, in the same manner the target
> > host
> > would; there's no "proxy-specific header"-type in HTTP.
> > Simply add your specific header it in the same way you would add a normal
> > request header, and the proxy will pick it up.
> >
> >
> > On Sat, Jun 18, 2011 at 7:49 AM, Wangbo126 <wa...@hotmail.com>
> wrote:
> >
> >>
> >> Hello,
> >>
> >> I am trying to use httpclient 3.x or 4.x to communicate to a target host
> >> through a proxy. The proxy requires a specific header to establish the
> >> tunnel. How could I pass the required header to the proxy through
> >> httpclient? I know we can set headers to post/get method to the target
> >> host.
> >> But I am not sure we can set headers for proxy. I went through the
> >> internet
> >> and cannot find any document on this.
> >>
> >> Thank you very much for any suggestion.
> >> Bo
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >>
> http://old.nabble.com/How-to-set-cutomized-header-for-httpclient-proxy--tp31873710p31873710.html
> >> Sent from the HttpClient-User mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> >> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://old.nabble.com/How-to-set-cutomized-header-for-httpclient-proxy--tp31873710p31878362.html
> Sent from the HttpClient-User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: How to set cutomized header for httpclient proxy?

Posted by Wangbo126 <wa...@hotmail.com>.
Thank you, Bart. I am using httpclient 3.0. I ran debugger to trace into
HttpMethodDirector class. The proxy tunnel is created in executeConnect()
method. At the beginning of the method (line 475 in
HttpMethodDirector.java), an instance of ConnectMethod class is created to
build the tunnel. The headers of get method to the target host are not
copied to the instance of ConnectMethod. So during the creationg of the
tunnel, the proxy cannot see the request headers. After the tunnel is set
up, the headers of get method to the target host is sent to the proxy as
general data. 

The header I need to pass to the proxy is used to create the tunnel. From
the source code, it seems 3.0 does not have such a capability. There are
lots of changes in 4.0. Do you know httpclient4.x has this capability to
pass a header to proxy to create the tunnel?

Thanks,
Bo


Bart Robeyns wrote:
> 
> The proxy also sees all request-headers, in the same manner the target
> host
> would; there's no "proxy-specific header"-type in HTTP.
> Simply add your specific header it in the same way you would add a normal
> request header, and the proxy will pick it up.
> 
> 
> On Sat, Jun 18, 2011 at 7:49 AM, Wangbo126 <wa...@hotmail.com> wrote:
> 
>>
>> Hello,
>>
>> I am trying to use httpclient 3.x or 4.x to communicate to a target host
>> through a proxy. The proxy requires a specific header to establish the
>> tunnel. How could I pass the required header to the proxy through
>> httpclient? I know we can set headers to post/get method to the target
>> host.
>> But I am not sure we can set headers for proxy. I went through the
>> internet
>> and cannot find any document on this.
>>
>> Thank you very much for any suggestion.
>> Bo
>>
>>
>>
>> --
>> View this message in context:
>> http://old.nabble.com/How-to-set-cutomized-header-for-httpclient-proxy--tp31873710p31873710.html
>> Sent from the HttpClient-User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>
>>
> 
> 

-- 
View this message in context: http://old.nabble.com/How-to-set-cutomized-header-for-httpclient-proxy--tp31873710p31878362.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


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


Re: How to set cutomized header for httpclient proxy?

Posted by Bart Robeyns <ba...@gmail.com>.
The proxy also sees all request-headers, in the same manner the target host
would; there's no "proxy-specific header"-type in HTTP.
Simply add your specific header it in the same way you would add a normal
request header, and the proxy will pick it up.


On Sat, Jun 18, 2011 at 7:49 AM, Wangbo126 <wa...@hotmail.com> wrote:

>
> Hello,
>
> I am trying to use httpclient 3.x or 4.x to communicate to a target host
> through a proxy. The proxy requires a specific header to establish the
> tunnel. How could I pass the required header to the proxy through
> httpclient? I know we can set headers to post/get method to the target
> host.
> But I am not sure we can set headers for proxy. I went through the internet
> and cannot find any document on this.
>
> Thank you very much for any suggestion.
> Bo
>
>
>
> --
> View this message in context:
> http://old.nabble.com/How-to-set-cutomized-header-for-httpclient-proxy--tp31873710p31873710.html
> Sent from the HttpClient-User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>