首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在WebClient类中使用CookieContainer

在WebClient类中使用CookieContainer
EN

Stack Overflow用户
提问于 2009-11-22 07:55:59
回答 5查看 150.7K关注 0票数 155

我以前将CookieContainer与HttpWebRequest和HttpWebResponse会话一起使用,但现在,我想将其与WebClient一起使用。据我所知,没有像HttpWebRequests (request.CookieContainer)那样的内置方法。如何从CookieContainer中的WebClient收集cookies?

我对此执行googled并找到了the following sample

代码语言:javascript
复制
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;
    }
}

这是最好的方法吗?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1777221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档