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 Eugeny N Dzhurinsky <bo...@redwerk.com> on 2006/07/05 13:36:39 UTC

managing cookies in threads

Hello!

I using MultiThreadedHttpConnectionManager to manage connections in several
threads. In general, I'm using single instance of
MultiThreadedHttpConnectionManager for all threads which are using HttpClient.

private static MultiThreadedHttpConnectionManager connectionManager;

static {                                                                                                             
        connectionManager = new MultiThreadedHttpConnectionManager();                                                    
        HttpConnectionManagerParams params = connectionManager.getParams();                                              
        params.setMaxTotalConnections(1000);                                                                             
        params.setStaleCheckingEnabled(true);                                                                            
        params.setConnectionTimeout(CONNECTION_TIMEOUT);                                                                 
        params.setSoTimeout(CONNECTION_TIMEOUT);                                                                         
        params.setBooleanParameter("http.protocol.single-cookie-header", false);                                         
        Protocol.registerProtocol("https", new Protocol("https",                                                         
                new SelfSignedSSLSocketFactory(), 443));                                                                 
    }

My question is: when some URI is handled, a cookie could be set, will that
cookie be used when creating new HttpClient instance with 
new HttpClient(connectionManager)?

If no, how can I implement this, so HttpClient will collect all cookies when
traversing a site?

-- 
Eugene N Dzhurinsky

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


Re: are refs allowed in HTTP POST URIs?

Posted by Ole Matzura <ol...@eviware.com>.
Hi Roland,

thanks again for your answers.. it seems we worked around the issue by 
preencoding the URL.. (since we needed the '#' to be in the URL)

regards!

/Ole

Roland Weber skrev:
> Hello Ole,
>
>   
>> contains a ref which is removed by the httpclient URI 
>> class.. 
>>     
>
> It is not removed by the URI class. It is dropped by the
> HTTP logic in HttpMethodBase.
>
>   
>> is that "correct" behaviour?
>>     
>
> Yes, for the HTTP logic. If it was the URI class that
> dropped the reference, that would be a bug.
>
>   
>> are refs "invalid" in a URI?
>>     
>
> No.
>
>   
>> (The 
>> java.net.URL class handles refs correctly)..
>>     
>
> And so does the URI class. The java.net.HttpURLConnection
> will drop the reference when generating a request.
>
>   
>> Should I tell him not to 
>> use the "#" character in a URI?
>>     
>
> It doesn't matter. As long as he/you are aware that the
> reference will not be sent to the server.
>
> cheers,
>   Roland
>
>
> ---------------------------------------------------------------------
> 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: are refs allowed in HTTP POST URIs?

Posted by Roland Weber <RO...@de.ibm.com>.
Hello Ole,

> contains a ref which is removed by the httpclient URI 
> class.. 

It is not removed by the URI class. It is dropped by the
HTTP logic in HttpMethodBase.

> is that "correct" behaviour?

Yes, for the HTTP logic. If it was the URI class that
dropped the reference, that would be a bug.

> are refs "invalid" in a URI?

No.

> (The 
> java.net.URL class handles refs correctly)..

And so does the URI class. The java.net.HttpURLConnection
will drop the reference when generating a request.

> Should I tell him not to 
> use the "#" character in a URI?

It doesn't matter. As long as he/you are aware that the
reference will not be sent to the server.

cheers,
  Roland


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


Re: are refs allowed in HTTP POST URIs?

Posted by Ole Matzura <ol...@eviware.com>.
Hi Roland,

thanks for your answer.. I get your point but the URL he is using 
never-the-less contains a ref which is removed by the httpclient URI 
class.. is that "correct" behaviour? are refs "invalid" in a URI? (The 
java.net.URL class handles refs correctly).. Should I tell him not to 
use the "#" character in a URI?

thanks again :-)

regards!

/Ole
eviware.com

Roland Weber skrev:
> Hello Ole,
>
>   
>> http://somehost.com/my/path#witharef
>>
>> When using this endpoint as the URI for a PostMethod the ref "#witharef"
>> is removed from the path and never transmitted in the request.. I am
>> guessing that refs are not actually allowed in this context.. am I right
>> or should this be supported?
>>     
>
> Refs are never part of HTTP requests. They are purely client-side, since
> they point _into_ a document. The HTTP request will always be for the
> whole document. Once the document is on the client, the application
> can then look for the part of the document indicated by the reference.
>
> hope that helps,
>   Roland
>
> ---------------------------------------------------------------------
> 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: are refs allowed in HTTP POST URIs?

