网络各通信 - 获取和设置 Cookie
如果在 Silverlight 应用程序中指定客户端 HTTP 处理,则可以获取和设置与请求和响应关联的 Cookie
设置请求消息上的 Cookie
1. 为 HttpWebRequest 的 HttpWebRequest.CookieContainer 属性创建一个 System.Net.CookieContainer 对象。
2. 使用 CookieContainer.Add 方法将 Cookie 对象添加到 HttpWebRequest.CookieContainer。
获取响应消息上的 Cookie
1. 在请求上创建一个 System.Net.CookieContainer 以保存对响应发送的 Cookie 对象。即使没有发送任何 Cookie 也必须执行此操作。
2. 检索 HttpWebResponse 的 HttpWebResponse.Cookies 属性中的值。在此示例中,将检索 Cookie 并将它们保存到独立存储中。
View Code
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();
}
}
}
}
}
}
UI Code
<UserControl x:Class="CookiesEx.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<StackPanel x:Name="LayoutRoot">
<Button Width="200" Height="50" Content="Click to send request"
HorizontalAlignment="Left"
x:Name="button1" Click="button1_Click" Margin="5"/>
<TextBlock Margin="5" Width="600" Height="400" x:Name="tb1"
HorizontalAlignment="Left" />
</StackPanel>
</UserControl>