0

I am using the new Apache HttpClient 4.2 (not the one from Apache Commons).

I need to open up one HttpClient and make multiple requests to the same server. From the documentation, the httpClient should automatically maintain the cookie, and therefore have the multiple requests fall in the same session. However, on the server side, I am debugging thru and see that

HttpSession session = req.getHttpSession(true);

is returning a new HttpSession Object every time.

my client code is like this.

// 1st time

HttpClient httpClient = new DefaultHttpClient();
            req.getSession(true).setAttribute(HTTPCLIENT, httpClient);

            HttpGet httpget = new HttpGet(redirectUrl);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String content = httpClient.execute(httpget, responseHandler);

// subsequent calls
HttpClient httpClient = getHttpClient(req);

            HttpGet httpget = new HttpGet(redirectUrl);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String content = httpClient.execute(httpget, responseHandler);


// supported by the private method

private HttpClient getHttpClient(HttpServletRequest req){
        return (HttpClient) req.getSession(true).getAttribute(HTTPCLIENT);
    }

did I do anything wrong?

bhomass
  • 3,414
  • 8
  • 45
  • 75
  • Possible duplicate - http://stackoverflow.com/questions/6272575/how-to-handle-the-session-in-httpclient-4-1 – Stewart Aug 14 '13 at 22:19

1 Answers1

0

my dumb experiment.

it was because I was mixing up using and by passing the HttpClient on different requests from the same browser (e.g. by passing on .js and .css files). These by passed resources returned a different sessionId to the browser, and subsequently, the browser started using the new sessionId.

bhomass
  • 3,414
  • 8
  • 45
  • 75