简体   繁体   English

apache commons httpclient 4.23表单登录问题在不同请求中使用了不同的会话cookie

[英]apache commons httpclient 4.23 form login problems different session cookies used in different requests

I have a protected resource which requires me to login. 我有一个受保护的资源,需要我登录。 Im using the commons client with the following code block. 我在下面的代码块中使用了commons客户端。

    HttpClient httpClient = new HttpClient();
    httpClient.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
    httpClient.getParams().setParameter("http.protocol.single-cookie-header", Boolean.TRUE);

    PostMethod postMethod = new PostMethod("/admin/adminlogon.do");
    postMethod.setRequestEntity(new StringRequestEntity("action=logon&adminUser=admin&adminPassword=password",
            "application/x-www-form-urlencoded",
            "UTF-8"));
    postMethod.addParameter("action","logon");
    postMethod.addParameter("adminUser","admin");
    postMethod.addParameter("adminPassword","password");

    httpClient.executeMethod(postMethod);
    String response2 = postMethod.getResponseBodyAsString();

Above is where I basically login. 以上是我基本上登录的地方。 This works fine im getting a nice little JSESSIONID cookie back. 即时获得一个不错的JSESSIONID cookie效果很好。

    GetMethod get = new GetMethod("/admin/api.do?action=getSomeJson");
    httpClient.executeMethod(get);

When I check the logic on the sever the for the 2nd request I notice that we are using a different JSESSIONID. 当我检查第二个请求的服务器逻辑时,我注意到我们正在使用不同的JSESSIONID。 Therefore the get seems to fail to log in. I was under the impression the httpClient managed the cookies and sent the same cookie back. 因此,get似乎无法登录。给我的印象是httpClient管理cookie并将相同的cookie发送回去。 When I log into my app normally through the UI I see the same cookie in each request just not in the this test code. 当我通常通过UI登录到我的应用程序时,我在每个请求中看到的是同一个cookie,只是不在此测试代码中。

    String s = get.getResponseBodyAsString();
    get.releaseConnection();

Do I need to do something with the httpClient to ensure it uses the same cookies from the first post request when it does its get request?? 我是否需要对httpClient做一些事情以确保它在获取请求时使用第一个发布请求中的相同cookie?

Thanks in advance. 提前致谢。

Your assumption regarding HTTP client cookie behavior is correct. 您对HTTP客户端Cookie行为的假设是正确的。 In your case your not use the same httpClient instance. 在您的情况下,您不能使用相同的httpClient实例。 To fix it you need to allocate the httpClient only once (in PostConstructor): 要解决此问题,您只需要分配一次httpClient(在PostConstructor中):

       httpClient = new DefaultHttpClient(); // or new HttpClient();

Then, you perform your calls using the same instance of the client. 然后,您使用客户端的相同实例执行呼叫。 The client will take a cookie from a response, will store it in the cookieStore and will send it with the next request. 客户端将从响应中获取一个cookie,并将其存储在cookieStore中,并将其与下一个请求一起发送。

[Added after the comment] The following code works for me: [在评论后添加]以下代码对我有用:

    httpClient = new DefaultHttpClient();
    // Create a local instance of cookie store
    cookieStore = new BasicCookieStore();
    // Set the store
    httpClient.setCookieStore(cookieStore);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM