HTTPWebRequest and CookieContainer

本文关键字:CookieContainer and HTTPWebRequest | 更新日期: 2023-09-27 18:36:44

我创建了一个新的HTTPWebRequest,但我无法为其分配CookieContainer,这怎么可能?

CookieContainer = new CookieContainer();
//Create a WebRequest to get the file
HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(@"http://www.example.com/file.zip);

HTTPWebRequest and CookieContainer

下面是一个使用 CookieContainer 的简单示例

       public static void Main(string[] args)
        {   
            if (args == null || args.Length != 1)
            {
                Console.WriteLine("Specify the URL to receive the request.");
                Environment.Exit(1);
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
            request.CookieContainer = new CookieContainer();
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();

            // Print the properties of each cookie.
            foreach (Cookie cook in response.Cookies)
            {                    
                // Show the string representation of the cookie.
                Console.WriteLine ("String: {0}", cook.ToString());
            }
        }