+ Reply to Thread
Results 1 to 3 of 3

Thread: Problem reading cookies with C#

  1. #1

    Problem reading cookies with C#

    i created php page with these cookies :

    Code:
    setcookie("0","hello+how+are+you",time()+30);
    and in C# i wrote that code to read cookies :

    Code:
    string webaddr = "http://www.mywebsite.com";        
    string cookiesresult = ""; 
    
    //----Establish Connection to web and get cookies [Commands]----//       
    HttpWebRequest httpwr = (HttpWebRequest)WebRequest.Create(webaddr);
    httpwr.CookieContainer = new CookieContainer();
    HttpWebResponse httpwrs = (HttpWebResponse)httpwr.GetResponse();    
        
    //----Start Getting Cookies----//
    foreach (Cookie cook in httpwrs.Cookies)        
    {            
       cookiesresult = cook.Value;        
    }        
    
    //----Print Received Cookies----//
    Console.WriteLine("Cookies Recieved : " + cookiesresult);

    now cookies value should be : hello+how+are+you
    but instead value is : hello%2Bhowo%2areo%2you so it exchanged + with o%2 and i don`t know what is the problem
    Last edited by basemhosam; 01-07-2012 at 04:40 AM.

  2. #2
    Code:
                //----Define Variables----//
                string webaddr = "http://www.mywebsite.com";
                string cookiesresult = "";
                
                //----Establish Connection to web and get cookies [Commands]----//
                HttpWebRequest httpwr = (HttpWebRequest)WebRequest.Create(webaddr);
                httpwr.CookieContainer = new CookieContainer();
                HttpWebResponse httpwrs = (HttpWebResponse)httpwr.GetResponse();
    
                //----Start Getting Cookies----//
                foreach (Cookie cook in httpwrs.Cookies)
                {
                    cookiesresult = cook.Value; 
                }
    
                //----Decode the cookies---- this is what must be done to prevent that error//
                string cookiesrecieved = HttpUtility.UrlDecode(cookiesresult);
    
    
                Console.WriteLine("Command Recieved : " + cookiesrecieved);
    Last edited by r.vector; 01-07-2012 at 04:08 AM.

  3. #3
    Cool. That decoding cookies method was the answer, now it works fine to me. Thanks a lot Victor.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts