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 <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED]
For additional commands, e-mail:
[EMAIL PROTECTED]