Posted by Roland Weber <ht...@dubioso.net>.
Hello Ole,

> http://somehost.com/my/path#witharef
> 
> When using this endpoint as the URI for a PostMethod the ref "#witharef"
> is removed from the path and never transmitted in the request.. I am
> guessing that refs are not actually allowed in this context.. am I right
> or should this be supported?

Refs are never part of HTTP requests. They are purely client-side, since
they point _into_ a document. The HTTP request will always be for the
whole document. Once the document is on the client, the application
can then look for the part of the document indicated by the reference.

hope that helps,
  Roland

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


are refs allowed in HTTP POST URIs?

Posted by Ole Matzura <ol...@eviware.com>.
Hi all!

I have a soapui user with a web-service endpoint containing a ref as 
follows;

http://somehost.com/my/path#witharef

When using this endpoint as the URI for a PostMethod the ref "#witharef" 
is removed from the path and never transmitted in the request.. I am 
guessing that refs are not actually allowed in this context.. am I right 
or should this be supported?

regards!

/Ole
eviware.com


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


Re: managing cookies in threads

Posted by Eugeny N Dzhurinsky <bo...@redwerk.com>.
On Thu, Jul 06, 2006 at 08:39:04AM +0200, Roland Weber wrote:
> Eugeny N Dzhurinsky <bo...@redwerk.com> wrote on 06.07.2006 08:31:40:
> 
> > But is it safe to share HttpClient between several threads?
> http://jakarta.apache.org/commons/httpclient/threading.html

OMG, I missed that point, I thought I need to create HttpClient for each
thread!

Thanks, I'll try this!

-- 
Eugene N Dzhurinsky

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


Re: managing cookies in threads

Posted by Roland Weber <RO...@de.ibm.com>.
Eugeny N Dzhurinsky <bo...@redwerk.com> wrote on 06.07.2006 08:31:40:

> But is it safe to share HttpClient between several threads?

http://jakarta.apache.org/commons/httpclient/threading.html


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


Re: managing cookies in threads

Posted by Eugeny N Dzhurinsky <bo...@redwerk.com>.
On Wed, Jul 05, 2006 at 09:51:53PM +0200, Roland Weber wrote:
> > I think I need to collect ALL cookies set by each page somewhere, because
> > implementation of sophisticated cookie management like passign cookie only to
> > children of current processed page will be some overkill for now.
> 
> Then you should be fine with simply using the same HttpClient object from
> all threads, without explicitly passing an HttpState. HttpClient objects
> have a default HttpState object in which all cookies will accumulate. If
> you don't see cookies being sent back with the requests, maybe they were
> rejected by the cookie policy? The default cookie policy is quite strict,
> unlike the browser compatibility one.
> http://jakarta.apache.org/commons/httpclient/cookies.html

But is it safe to share HttpClient between several threads?

-- 
Eugene N Dzhurinsky

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


Re: managing cookies in threads

Posted by Roland Weber <ht...@dubioso.net>.
> I think I need to collect ALL cookies set by each page somewhere, because
> implementation of sophisticated cookie management like passign cookie only to
> children of current processed page will be some overkill for now.

Then you should be fine with simply using the same HttpClient object from
all threads, without explicitly passing an HttpState. HttpClient objects
have a default HttpState object in which all cookies will accumulate. If
you don't see cookies being sent back with the requests, maybe they were
rejected by the cookie policy? The default cookie policy is quite strict,
unlike the browser compatibility one.
http://jakarta.apache.org/commons/httpclient/cookies.html

hope that helps,
  Roland

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


Re: managing cookies in threads

Posted by bo...@redwerk.com.
On Wed, Jul 05, 2006 at 07:58:29PM +0200, Roland Weber wrote:
> > Okay, so I should keep cookies in my own code and pass them to executeMethod
> > either with using HttpState object or with adding cookie directly with
> > addcookie method.
> 
> No. You should let HttpClient handle cookies in HttpState. But your code
> needs to make sure that there is a separate HttpState for each session, or
> each set of cookies you want to maintain.
> 
> > Could you please explain why you are using HostConfiguration
> > and why it needs to be created for each thread?
> 
> The HttpClient method that allows for passing an HttpState also expects
> a HostConfiguration:
> http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/HttpClient.html#executeMethod(org.apache.commons.httpclient.HostConfiguration,%20org.apache.commons.httpclient.HttpMethod,%20org.apache.commons.httpclient.HttpState)
> 
> The HostConfiguration object could get updated during method execution,
> in particular if the method director has to follow a redirect. That's
> why you don't want different threads to share HttpConfiguration objects.

