Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request?

后端 未结 4 975
梦毁少年i
梦毁少年i 2020-11-27 02:38

can you tell me how to store jsessionid in cookie, so it can be passed to the servlet with post request? I\'m using Apache HttpClient version 4.0.3. All the solutions I\'ve

相关标签:
4条回答
  • 2020-11-27 03:11

    I am so glad to solve this problem:

    HttpPost httppost = new HttpPost(postData); 
    CookieStore cookieStore = new BasicCookieStore(); 
    BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", getSessionId());
    
    //cookie.setDomain("your domain");
    cookie.setPath("/");
    
    cookieStore.addCookie(cookie); 
    client.setCookieStore(cookieStore); 
    response = client.execute(httppost); 
    

    So Easy!

    0 讨论(0)
  • 2020-11-27 03:14

    I did it by passing the cookie through the HttpContext:

    HttpContext localContext = new BasicHttpContext();
    
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    
    response = client.execute(httppost, localContext);
    
    0 讨论(0)
  • 2020-11-27 03:29

    You should probably set all of the cookie properties not just the value of it. setPath(), setDomain() ... etc

    0 讨论(0)
  • 2020-11-27 03:31
    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    response = client.execute(httppost, localContext);
    

    doesn't work in 4.5 version without

    cookie.setDomain(".domain.com");
    cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
    
    0 讨论(0)
提交回复
热议问题