HttpListenerResponse.Cookies プロパティ
アセンブリ: System (system.dll 内)

Dim instance As HttpListenerResponse Dim value As CookieCollection value = instance.Cookies instance.Cookies = value
public: property CookieCollection^ Cookies { CookieCollection^ get (); void set (CookieCollection^ value); }
/** @property */ public CookieCollection get_Cookies () /** @property */ public void set_Cookies (CookieCollection value)
public function get Cookies () : CookieCollection public function set Cookies (value : CookieCollection)
応答に付随している Cookie を格納している CookieCollection。応答に Cookie が追加されていない場合、このコレクションは空です。

Cookie は、ローカル (クライアント) コンピュータに格納されている、Web サーバーから取得された名前と値のテキスト データです。サポートされている Cookie の形式は、Netscape、RFC 2109、および RFC 2965 です。Netscape Cookie の仕様については、http://wp.netscape.com/newsref/std/cookie_spec.html を参照してください。RFC のドキュメントは、http://www.rfc-editor.org を参照してください。

要求の Cookie を確認し、要求に Cookie がない場合は応答と共に新しい Cookie を返すコード例を次に示します。
// This example requires the System and System.Net namespaces. public static string NextCustomerID() { // A real-world application would do something more robust // to ensure uniqueness. return DateTime.Now.ToString(); } public static void SimpleListenerCookieExample(string[] prefixes) { // Create a listener. HttpListener listener = new HttpListener(); // Add the prefixes. foreach (string s in prefixes) { listener.Prefixes.Add(s); } listener.IgnoreWriteExceptions = true; listener.Start(); Console.WriteLine("Listening..."); // Note: The GetContext method blocks while waiting for a request. HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; string customerID = null; // Did the request come with a cookie? Cookie cookie = request.Cookies["ID"]; if (cookie != null) { customerID=cookie.Value; } if (customerID !=null) { Console.WriteLine("Found the cookie!"); } // Get the response object. HttpListenerResponse response = context.Response; // If they didn't provide a cookie containing their ID, give them one. if (customerID == null) { customerID = NextCustomerID(); Cookie cook = new Cookie("ID", customerID ); response.AppendCookie (cook); } // Construct a response. string responseString = "<HTML><BODY> Hello " + customerID + "!</BODY></HTML>"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get the response stream and write the response to it. response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer,0,buffer.Length); // You must close the output stream. output.Close(); // Closing the response sends the response to the client. response.Close(); listener.Stop(); }

Windows 98, Windows Server 2003, Windows XP Media Center Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- HttpListenerResponse.Cookies プロパティのページへのリンク