You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Yue Luo <yu...@sun.com> on 2003/08/25 18:27:24 UTC

question about HttpState

Hi,
   I want to do the following:
1. The main thread/conneciton logins to a web site.  It is a post 
request like a online banking login, not HTTP authentication.  The 
server will return a cookie containing session id.
2. Then the main thread creates two simultaneous threads to create two 
connections to send two queries to the web site. Of course the cookie 
needs to be sent with the get request.

My questions are:
1. Can the two subsequent threads/connections use the HttpState from the 
main thread/connection directly?  Or, should I clone two HttpState for 
the two sub-threads?  Does the clone() method of HttpState work properly?

2. Do HttpState.addCookies() and HttpState.getCookies() create COPIES 
of the cookies or work on the cookie objects directly?

3. This question is somewhat related to the above one.
client.executeMethod(method1);
Cookies[] cookie1=state.getState().getCooies();
client.executeMethod(method2);
Cookies[] cookie2=state.getState().getCooies();
If the server changes some cookies in the second method, will the cookie 
objects in cookie1[] be changed?

Thanks.

Yue


Re: question about HttpState

Posted by Michael Becke <be...@u.washington.edu>.
Hi Yue,

Just a few additions to what Adrian posted.

On Monday, August 25, 2003, at 12:27 PM, Yue Luo wrote:
> 2. Do HttpState.addCookies() and HttpState.getCookies() create COPIES 
> of the cookies or work on the cookie objects directly?

HttpState uses the instances of Cookie that it is given.  No copies are 
made.

> 3. This question is somewhat related to the above one.
> client.executeMethod(method1);
> Cookies[] cookie1=state.getState().getCooies();
> client.executeMethod(method2);
> Cookies[] cookie2=state.getState().getCooies();
> If the server changes some cookies in the second method, will the 
> cookie objects in cookie1[] be changed?

Yes, any changes to HttpState, including cookies, will be seen by all 
of the methods using it.

Mike


Re: question about HttpState

Posted by Adrian Sutton <ad...@intencha.com>.
On 26/08/2003 2:27 AM, "Yue Luo" <yu...@sun.com> wrote:

> Hi,
>  I want to do the following:
> 1. The main thread/conneciton logins to a web site.  It is a post
> request like a online banking login, not HTTP authentication.  The
> server will return a cookie containing session id.
> 2. Then the main thread creates two simultaneous threads to create two
> connections to send two queries to the web site. Of course the cookie
> needs to be sent with the get request.
> 
> My questions are:
> 1. Can the two subsequent threads/connections use the HttpState from the
> main thread/connection directly?  Or, should I clone two HttpState for
> the two sub-threads?  Does the clone() method of HttpState work properly?

Yes, you should use 1 instance of HttpClient and make sure you are using the
MultiThreadedHttpConnectionManager.  So something like:

HttpClient client = new HttpClient(new
MultiThreadedHttpConnectionManager());
PostMethod login = new PostMethod(...);
client.execute(login);
login.releaseConnection();

Create as many threads as you like to do something like:
GetMethod get = new GetMethod(...);
client.execute(get);
get.releaseConnection();

You should probably take a look at the multithreading guide:
http://jakarta.apache.org/commons/httpclient/threading.html

> 2. Do HttpState.addCookies() and HttpState.getCookies() create COPIES
> of the cookies or work on the cookie objects directly?
> 
> 3. This question is somewhat related to the above one.
> client.executeMethod(method1);
> Cookies[] cookie1=state.getState().getCooies();
> client.executeMethod(method2);
> Cookies[] cookie2=state.getState().getCooies();
> If the server changes some cookies in the second method, will the cookie
> objects in cookie1[] be changed?

On this I couldn't be entirely certain and don't have time to analyse the
code right now, though you could certainly do it yourself or even easier,
just test it and find out. :)

> Yue

Regards,

Adrian Sutton.
----------------------------------------------
Intencha "tomorrow's technology today"
Ph: 38478913 0422236329
Suite 8/29 Oatland Crescent
Holland Park West 4121
Australia QLD
www.intencha.com