开发者

httpclient - use cookies with POST message

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 t开发者_StackOverflow中文版o 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.


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


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 :

  • Apache HttpClient - cookies


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

postPutPage.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