Java Cookies似乎设置不正确

Java Cookies似乎设置不正确,java,android,http,cookies,Java,Android,Http,Cookies,我设法登录并获取有关会话的cookies。但是当我尝试发出新请求时,登录信息似乎丢失了(两个请求的HTML数据相同。第二个请求应该提供我的用户名和一些其他数据) 在发送新请求之前,我像这样设置cookie(DefaultHttpClient实例相同): List cookies=httpclient.getCookieStore().getCookies(); 如果(cookies!=null) { 用于(Cookie:cookies) { 字符串cookieString=cookie.getN

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

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

List cookies=httpclient.getCookieStore().getCookies();
如果(cookies!=null)
{
用于(Cookie:cookies)
{
字符串cookieString=cookie.getName()+“=”+cookie.getValue()+”;domain=“+cookie.getDomain();
httppost.addHeader(“Cookie”,Cookie.getName()+”=“+Cookie.getValue()+”;”;
System.out.println(cookieString);
}
}  
尝试
{
//System.out.println(httpclient.getCookieStore().getCookies());
response=(BasicHttpResponse)httpclient.execute(httppost,localContext);
}
我已经检查了cookie信息,它似乎返回了我在上面的for循环中设置的两个不同的“cookie实例”(两个会话ID)。但它似乎仍然不起作用。 有什么问题吗


谢谢你的帮助

谢谢你的回复,我猜http.execute(…)中的http是一个DefaultHttpClient实例?而且它应该能自动管理cookies,这样我就不必自己设置它们了?我一试用你的解决方案就会给你反馈:)结果它一直都有效。我犯的错误是,我试图将服务器响应打印为字符串。但是对于控制台窗口来说,字符串太大了(所以它没有打印出所有的数据)。不过,感谢您的帮助,您的解决方案也很有效,因此答案被接受:)
// 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);
// 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);