Using CookieContainer with WebClient class

后端 未结 5 1908
遇见更好的自我
遇见更好的自我 2020-11-22 02:07

I\'ve previously used a CookieContainer with HttpWebRequest and HttpWebResponse sessions, but now, I want to use it with a WebClient. As far as I understand, there is no bui

相关标签:
5条回答
  • 2020-11-22 02:11

    The HttpWebRequest modifies the CookieContainer assigned to it. There is no need to process returned cookies. Simply assign your cookie container to every web request.

    public class CookieAwareWebClient : WebClient
    {
        public CookieContainer CookieContainer { get; set; } = new CookieContainer();
    
        protected override WebRequest GetWebRequest(Uri uri)
        {
            WebRequest request = base.GetWebRequest(uri);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = CookieContainer;
            }
            return request;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:20

    This one is just extension of article you found.

    
    public class WebClientEx : WebClient
    {
        public WebClientEx(CookieContainer container)
        {
            this.container = container;
        }
    
        public CookieContainer CookieContainer
            {
                get { return container; }
                set { container= value; }
            }
    
        private CookieContainer container = new CookieContainer();
    
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest r = base.GetWebRequest(address);
            var request = r as HttpWebRequest;
            if (request != null)
            {
                request.CookieContainer = container;
            }
            return r;
        }
    
        protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
        {
            WebResponse response = base.GetWebResponse(request, result);
            ReadCookies(response);
            return response;
        }
    
        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            ReadCookies(response);
            return response;
        }
    
        private void ReadCookies(WebResponse r)
        {
            var response = r as HttpWebResponse;
            if (response != null)
            {
                CookieCollection cookies = response.Cookies;
                container.Add(cookies);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:26

    I think there's cleaner way where you don't have to create a new webclient (and it'll work with 3rd party libraries as well)

    internal static class MyWebRequestCreator
    {
        private static IWebRequestCreate myCreator;
    
        public static IWebRequestCreate MyHttp
        {
            get
            {
                if (myCreator == null)
                {
                    myCreator = new MyHttpRequestCreator();
                }
                return myCreator;
            }
        }
    
        private class MyHttpRequestCreator : IWebRequestCreate
        {
            public WebRequest Create(Uri uri)
            {
                var req = System.Net.WebRequest.CreateHttp(uri);
                req.CookieContainer = new CookieContainer();
                return req;
            }
        }
    }
    

    Now all you have to do is opt in for which domains you want to use this:

        WebRequest.RegisterPrefix("http://example.com/", MyWebRequestCreator.MyHttp);
    

    That means ANY webrequest that goes to example.com will now use your custom webrequest creator, including the standard webclient. This approach means you don't have to touch all you code. You just call the register prefix once and be done with it. You can also register for "http" prefix to opt in for everything everywhere.

    0 讨论(0)
  • 2020-11-22 02:32
     WebClient wb = new WebClient();
     wb.Headers.Add(HttpRequestHeader.Cookie, "somecookie");
    

    From Comments

    How do you format the name and value of the cookie in place of "somecookie" ?

    wb.Headers.Add(HttpRequestHeader.Cookie, "cookiename=cookievalue"); 
    

    For multiple cookies:

    wb.Headers.Add(HttpRequestHeader.Cookie, 
                  "cookiename1=cookievalue1;" +
                  "cookiename2=cookievalue2");
    
    0 讨论(0)
  • 2020-11-22 02:33

    Yes. IMHO, overriding GetWebRequest() is the best solution to WebClient's limited functionalty. Before I knew about this option, I wrote lots of really painful code at the HttpWebRequest layer because WebClient almost, but not quite, did what I needed. Derivation is much easier.

    Another option is to use the regular WebClient class, but manually populate the Cookie header before making the request and then pull out the Set-Cookies header on the response. There are helper methods on the CookieContainer class which make creating and parsing these headers easier: CookieContainer.SetCookies() and CookieContainer.GetCookieHeader(), respectively.

    I prefer the former approach since it's easier for the caller and requires less repetitive code than the second option. Also, the derivation approach works the same way for multiple extensibility scenarios (e.g. cookies, proxies, etc.).

    0 讨论(0)
提交回复
热议问题