简体   繁体   English

为什么cookieManager的cookie与Android中的httpClient的cookie不同?

[英]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 : 我正在为Android编写一个程序,使用HttpClient将一些POST发送到webService,如下所示:

      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: 我试图检索名为“ Form”的“ www.example.com” cookie,如下所示:

             `
    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" ! 但是,两种检索Cookie的方法都会为“ Form”返回两个不同的值! why? 为什么?

According to the documentation CookieManager is used for cookies in WebViews: 根据文档, CookieManager用于CookieManager中的cookie:

Manages the cookies used by an application's WebView instances 管理应用程序的WebView实例使用的cookie

So if you want to get the cookie from your request httpclient.getCookieStore() should be the right way. 因此,如果您想从请求中获取Cookie,则httpclient.getCookieStore()应该是正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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