系列目錄???? 【已更新最新開發(fā)文章,點擊查看詳細】
<https://www.cnblogs.com/SavionZhang/p/11422481.html>
HttpWebRequest.CookieContainer 獲取或設置與此請求關聯的 Cookie。默認情況下CookieContainer
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.httpwebrequest.cookiecontainer?view=netframework-4.8>
?是null。?
它是一種數據結構, 它為Cookie
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookie?view=netframework-4.8>
類的實例提供存儲, 并以類似于數據庫的方式訪問。?CookieContainer
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecontainer?view=netframework-4.8>
?具有一個容量限制, 該限制是在創(chuàng)建容器或由屬性更改時設置的。
Cookie
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookie?view=netframework-4.8>
類的實例根據其源 URI 添加到容器中。?它會添加到與 URI?CookieCollection
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecollection?view=netframework-4.8>
關聯的內部。?從基于 URI?CookieCollection
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecollection?view=netframework-4.8>
的容器中檢索, 或者作為可用于提交 HTTP WebRequests 的字符串從容器中檢索。
Cookie
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookie?view=netframework-4.8>
?有三個屬性, 這些屬性控制容器的內容量:?Capacity
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecontainer.capacity?view=netframework-4.8>
、?MaxCookieSize
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecontainer.maxcookiesize?view=netframework-4.8>
和PerDomainCapacity
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecontainer.perdomaincapacity?view=netframework-4.8>
。?CookieContainer
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecontainer?view=netframework-4.8>
?這些值分別為300、4096和20的默認設置。?當將Cookie
<https://docs.microsoft.com/en-us/dotnet/api/system.net.cookie?view=netframework-4.8>
添加到容器時,這些屬性用于確定是否應丟棄CookieContainer中
<https://docs.microsoft.com/en-us/dotnet/api/system.net.cookiecontainer?view=netframework-4.8>
已包含的Cookie
<https://docs.microsoft.com/en-us/dotnet/api/system.net.cookie?view=netframework-4.8>
以便為新容器騰出空間。?Cookie
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookie?view=netframework-4.8>
?跟蹤每個加法, 以確保Capacity
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecontainer.capacity?view=netframework-4.8>
?不會超過或PerDomainCapacity
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecontainer.perdomaincapacity?view=netframework-4.8>
限制。?CookieContainer
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecontainer?view=netframework-4.8>
?如果超過其中一個或兩個,?Cookie
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookie?view=netframework-4.8>
則將刪除由CookieContainer
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecontainer?view=netframework-4.8>
保留的實例。?首先, 刪除任何Cookie
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookie?view=netframework-4.8>
過期的。?如果必須回收更多的容量, 則會清除最近最少使用CookieCollection
<https://docs.microsoft.com/zh-cn/dotnet/api/system.net.cookiecollection?view=netframework-4.8>
的空間。
出于安全原因,默認情況下禁用了 cookie。 如果你想要使用 cookie,則使用CookieContainer屬性,以便啟用 cookie。
下面的代碼示例將請求發(fā)送到的 URL,并顯示在響應中返回的 cookie。
1 using System.Net; 2 using System; 3 namespace Examples.System.Net.Cookies
4 { 5 // 此示例在命令行中運行。 6 // 指定一個參數:發(fā)送請求的主機的名稱。 7 //
如果請求成功,該示例將顯示主機返回的cookie的內容。 8 9 public class CookieExample 10 { 11 public
static void Main(string[] args) 12 { 13 if (args == null || args.Length != 1)
14 { 15 Console.WriteLine("Specify the URL to receive the request."); 16
Environment.Exit(1); 17 } 18 HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(args[0]); 19 request.CookieContainer = new
CookieContainer();20 21 HttpWebResponse response = (HttpWebResponse)
request.GetResponse();22 23 24 25 // Print the properties of each cookie. 26
foreach (Cookie cook in response.Cookies) 27 { 28 Console.WriteLine("Cookie:");
29 Console.WriteLine("{0} = {1}", cook.Name, cook.Value); 30 Console.WriteLine("
Domain: {0}", cook.Domain); 31 Console.WriteLine("Path: {0}", cook.Path); 32
Console.WriteLine("Port: {0}", cook.Port); 33 Console.WriteLine("Secure: {0}",
cook.Secure);34 35 Console.WriteLine("When issued: {0}", cook.TimeStamp); 36
Console.WriteLine("Expires: {0} (expired? {1})", 37 cook.Expires,
cook.Expired);38 Console.WriteLine("Don't save: {0}", cook.Discard); 39
Console.WriteLine("Comment: {0}", cook.Comment); 40 Console.WriteLine("Uri for
comments: {0}", cook.CommentUri); 41 Console.WriteLine("Version: RFC {0}" ,
cook.Version ==1 ? "2109" : "2965"); 42 43 // Show the string representation of
the cookie. 44 Console.WriteLine ("String: {0}", cook.ToString()); 45 } 46 }
47 } 48 } 49 50 // 此示例的輸出將根據指定的主機名而有所不同,但將類似于以下內容。 51 /* 52 Cookie: 53
CustomerID = 13xyz54 Domain: .contoso.com 55 Path: / 56 Port: 57 Secure: False
58 When issued: 1/14/2003 3:20:57 PM 59 Expires: 1/17/2013 11:14:07 AM
(expired? False)60 Don't save: False 61 Comment: 62 Uri for comments: 63
Version: RFC 296564 String: CustomerID = 13xyz 65 */ CookieContainer 在 .NET3.5
與 .NET4.0 中的不同 .NET Framework 4.0 中的?HttpWebRequest.CookieContainer 有bug,參考:
https://www.crifan.com/baidu_emulate_login_for_dotnet_4_0_error_the_fisrt_two_args_should_be_string_type_0_1/
<https://www.crifan.com/baidu_emulate_login_for_dotnet_4_0_error_the_fisrt_two_args_should_be_string_type_0_1/>
?
系列目錄???? 【已更新最新開發(fā)文章,點擊查看詳細】
<https://www.cnblogs.com/SavionZhang/p/11422481.html>
熱門工具 換一換