How to Handle the Session in HttpClient 4.1

后端 未结 1 473
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 08:12

I am using the HttpClient 4.1.1 to test my server\'s REST API.

I can manage to login seem to work fine but when I try to do anything else I am failing.

Most

相关标签:
1条回答
  • 2020-12-02 08:50

    The correct way is to prepare a CookieStore which you need to set in the HttpContext which you in turn pass on every HttpClient#execute() call.

    HttpClient httpClient = new DefaultHttpClient();
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    // ...
    
    HttpResponse response1 = httpClient.execute(method1, httpContext);
    // ...
    
    HttpResponse response2 = httpClient.execute(method2, httpContext);
    // ...
    
    0 讨论(0)
提交回复
热议问题