1

Im requesting on a web server with Basic Authent and the server is supposed to return a cookie.It does when i use a web brower i can check it in FiddlerProxy... When i debug my code the response.cookie remains null. Here is my code.Thanks for your help

  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}
     }

EDIT

If i move the Cookiecontainer decalrtion at the class level the cookie property of httpwebresponse is not null anymore...

EDIT2

Is this code correct to receive cookie from the server ?

 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();

                        }

                    }

                }
Paul Martinez
  • 562
  • 1
  • 9
  • 19

2 Answers2

2

From MSDN:

If the CookieContainer property of the associated HttpWebRequest is null, the Cookies property will also be null. Any cookie information sent by the server will be available in the Headers property, however.

you need to create a CookieContainer in the request.

        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);
        }
Andras Csehi
  • 4,305
  • 1
  • 28
  • 36
  • I dont get it...I set a cookie container in the httpwebrequest... CookieContainer _cookiecontainer=new CookieContainer();request.CookieContainer = _cookiecontainer;..am i missing something ? – Paul Martinez Dec 18 '13 at 11:45
  • Can you try to move your CookieContainer declaration to class level? – Andras Csehi Dec 18 '13 at 12:04
  • Ok now the cookie property is not null but remains empty...and i can't check the exchanges between client and server by using windows phone emulator (FiddlerProxy not working) thanks for ur help – Paul Martinez Dec 18 '13 at 13:21
  • You can use fiddler with the emulator or the device. http://fiddler2.com/documentation/Configure-Fiddler/Tasks/MonitorWindowsPhone7 – Andras Csehi Dec 18 '13 at 14:34
  • I can't make it works...i tried also with charles, wireshark and Microsof network monitoring...But eachtime im not able to see what going on between my WP emulator and the server...Ca you tell me if my code is correct i wrote it in EDIT2 thanks a lot – Paul Martinez Dec 19 '13 at 14:52
0

Try this. may this will helps you

 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}
         }
Jaihind
  • 2,770
  • 1
  • 12
  • 19