本文介绍了HTTPWebRequest 和 CookieContainer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个新的 HTTPWebRequest,但我无法为其分配 CookieContainer,这怎么可能?
I create a new HTTPWebRequest, but I am unable to assign a CookieContainer to it, how would this be possible?
CookieContainer = new CookieContainer();
//Create a WebRequest to get the file
HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(@"http://www.example.com/file.zip);
推荐答案
这里是一个简单的使用CookieContainer的例子
Here is a simple example of using 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());
}
}
这篇关于HTTPWebRequest 和 CookieContainer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!