我以前在CookieHttpContainer和HttpWebRequest和HttpWebResponse会话中使用过,但是现在,我想在WebClient中使用它。据我了解,没有像HttpWebRequests(request.CookieContainer
)那样的内置方法。如何在CookieContainer中从WebClient收集cookie?
public class CookieAwareWebClient : WebClient
{
private readonly CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
HttpWebRequest webRequest = request as HttpWebRequest;
if (webRequest != null)
{
webRequest.CookieContainer = m_container;
}
return request;
}
}
这是最好的方法吗?
m_container
,永远都不会设定!它是否总是空的?