有没有办法在使用HttpWebRequest/HttpWebResponse对象时自动处理cookie?我希望能够在.NET环境中找到类似于LWP::UserAgent及其行为的功能(perl)。
有什么建议或者意见吗?
我认为你需要的是CookieContainer类。如果我正确理解你想要做的事情,你有请求和响应的分开对象,并且希望自动将响应cookie集合传输到下一个请求cookie集合中。尝试使用以下代码:
CookieContainer cookieJar = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
request.CookieContainer = cookieJar;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
int cookieCount = cookieJar.Count;