获取和设置 Cookie

获取和设置 Cookie如果在 Silverlight 应用程序中指定客户端 HTTP 处理,则可以获取和设置与请求和响应关联的 Cookie。 本主题演示使用客户端 HTTP 处理时,如何获取和设置 HTTP 请求和响应上的 Cookie。

操作方法

  • 01

    设置请求消息上的 Cookie 为 HttpWebRequest 的 HttpWebRequest.CookieContainer 属性创建一个 System.Net.CookieContainer 对象。 C# VB request.CookieContainer = new CookieContainer();

  • 02

    使用 CookieContainer.Add 方法将 Cookie 对象添加到 HttpWebRequest.CookieContainer。 C# VB request.CookieContainer.Add(new Uri("http://api.search.live.net"),    new Cookie("id", "1234"));

  • 03

    获取响应消息上的 Cookie 在请求上创建一个 System.Net.CookieContainer 以保存对响应发送的 Cookie 对象。 即使没有发送任何 Cookie 也必须执行此操作。 C# VB request.CookieContainer = new CookieContainer();

  • 04

    检索 HttpWebResponse 的 HttpWebResponse.Cookies 属性中的值。 在此示例中,将检索 Cookie 并将它们保存到独立存储中。 C# VB private void ReadCallback(IAsyncResult asynchronousResult) {    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;    HttpWebResponse response = (HttpWebResponse)        request.EndGetResponse(asynchronousResult);    using (IsolatedStorageFile isf =        IsolatedStorageFile.GetUserStoreForSite())    {        using (IsolatedStorageFileStream isfs = isf.OpenFile("CookieExCookies",            FileMode.OpenOrCreate, FileAccess.Write))        {            using (StreamWriter sw = new StreamWriter(isfs))            {                foreach (Cookie cookieValue in response.Cookies)                {                    sw.WriteLine("Cookie: " + cookieValue.ToString());                }                sw.Close();            }        }    } }

  • 05

    示例 下面的示例演示如何创建 Web 请求并向该请求添加 Cookie。 此示例还演示如何从 Web 响应提取 Cookie,如何将 Cookie 写入独立存储中的文件以及从独立存储中进行读取。 运行此示例时,将在 TextBlock 控件中显示 System.Net.Cookie 值。 运行此示例 C# VB using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Net.Browser; using System.IO; using System.Text; using System.IO.IsolatedStorage; namespace CookiesEx {    public partial class MainPage : UserControl    {        public MainPage()        {            InitializeComponent();        }        private void button1_Click(object sender, RoutedEventArgs e)        {            InitializeWebRequestClientStackForURI();            ReadFromIsolatedStorage();        }        private void InitializeWebRequestClientStackForURI()        {            // Create the client WebRequest creator.            IWebRequestCreate creator = WebRequestCreator.ClientHttp;            // Register both http and https.            WebRequest.RegisterPrefix("http://", creator);            WebRequest.RegisterPrefix("https://", creator);            // Create a HttpWebRequest.            HttpWebRequest request = (HttpWebRequest)                WebRequest.Create("http://api.search.live.net/clientaccesspolicy.xml");            //Create the cookie container and add a cookie.            request.CookieContainer = new CookieContainer();            // This example shows manually adding a cookie, but you would most            // likely read the cookies from isolated storage.            request.CookieContainer.Add(new Uri("http://api.search.live.net"),                new Cookie("id", "1234"));            // Send the request.            request.BeginGetResponse(new AsyncCallback(ReadCallback), request);        }        // Get the response and write cookies to isolated storage.        private void ReadCallback(IAsyncResult asynchronousResult)        {            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;            HttpWebResponse response = (HttpWebResponse)                request.EndGetResponse(asynchronousResult);            using (IsolatedStorageFile isf =                IsolatedStorageFile.GetUserStoreForSite())            {                using (IsolatedStorageFileStream isfs = isf.OpenFile("CookieExCookies",                    FileMode.OpenOrCreate, FileAccess.Write))                {                    using (StreamWriter sw = new StreamWriter(isfs))                    {                        foreach (Cookie cookieValue in response.Cookies)                        {                            sw.WriteLine("Cookie: " + cookieValue.ToString());                        }                        sw.Close();                    }                }            }        }        private void ReadFromIsolatedStorage()        {            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite())            {                using (IsolatedStorageFileStream isfs =                   isf.OpenFile("CookieExCookies", FileMode.Open))                {                    using (StreamReader sr = new StreamReader(isfs))                    {                        tb1.Text = sr.ReadToEnd();                        sr.Close();                    }                }            }        }    } }

(0)

相关推荐