It seems I need to explain my situation, since I feel we are talking about
different things.

I had developed a spider application. This application has 1 main thread
(processor), which maintains URL queue and set of processed URLs.  This
processor thread takes URLs from it's queue, creates consumer thread and
passess url to consumer thread. Consumer thread takes an url, gets a page and
parses it, extracts URLs which are pushed back to queue, and so on.

So in general we're starting with 1 single URL, which could set some cookies
needed by any other URL which will be processed later.

I think I need to collect ALL cookies set by each page somewhere, because
implementation of sophisticated cookie management like passign cookie only to
children of current processed page will be some overkill for now.

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


Re: managing cookies in threads

Posted by Roland Weber <ht...@dubioso.net>.
Hello Jeremy,

> I had to do this:
>  
> Cookie[] cookies = initialState.getCookies();
>  
> instead of:
>  
> Cookie[] cookies = client.getState().getCookies();
>  
> to get the actual cookie for each thread.

Quite right. You should also delete this part:

>   // How does this work?
>   // I need a different state for each thread, each time through.
>   client.setState(initialState);

It doesn't work at all.

> Does the last cookie get set to the default HttpState of the HttpClient
> instance?

If you pass a state object to the execute method,
nothing is taken from or set to the default state.

cheers,
  Roland

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


Re: managing cookies in threads

Posted by Jeremy Hicks <je...@novell.com>.
Okay, I think I figured it out, but let me know if this is wrong. 
 
I had to do this:
 
Cookie[] cookies = initialState.getCookies();
 
instead of:
 
Cookie[] cookies = client.getState().getCookies();
 
to get the actual cookie for each thread.
 
Does the last cookie get set to the default HttpState of the HttpClient
instance?
 
Thanks
 
Jeremy Hicks
Novell, Inc., the leading provider of information solutions
http://www.novell.com 

>>> "Jeremy Hicks" <je...@novell.com> 7/7/2006 9:17 AM >>>
I'm trying to do something very similar. I've got a multi-threaded app
where each thread shares the connection manager and an instance of
HttpClient. Each thread has its own HttpState and HostConfiguration. 

I still am getting back the same SESSIONID (cookie) for each thread.
What am I doing wrong?

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.cookie.*;
import org.apache.commons.httpclient.protocol.*;

public class j3 extends Thread
{
public static void main(String[] args) 
{
  int numberOfThreads = 2;
  
  // Create an HttpClient with the MultiThreadedHttpConnectionManager.
        // This connection manager must be used if more than one
thread
will
        // be using the HttpClient.
  MultiThreadedHttpConnectionManager connectionManager = 
   new MultiThreadedHttpConnectionManager();
        HttpClient client = new HttpClient(connectionManager);
        
        // THREE PROTOCOL OPTIONS:
        
        //Protocol myhttps = new Protocol("https", new
EasySSLProtocolSocketFactory(), 8443);
        //client.getHostConfiguration().setHost("151.155.166.140",
8443, myhttps);
        
        /*
        Finally, you can register your custom protocol as the default
handler for a 
        specific protocol designator (eg: https) by calling the
Protocol.registerProtocol 
        method. You can specify your own protocol designator (such as
'myhttps') if you 
        need to use your custom protocol as well as the default SSL
protocol implementation.

        Protocol.registerProtocol("myhttps", 
         new Protocol("https", new EasySSLProtocolSocketFactory(),
8443));
        GetMethod get = new GetMethod("myhttps://www.whatever.com/");
        */
        
        /*
        If you want this protocol to represent the default SSL
protocol
implementation, 
        simply register it under 'https' designator, which will make
the protocol object 
        take place of the existing one

        Protocol.registerProtocol("https", 
         new Protocol("https", new EasySSLProtocolSocketFactory(),
8443));
        GetMethod get = new GetMethod("https://www.whatever.com/");
  */
        
        // Set the default host/protocol for the methods to connect
to.
        // This value will only be used if the methods are not given
an
absolute URI
        //client.getHostConfiguration().setHost("151.155.166.140",
