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?

4

1 に答える 1

0

私の愚かな実験。

これは、同じブラウザーからの異なる要求で HttpClient を使用して渡すことを混同していたためです (たとえば、.js ファイルと .css ファイルを渡すことによって)。これらの渡されたリソースは、ブラウザーに異なる sessionId を返し、その後、ブラウザーは新しい sessionId の使用を開始しました。

于 2013-08-16T00:40:22.337 に答える