Tag: webclient cookiecontainer

使用CookieContainer和WebClient类

我以前用HttpWebRequest和HttpWebResponse会话CookieContainer,但现在,我想用它与WebClient。 据我所知,没有像HttpWebRequests( request.CookieContainer )的内置方法。 如何从CookieContainer中的WebClient收集Cookie? 我google了这个,并发现以下示例 : 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; } } 这是做这件事的最好方法吗?