8080, "http");
                  
        j3[] threads = new j3[2];
        for(int i = 0; i < threads.length; i++) 
        {
            threads[i] = new j3(client, i + 1, numberOfThreads);
        }
        
        // start the threads
        for(int j = 0; j < threads.length; j++) 
        {
            threads[j].start();
        }
}

private HttpClient client;
private HttpState initialState;
private HostConfiguration hostConf;

private int id;
private int numberOfThreads;

private static int threadCounter;

public j3(HttpClient client, int id, int numberOfThreads)
{
  this.client = client;
  this.initialState = new HttpState();
  this.hostConf = new HostConfiguration();
  this.id = id;
  this.numberOfThreads = numberOfThreads;
}

public void run() 
{
  updateThreadCounter(1);
  
  while(threadCounter != numberOfThreads) {}
  
  
  // How does this work?
  // I need a different state for each thread, each time through.
  client.setState(initialState);

//client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
  
  client.setHostConfiguration(hostConf);
  client.getHostConfiguration().setHost("151.155.166.140", 8080,
"http");
  
  System.out.println("Starting thread " + id);
  
  GetMethod get = new GetMethod("/nidp/app/");
  get.setFollowRedirects(true);
  get.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
  
  try 
  {
   //client.executeMethod(get);
   client.executeMethod(hostConf, get, initialState);
   //GetMethod httpget = new GetMethod("/stuff");
         //try {
             //this.httpclient.executeMethod(
               //this.hostconfig, httpget, this.httpstate);
   
   
   byte[] bytes = get.getResponseBody();
            
            //System.out.println(id + " - " + bytes.length + " bytes
read");
            
            //System.out.println(get.getResponseBodyAsString());
            
            Cookie[] cookies = client.getState().getCookies();
            
            // Display the cookies
            // I'm getting the same JSESSIONID for each thread!!!
            System.out.println(id + " - Present cookies: ");
            for (int i = 0; i < cookies.length; i++) 
            {
                System.out.println(id + " - " +
cookies[i].toExternalForm());
            }
  }
  
  catch(Exception e) 
  {
   System.out.println(id + " - error: " + e);
  }
  
  finally 
  {
   get.releaseConnection();
   System.out.println(id + " - connection released");
  }
}

private synchronized void updateThreadCounter(int n)
{
  threadCounter+= n;
}
}

Jeremy Hicks
Novell, Inc., the leading provider of information solutions
http://www.novell.com ( http://www.novell.com/ ) 

>>> Roland Weber <ht...@dubioso.net> 7/5/2006 11:58 AM >>>
> Okay, so I should keep cookies in my own code and pass them to
executeMethod
> either with using HttpState object or with adding cookie directly
with
> addcookie method.

No. You should let HttpClient handle cookies in HttpState. But your
code
needs to make sure that there is a separate HttpState for each
session,
or
each set of cookies you want to maintain.

> Could you please explain why you are using HostConfiguration
> and why it needs to be created for each thread?

The HttpClient method that allows for passing an HttpState also
expects
a HostConfiguration:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/HttpClient.html#executeMethod(org.apache.commons.httpclient.HostConfiguration,%20org.apache.commons.httpclient.HttpMethod,%20org.apache.commons.httpclient.HttpState)

The HostConfiguration object could get updated during method
execution,
in particular if the method director has to follow a redirect. That's
why you don't want different threads to share HttpConfiguration
objects.

hope that helps,
  Roland

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org


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


Re: managing cookies in threads

Posted by Jeremy Hicks <je...@novell.com>.
I'm trying to do something very similar. I've got a multi-threaded app
where each thread shares the connection manager and an instance of
HttpClient. Each thread has its own HttpState and HostConfiguration. 
 
I still am getting back the same SESSIONID (cookie) for each thread.
What am I doing wrong?
 
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.cookie.*;
import org.apache.commons.httpclient.protocol.*;
 
