httpclient - 使用带有POST消息的cookie

And*_*s_D 1 java xml-rpc httpclient

我想创建一个小型Java应用程序,将一些wiki内容从一台服务器复制到另一台服务器.API 基于XML-RPC.

基本上我有三种方法login,getPageputPage.我使用Apache HttpClient 3.x并设法login成功登录并getPage正确地从旧维基获取页面.

使用cookie处理身份验证:我登录到新的wiki,并在相应的httpclient上设置了一些cookie.doku告诉我其中一个cookie用于验证.

然后我putPage在同一个httpclient上使用另一个POST方法执行,服务器响应身份验证失败消息.

代码序列就像这样(非常简化):

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
Run Code Online (Sandbox Code Playgroud)

它应该像那样工作,还是我必须手动将cookie添加到第二个帖子方法,如果是,如何?


编辑/解决方案

在这个问题的答案的帮助下,我能够识别并解决问题,这超出了httpclient的使用范围.最后,它是目标维基端的配置问题.这里的答案帮助我在另一个论坛中提出正确的问题.

Viv*_*sse 5

默认情况下,Cookie由HTTPClient处理.您不应该做任何事情来让cookie正常工作.

资料来源:http: //www.innovation.ch/java/HTTPClient/getting_started.html#cookies


编辑Apache HTTP客户端:

Apache HTTP Client的行为相同:-)

以下是来源:http: //hc.apache.org/httpclient-3.x/cookies.html