Cookies 为什么httpclient正在为每个请求刷新jsession id?

Cookies 为什么httpclient正在为每个请求刷新jsession id?,cookies,httpcontext,apache-httpclient-4.x,jsessionid,cookiestore,Cookies,Httpcontext,Apache Httpclient 4.x,Jsessionid,Cookiestore,我试图点击一个url(登录屏幕),获取jsessionid(J2EEJSESSIONID)并将其添加到cookie存储中,然后依次添加到上下文中,并点击具有凭据的相同url。我期待一个登录成功的屏幕。 然而,我再次被登录屏幕弹出。 然后,我打印了两个点击的响应标题。我希望两个具有相同J2EESESSIONID的响应都能保持会话。相反,这两个会话ID是不同的。请帮忙 请查找以下代码: HttpEntity entity = null; DefaultHttpClient httpC

我试图点击一个url(登录屏幕),获取jsessionid(J2EEJSESSIONID)并将其添加到cookie存储中,然后依次添加到上下文中,并点击具有凭据的相同url。我期待一个登录成功的屏幕。 然而,我再次被登录屏幕弹出。 然后,我打印了两个点击的响应标题。我希望两个具有相同J2EESESSIONID的响应都能保持会话。相反,这两个会话ID是不同的。请帮忙

请查找以下代码:

    HttpEntity entity = null;
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try{

        // Initialization
        HttpPost httpPost = new HttpPost("https://yyyyy.xxx.com/enl");
        HttpClientExample httpClientExample = new HttpClientExample();
        CookieStore cookieStore = new BasicCookieStore();
        HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        HttpGet httpGet = new HttpGet("https://yyyyy.xxx.com/enl");

        // Execute Get
        HttpResponse httpResponse = httpClient.execute(httpGet, httpContext);

        // Print the header for 1st url
        org.apache.http.Header[] headers = httpResponse.getAllHeaders();
        System.out.println("##### Header length::"+headers.length);
        for(int i=0;i<headers.length; i++)
        {
            System.out.println("Header Name::"+headers[i].getName());
            System.out.println("Header Val::"+headers[i].getValue());
        }  

        // update Cookie for the next hit
        org.apache.http.Header[] cookieHeaders = httpResponse.getHeaders("Set-Cookie");
        String html = EntityUtils.toString(httpResponse.getEntity());
        cookieStore = httpClientExample.updateCookieStore(cookieHeaders, cookieStore);
        httpClient.setCookieStore(cookieStore);
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        // Setting the redirects since i received 302 error
        httpClient.setRedirectStrategy(new DefaultRedirectStrategy() {                
            public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)  {
                boolean isRedirect=false;
                try {
                    isRedirect = super.isRedirected(request, response, context);
                } catch (ProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (!isRedirect) {
                    int responseCode = response.getStatusLine().getStatusCode();
                    if (responseCode == 301 || responseCode == 302) {
                        return true;
                    }
                }
                return false;
            }
        });

        // Added because i received Circular redirect error
        httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); 

        // Execute Post with credentials
         httpClient.getCredentialsProvider().setCredentials(
                 new AuthScope("http://yyyyy.xxx.com", 443),
                 new UsernamePasswordCredentials("usr", "pswd"));
         httpPost.setHeader("Cookie", "JSESSIONID="+ getSessionId(cookieHeaders));
         HttpResponse response = httpClient.execute(httpPost, httpContext);


       // Print the response
        entity = response.getEntity();
        InputStream content1 = (InputStream)entity.getContent();
        System.out.println("############### 2nd #####################"+response.getStatusLine().getStatusCode());
        BufferedReader in1   = 
            new BufferedReader (new InputStreamReader (content1));
        String line1;
        while ((line1 = in1.readLine()) != null) {
            System.out.println(line1);
        }

        // Print the header for 2nd url
        org.apache.http.Header[] headers1 = response.getAllHeaders();
        System.out.println("##### Header length 2 ::"+headers1.length);
        for(int i=0;i<headers1.length; i++)
        {
            System.out.println("Header Name 2 ::"+headers1[i].getName());
            System.out.println("Header Val 2 ::"+headers1[i].getValue());
        }  



    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally{

        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        httpClient.getConnectionManager().shutdown();
    }
}

private static String getSessionId(org.apache.http.Header[] headers) {
    // TODO Auto-generated method stub

    for(int i=0;i<headers.length; i++)
    {
        String str = headers[i].getValue();
        String[] strArray = str.split("=");
        String[] cookieValueArray = strArray[1].split(";");
        System.out.println(strArray[0]+"|"+cookieValueArray[0]);
        if(strArray[0].startsWith("J2EEJSESSION"))
        {
            System.out.println("cookieValueArray[0]:"+cookieValueArray[0]);
            return cookieValueArray[0];
        }

    }
    return null;
}

protected CookieStore updateCookieStore(org.apache.http.Header[] headers, CookieStore cookieStore)
{
    for(int i=0;i<headers.length; i++)
    {
        String str = headers[i].getValue();
        String[] strArray = str.split("=");
        String[] cookieValueArray = strArray[1].split(";");
        System.out.println(strArray[0]+"|"+cookieValueArray[0]);
        BasicClientCookie cookie = new BasicClientCookie(strArray[0], "A"+cookieValueArray[0]);
        /*if(strArray[0].startsWith("J2EEJSESSION"))
        {
            cookie.setDomain("yyyyy.xxx.com");
        }
        else
        {
            cookie.setDomain(".xxx.com");
        }*/

        cookie.setDomain(".xxx.com");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
        if(strArray[0].startsWith("J2EEJSESSION"))
        {
            BasicClientCookie cookie1 = new BasicClientCookie("JSESSIONID", "A"+cookieValueArray[0]);
            cookie1.setDomain(".xxx.com");
            cookie1.setPath("/");
            cookieStore.addCookie(cookie1);
        }
    }
    return cookieStore;
}
HttpEntity=null;
DefaultHttpClient httpClient=新的DefaultHttpClient();
试一试{
//初始化
HttpPost HttpPost=新的HttpPost(“https://yyyyy.xxx.com/enl");
HttpClientExample HttpClientExample=新的HttpClientExample();
CookieStore CookieStore=新的BasicCookieStore();
HttpContext HttpContext=新的BasicHttpContext();
setAttribute(ClientContext.COOKIE_存储,cookieStore);
HttpGet HttpGet=新的HttpGet(“https://yyyyy.xxx.com/enl");
//执行Get
HttpResponse HttpResponse=httpClient.execute(httpGet,httpContext);
//打印第一个url的标题
org.apache.http.Header[]headers=httpResponse.getAllHeaders();
System.out.println(“#####头长度::”+头.length);

对于(int i=0;我在发布此问题的同一天找到了答案..想到了分享。。
答案很简单..由于某些原因,身份验证没有成功,因此创建了新的jsessionId。将“httpClient.getCredentialsProvider().setCredentials()”替换为“BasicNameValuePair”,结果成功了:)

在我发布此问题的同一天找到了答案..想到了共享。。 答案很简单。由于某些原因,身份验证未成功,因此创建了新的jsessionId。将“httpClient.getCredentialsProvider().setCredentials()”替换为“BasicNameValuePair”,并成功:)