public class j3 extends Thread
{
 public static void main(String[] args) 
 {
  int numberOfThreads = 2;
  
  // Create an HttpClient with the MultiThreadedHttpConnectionManager.
        // This connection manager must be used if more than one thread
will
        // be using the HttpClient.
  MultiThreadedHttpConnectionManager connectionManager = 
   new MultiThreadedHttpConnectionManager();
        HttpClient client = new HttpClient(connectionManager);
        
        // THREE PROTOCOL OPTIONS:
        
        //Protocol myhttps = new Protocol("https", new
EasySSLProtocolSocketFactory(), 8443);
        //client.getHostConfiguration().setHost("151.155.166.140",
8443, myhttps);
        
        /*
        Finally, you can register your custom protocol as the default
handler for a 
        specific protocol designator (eg: https) by calling the
Protocol.registerProtocol 
        method. You can specify your own protocol designator (such as
'myhttps') if you 
        need to use your custom protocol as well as the default SSL
protocol implementation.
 
        Protocol.registerProtocol("myhttps", 
         new Protocol("https", new EasySSLProtocolSocketFactory(),
8443));
        GetMethod get = new GetMethod("myhttps://www.whatever.com/");
        */
        
        /*
        If you want this protocol to represent the default SSL protocol
implementation, 
        simply register it under 'https' designator, which will make
the protocol object 
        take place of the existing one
 
        Protocol.registerProtocol("https", 
         new Protocol("https", new EasySSLProtocolSocketFactory(),
8443));
        GetMethod get = new GetMethod("https://www.whatever.com/");
  */
        
        // Set the default host/protocol for the methods to connect
to.
        // This value will only be used if the methods are not given an
absolute URI
        //client.getHostConfiguration().setHost("151.155.166.140",
8080, "http");
                  
        j3[] threads = new j3[2];
        for(int i = 0; i < threads.length; i++) 
        {
            threads[i] = new j3(client, i + 1, numberOfThreads);
        }
        
        // start the threads
        for(int j = 0; j < threads.length; j++) 
        {
            threads[j].start();
        }
 }
 
 private HttpClient client;
 private HttpState initialState;
 private HostConfiguration hostConf;
 
 private int id;
 private int numberOfThreads;
 
 private static int threadCounter;
 
 public j3(HttpClient client, int id, int numberOfThreads)
 {
  this.client = client;
  this.initialState = new HttpState();
  this.hostConf = new HostConfiguration();
  this.id = id;
  this.numberOfThreads = numberOfThreads;
 }
 
 public void run() 
 {
  updateThreadCounter(1);
  
  while(threadCounter != numberOfThreads) {}
  
  
  // How does this work?
  // I need a different state for each thread, each time through.
  client.setState(initialState);
 
//client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
  
  client.setHostConfiguration(hostConf);
  client.getHostConfiguration().setHost("151.155.166.140", 8080,
"http");
  
  System.out.println("Starting thread " + id);
  
  GetMethod get = new GetMethod("/nidp/app/");
  get.setFollowRedirects(true);
  get.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
  
  try 
  {
   //client.executeMethod(get);
   client.executeMethod(hostConf, get, initialState);
   //GetMethod httpget = new GetMethod("/stuff");
         //try {
             //this.httpclient.executeMethod(
               //this.hostconfig, httpget, this.httpstate);
   
   
   byte[] bytes = get.getResponseBody();
            
            //System.out.println(id + " - " + bytes.length + " bytes
read");
            
            //System.out.println(get.getResponseBodyAsString());
            
            Cookie[] cookies = client.getState().getCookies();
            
            // Display the cookies
            // I'm getting the same JSESSIONID for each thread!!!
            System.out.println(id + " - Present cookies: ");
            for (int i = 0; i < cookies.length; i++) 
            {
                System.out.println(id + " - " +
cookies[i].toExternalForm());
            }
  }
  
  catch(Exception e) 
  {
   System.out.println(id + " - error: " + e);
  }
  
  finally 
  {
   get.releaseConnection();
   System.out.println(id + " - connection released");
  }
 }
 
 private synchronized void updateThreadCounter(int n)
 {
  threadCounter+= n;
 }
}
 
Jeremy Hicks
Novell, Inc., the leading provider of information solutions
http://www.novell.com 

>>> Roland Weber <ht...@dubioso.net> 7/5/2006 11:58 AM >>>
> Okay, so I should keep cookies in my own code and pass them to
executeMethod
> either with using HttpState object or with adding cookie directly
with
> addcookie method.

No. You should let HttpClient handle cookies in HttpState. But your
code
needs to make sure that there is a separate HttpState for each session,
or
each set of cookies you want to maintain.

> Could you please explain why you are using HostConfiguration
> and why it needs to be created for each thread?

The HttpClient method that allows for passing an HttpState also
expects
a HostConfiguration:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/HttpClient.html#executeMethod(org.apache.commons.httpclient.HostConfiguration,%20org.apache.commons.httpclient.HttpMethod,%20org.apache.commons.httpclient.HttpState)

