0

我设法登录并获取有关会话的 cookie。但是当我尝试发出新请求时,登录信息似乎丢失了(两个请求的 HTML 数据相同。第二个请求应提供我的用户名和其他一些数据)。

在发送新请求之前,我设置了这样的 cookie(DefaultHttpClient 实例是相同的):

    List<Cookie> cookies = httpclient.getCookieStore().getCookies();

    if(cookies != null)
    {
        for(Cookie cookie : cookies)
        {
            String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();  
            httppost.addHeader("Cookie",cookie.getName() + "=" + cookie.getValue() + ";");
            System.out.println(cookieString);
        }
    }  

    try
    {
        //System.out.println(httpclient.getCookieStore().getCookies());
        response = (BasicHttpResponse) httpclient.execute(httppost,localContext);
    }

我检查了 cookie 信息,它似乎返回了我在上面的 for 循环中设置的两个不同的“cookie 实例”(两个会话 ID)。但它似乎仍然不起作用。可能是什么问题?

谢谢你的帮助!

4

1 回答 1

0
// Create a static instance of cookie store globally 

     cookieStore = new BasicCookieStore();

 // Create local HTTP context

    HttpContext localContext = new BasicHttpContext();

// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//execute your connection with context
  HttpResponse response = http.execute(post,localContext);


And then whenever you connect use that static cookie instance to connect
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, StaticInstance(cookieStore));
//and as usual
response = http.execute(post,localContext);
于 2013-05-23T06:10:00.847 回答