5
将CookieContainer与WebClient类一起使用
我以前在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; } } 这是最好的方法吗?