Android-HttpContext中的Cookie不会为每个URL检索一次以上

Android-HttpContext中的Cookie不会为每个URL检索一次以上,android,apache,redirect,cookies,http-post,Android,Apache,Redirect,Cookies,Http Post,我试图为受保护的服务器检索JSon信息,并且每次尝试获取受保护的资源时都会重定向到登录页面。所以我应该使用cookies来实现对信息的访问 不幸的是,每个URL的帖子总共有3个,只工作了一次 使用以下功能登录应用程序: // Making HTTP request try { httpClient = getNewHttpClient(); String redirectedUrl = getUrl(url); // defau

我试图为受保护的服务器检索JSon信息,并且每次尝试获取受保护的资源时都会重定向到登录页面。所以我应该使用cookies来实现对信息的访问

不幸的是,每个URL的帖子总共有3个,只工作了一次

使用以下功能登录应用程序:

    // Making HTTP request
    try {
        httpClient = getNewHttpClient();

        String redirectedUrl = getUrl(url);

        // defaultHttpClient
        HttpPost httpPost = new HttpPost(redirectedUrl);

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", login));
        nameValuePairs.add(new BasicNameValuePair("password", password));

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        httpContext = new BasicHttpContext();
        CookieStore mCookieStore      = new BasicCookieStore();        
        httpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);

        HttpResponse response = httpClient.execute(httpPost, httpContext);

        HttpEntity entity = response.getEntity();

        String html = null;
        if (entity != null) {
            InputStream instream = entity.getContent();
            try {
                html = streamToString(instream);
            } finally {
                instream.close();
            }
        }

        if ((html != null) && html.contains("error loginError")) {

        } else
            return html;
    } catch (IOException e) {

}

当我试图在第二次抛出IOException时获取信息时,httpClient和httpContext都是全局的和静态的。

我得到了一个解决方案,我认为这不是一个好的解决方案,但它完全可以工作

为了让它停止以获取IOException,我只在所有post之前调用了login函数,这意味着对于所有post,我必须设置一个新的HTTPContext logged和valid

如果有人有更好的解决办法,我很高兴听到你的意见

PS:实际上问题是关于服务器端的会话持续时间,现在它工作正常了,无论如何,我将把这个主题放在这里,因为它可能对实现这种解决方案的人有用

        HttpPost httpPost = new HttpPost(url);

        HttpResponse response = httpClient.execute(httpPost, httpContext);

        HttpEntity entity = response.getEntity();
        String html = null;
        if (entity != null) {
            InputStream instream = entity.getContent();
            try {
                html = streamToString(instream);
            } finally {
                instream.close();
            }
        }