在C#中阅读Cookie

时间:2011-06-22 17:52:00

标签: c# php cookies

我使用这些cookie创建了php页面:

setcookie("0","hello+how+are+you",time()+30);

在C#中我写了代码来读取cookie:

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

        Console.WriteLine("Cookies Recieved : " + cookiesresult);

现在cookie的值应该是:你好+怎么样+你+你 但相反值是:你好%2Bhowo%2areo%2you

所以它与o%2交换了+我不知道问题是什么

2 个答案:

答案 0 :(得分:3)

Cookie为URL Encoded,也称为百分比编码。使用HttpServerUtility.UrlDecode方法解码它们。

答案 1 :(得分:0)

当你创建一个cookie时,它会将所有特殊字符转换为它们的html等价物。

例如Google搜索:http://www.google.com/?q=hello%20there%20how%20are%20you