The HostConfiguration object could get updated during method
execution,
in particular if the method director has to follow a redirect. That's
why you don't want different threads to share HttpConfiguration
objects.

hope that helps,
  Roland

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org

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


Re: managing cookies in threads

Posted by Roland Weber <ht...@dubioso.net>.
> Okay, so I should keep cookies in my own code and pass them to executeMethod
> either with using HttpState object or with adding cookie directly with
> addcookie method.

No. You should let HttpClient handle cookies in HttpState. But your code
needs to make sure that there is a separate HttpState for each session, or
each set of cookies you want to maintain.

> Could you please explain why you are using HostConfiguration
> and why it needs to be created for each thread?

The HttpClient method that allows for passing an HttpState also expects
a HostConfiguration:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/HttpClient.html#executeMethod(org.apache.commons.httpclient.HostConfiguration,%20org.apache.commons.httpclient.HttpMethod,%20org.apache.commons.httpclient.HttpState)

The HostConfiguration object could get updated during method execution,
in particular if the method director has to follow a redirect. That's
why you don't want different threads to share HttpConfiguration objects.

hope that helps,
  Roland

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


Re: managing cookies in threads

Posted by bo...@redwerk.com.
On Wed, Jul 05, 2006 at 12:56:09PM -0400, Steve Terrell wrote:
> I have successfully managed cookies in threads. Your code shows setting
> the cookie policy, but not setting any actual cookies.
> 
> I would suggest creating a new HostConfiguration and HttpState for every
> executeMethod call, and use HttpClient.executeMethod(HostConfiguration,
> GetMethod, HttpState). The cookies are stored in and retrieved from
> HttpState. You will need to manage the storage of the cookies locally,
> including how to retrieve the proper cookies to be used in the current
> thread.

Okay, so I should keep cookies in my own code and pass them to executeMethod
either with using HttpState object or with adding cookie directly with
addcookie method. Could you please explain why you are using HostConfiguration
and why it needs to be created for each thread?

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


RE: managing cookies in threads

Posted by Steve Terrell <St...@guideworkstv.com>.
I have successfully managed cookies in threads. Your code shows setting
the cookie policy, but not setting any actual cookies.

I would suggest creating a new HostConfiguration and HttpState for every
executeMethod call, and use HttpClient.executeMethod(HostConfiguration,
GetMethod, HttpState). The cookies are stored in and retrieved from
HttpState. You will need to manage the storage of the cookies locally,
including how to retrieve the proper cookies to be used in the current
thread.

--Steve

-----Original Message-----
From: Eugeny N Dzhurinsky [mailto:bofh@redwerk.com] 
Sent: Wednesday, July 05, 2006 11:52 AM
To: httpclient-user@jakarta.apache.org
Subject: Re: managing cookies in threads

After performing some tests, I found cookies are not set back to server
at all
for some reason. I wire logs i can see something like

DEBUG 05/07/06 06:07:39 [Thread-3] (Wire:69) - << "Set-Cookie:
ASPSESSIONIDQSCQASSS=IFLMCPNBLHAKEFLOMPPCOMCM; path=/[\r][\n]"

And I assume this cookie is returned, but I can't see this cookie
is sent to server.

Code to retrieve page looks like:

get = new GetMethod();
get.setURI(uri);
get.setFollowRedirects(true);
HttpMethodParams p = get.getParams();
p.setVersion(new HttpVersion(1, 1));
p.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
http.executeMethod(get);

Do I miss something?
-- 
Eugene N Dzhurinsky

---------------------------------------------------------------------
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: managing cookies in threads

Posted by Eugeny N Dzhurinsky <bo...@redwerk.com>.
After performing some tests, I found cookies are not set back to server at all
for some reason. I wire logs i can see something like

DEBUG 05/07/06 06:07:39 [Thread-3] (Wire:69) - << "Set-Cookie:
ASPSESSIONIDQSCQASSS=IFLMCPNBLHAKEFLOMPPCOMCM; path=/[\r][\n]"

And I assume this cookie is returned, but I can't see this cookie
is sent to server.

Code to retrieve page looks like:

get = new GetMethod();
get.setURI(uri);
get.setFollowRedirects(true);
HttpMethodParams p = get.getParams();
p.setVersion(new HttpVersion(1, 1));
p.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
http.executeMethod(get);

Do I miss something?
-- 
Eugene N Dzhurinsky

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