为什么我的Httpwebresponse对象的cookie属性保持为空?(WP8)

时间:2013-12-18 10:54:27

标签: cookies windows-phone-8 httpwebresponse

我在Web服务器上请求Basic Authent并且服务器应该返回一个cookie。当我使用web浏览器时我可以在FiddlerProxy中检查它...当我调试我的代码时,response.cookie仍为null 。这是我的代码。谢谢你的帮助

  public void ConnexionNT(string password,string user)

    {
        CookieContainer _cookiecontainer=new CookieContainer();
        try
        {

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL_CONNEXION);

            request.Method = "GET";

            request.Credentials = new NetworkCredential(user, password, domain);

            request.CookieContainer = _cookiecontainer;

            request.BeginGetResponse(new AsyncCallback(GetResponse), request);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }
    private void GetResponse(IAsyncResult MyresponseAsync)
    {
        HttpWebRequest request = (HttpWebRequest)MyresponseAsync.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(MyresponseAsync);


        if (response.Cookies!=null) //REMAINS NULL
        {    //SAVE COOKIE}
     }

修改

如果我在类级移动Cookiecontainer decalrtion,httpwebresponse的cookie属性不再为null ...

EDIT2

此代码是否正确以从服务器接收cookie?

 public void ConnexionNT(string password,string user)

    {

        try
        {

            HttpWebRequest request=     (HttpWebRequest)HttpWebRequest.Create(URL_CONNEXION);

            request.Method = "GET";

            request.Credentials = new NetworkCredential(user, password, domain);

            request.CookieContainer = _cookiecontainer;

            request.BeginGetResponse(new AsyncCallback(GetResponse),request);


        }

        catch(HttpRequestException)
        {
            MessageBox.Show("Un problème de connexion avec  le serveur a eu lieu.", "Echec Authentification", MessageBoxButton.OK);
        }
        catch (Exception )
        {
            MessageBox.Show("Une erreur a eu lieu","Echec Authentification", MessageBoxButton.OK);
        }

    }



    private void GetResponse(IAsyncResult MyresponseAsync)
    {

            HttpWebRequest request = (HttpWebRequest)MyresponseAsync.AsyncState;

            if (request != null)
            {
                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(MyresponseAsync);


 //Not null but EMPTY WHY?        if (response.Cookies != null)
                    {
                        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            using (IsolatedStorageFileStream isfs = isf.OpenFile("CookiesFile", FileMode.Create, FileAccess.Write))
                            {
                                using (StreamWriter sw = new StreamWriter(isfs))
                                {
                                    foreach (Cookie cookieValue in response.Cookies)
                                    {
                                        sw.WriteLine("Cookie: " + cookieValue.ToString());

                                    }
                                    sw.Close();
                                }

                            }
                            //declenche l'event pour lancer la navigation sur la vue "codepin"
                            if (event_GetCookie != null && response.Cookies.Count>0)
                                event_GetCookie.Invoke();

                        }

                    }

                }

2 个答案:

答案 0 :(得分:2)

来自MSDN:

  

如果关联的HttpWebRequest的CookieContainer属性为null,则Cookies属性也将为null。但是,服务器发送的任何cookie信息都将在Headers属性中提供。

您需要在请求中创建CookieContainer

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
        request.CookieContainer = new CookieContainer();

        HttpWebResponse response = (HttpWebResponse) request.GetResponse();



        // Print the properties of each cookie. 
        foreach (Cookie cook in response.Cookies)
        {
            Console.WriteLine("Cookie:");
            Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
        }

答案 1 :(得分:0)

试试这个。这可能会对你有所帮助

 public void ConnexionNT(string password,string user)    
        {
            CookieContainer _cookiecontainer=new CookieContainer();
            try
            {   
                CookieContainer container = new CookieContainer();
                container.Add(new Uri("http://yoursite"), new Cookie("name", "value"));
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://yoursite");
                request.CookieContainer = container;
                request.Method = "GET";
                request.Credentials = new NetworkCredential(user, password, domain);
                request.BeginGetResponse(new AsyncCallback(GetResponse), request);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void GetResponse(IAsyncResult MyresponseAsync)
        {
            HttpWebRequest request = (HttpWebRequest)MyresponseAsync.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(MyresponseAsync);    

            if (response.Cookies!=null) //REMAINS NULL
            {    //SAVE COOKIE}
         }