HTTPWebRequest和CookieContainer

时间:2012-06-09 20:01:02

标签: c# .net windows

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

CookieContainer = new CookieContainer();

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

1 个答案:

答案 0 :(得分:3)

以下是使用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());
            }
        }