简体   繁体   中英

why the cookies of cookieManager is differenet from cookies of httpClient In Android?

I'm writing a program for Android that sends some POST to a webService with HttpClient like this :

      DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://example.com/service");
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
      nameValuePairs.add(new BasicNameValuePair("email", email));
      nameValuePairs.add(new BasicNameValuePair("password", password));
         // Execute HTTP Post Request
      ResponseHandler<String> responseHandler=new BasicResponseHandler();
      String response = httpclient.execute(httppost , responseHandler) ;

And I tried to retrieve a cookie of "www.example.com" called "Form" like this:

             `
    Log.d("Cookie0" , httpclient.getCookieStore().getCookies().get(0).getValue()) ;              
    Log.d("Cookie1", CookieManager.getInstance().getCookie("http://example.com"));

but the two methods of retrieving a cookie returns two different values for "Form" ! why?

According to the documentation CookieManager is used for cookies in WebViews:

Manages the cookies used by an application's WebView instances

So if you want to get the cookie from your request httpclient.getCookieStore() should be the right way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM