我以前将CookieContainer与HttpWebRequest和HttpWebResponse会话一起使用,但现在,我想将其与WebClient一起使用。据我所知,没有像HttpWebRequests (request.CookieContainer
)那样的内置方法。如何从CookieContainer中的WebClient收集cookies?
我对此执行googled并找到了the following sample
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;
}
}
这是最好的方法吗?
https://stackoverflow.com/questions/1777221
复制相似问题