views:

74

answers:

3

I want to create a small java application to copy some wiki content from one server to another. The API is based on the XML-RPC.

Basically I have three methods, login, getPage and putPage. I use Apache HttpClient 3.x and managed to use login to login successfully and getPage to get a page from the old wiki correctly.

Authentication is handled with cookies: I log into the new wiki and some cookies are set on the corresponding httpclient. The doku tells me that one of those cookies is used for authentification.

Then I execute putPage with another POST method on the same httpclient and the server responds with a authentication failure message.

The code sequence goes like this (very reduced):

HttpClient client = new HttpClient();
PostMethod postLogin = createNewPostMethod("login", "user", "pw");
client.executeMethod(postLogin);
// Now I'm logged in and the client definitly has stored the cookies
PostMethod postPutPage = createNewPostMethod("putPage", getPage());
client.executeMethod(postPutPage);  // the server won't let me put the page

Should it work like that or do I have to add the cookies manually to the second post method and, if yes, how?


Edit / Solution

With the help of the answers to this question I was able to identify and solve the problem, which was outside of the usage of httpclient. At the end it was a configuration issue on the target wiki side. The answers here helped me to ask the right questions in another forum.

+3  A: 

Cookies are handled by HTTPClient by default. You shouldn't have to do anything to have cookies work properly.

Source: http://www.innovation.ch/java/HTTPClient/getting_started.html#cookies


Edit for Apache HTTP Client:

Apache HTTP Client behaves the same :-)

Here is the source: http://hc.apache.org/httpclient-3.x/cookies.html

Vivien Barousse
Sorry, just realized, there are too many 'httpclients' around. I'm using the Apache HttpClient, the referenced page made me nervous but it describes a HTTPClient, which is something different. Edited my question to make it a bit clearer.
Andreas_D
I edited my answer to match the Apache HTTP client, but the behaviour is quite the same :-)
Vivien Barousse
+1  A: 

You can set manually cookies with HTTP Client but it will handle correctly cookies created during your connection.

HttpClient supports automatic management of cookies, including allowing the server to set cookies and automatically return them to the server when required. It is also possible to manually set cookies to be sent to the server.


Resources :

Colin Hebert
It says *when required*, how does HttpClient know that the second POST method requires cookies? Or do have some handshake protocol with the server - like the server receives the POST and then asks for cookies?
Andreas_D
When HTTPClient connects to your server I think that the *cookie protocol* will tell (depends on the protocol) if cookies are required. But I must say, I didn't even know that there was RFC on cookies.Anyway, HTTPClient will handle this for you.
Colin Hebert
+1  A: 

I have historically used this when I wanted to accept cookies with HttpClient

postPutPage.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
Joel
Tried that one too with no success - even worse, with this setting my cookies were overwritten/partially deleted after the second `executeMethod` call...
Andreas_D