HttpContext没有持有cookie

时间:2014-10-15 15:45:46

标签: android http cookies

我尝试使用Cookie在我的Android应用上举行会话,但似乎我遇到了错误,因为我从未收到服务器的预期回复。

首先,我有一个按预期运行的登录例程,并返回所有预期的数据。

我的登录请求:

        HttpContext httpContext = new BasicHttpContext();
        HttpResponse response;
        HttpClient client = new DefaultHttpClient();
        String url = context.getString(R.string.url_login);
        HttpPost  connection = new HttpPost(url); 
        connection.setHeader("Content-Type","application/x-www-form-urlencoded");
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
        nameValuePair.add(new BasicNameValuePair(PARAM_LOGIN,params[0]));
        nameValuePair.add(new BasicNameValuePair(PARAM_PASSWORD,params[1]));
        connection.setEntity(new UrlEncodedFormEntity(nameValuePair,"UTF-8"));
        response = client.execute(connection,httpContext);
        data = EntityUtils.toString(response.getEntity());

在我做出回应之后,我只是根据数据做出了我需要的东西,然后事情就开始成为其中的一部分。因为现在我只是试图在同一个AsyncTask中调用我的服务器来测试我的cookie是否正确保存在我的HttpContext上。

首先我刚刚调用了我的网址而没有任何更改,只是重用我当前的HttpContext:

        HttpPost httpPost = new HttpPost(context.getString(R.string.url_cookie_test)); 
        HttpResponse response = client.execute(httpPost, httpContext);

由于此测试失败,我测试了在我的HttpPost标头上添加我的cookie值:

       httpPost.addHeader(context.getString(R.string.domain),PHPSESSID+"="+cookieID+";");

然后我尝试创建一个新的HttpContext并强制COOKIE_STORE:

        CookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie(PHPSESSID, cookieID);
        cookieStore.addCookie(cookie);
        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        HttpResponse response;
        HttpClient client = new DefaultHttpClient();
        HttpPost  httpPost = new HttpPost(context.getString(R.string.url_cookie_test));
        response = client.execute(connection,localContext);

一切都失败了,我已经确认,当我第一次收到登录回复时,我得到了Cookie中预期的数据,如下所示:

        List<Cookie> cookies = ((AbstractHttpClient) client).getCookieStore().getCookies();
        for (Cookie cookie: cookies){
             Log.i("Cookie Value",cookie.toString());
             /*
              Prints:[[version: 0][name: PHPSESSID][value: 2ebbr87lsd9077m79n842hdgl3][domain: mydomain.org][path: /][expiry: null]]
             */
        }

我已经在StackOverflow上搜索了一下,我找到了大量对我来说不起作用的解决方案,我将分享我已经尝试过的所有解决方案:

Android: Using Cookies in HTTP Post request

HttpPost request with cookies

Sending cookie with http post android

Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request

1 个答案:

答案 0 :(得分:0)

正如我告诉过你的,这里是一段代码,以便使用API​​ REST将httpPost创建到Spring MVC中开发的服务器上。请考虑以这种方式构建您的请求:

,注意评论。你应该根据你的情况调整它;)。您也可以将此代码包含在方法或任何您喜欢的方法中。

try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("yourPath");

        //NameValuePairs is  build with the params for your request

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        httppost.setHeader("Content-Type",
                "application/x-www-form-urlencoded");

        CookieStore cookieStore = new BasicCookieStore();

        //cookie is a variable that I stored in my shared preferences.
        //You have to send in it every request
        //In your case, JSESSIONID should change, because it's for Java.
        //Maybe it could be "PHPSESSID"
        BasicClientCookie c = new BasicClientCookie("JSESSIONID", cookie);

        //JSESSIONID: same comment as before.
        httppost.setHeader("Cookie", "JSESSIONID="+cookie);

        cookieStore.addCookie(c);
        ((AbstractHttpClient)httpclient).setCookieStore(cookieStore);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection" + e.toString());
    }

我希望这有帮助!!在“旧”项目中很难找到它:)