sexta-feira, 24 de julho de 2015

WebClientEx

using System;
using System.Net;

namespace Lapuinka.Web
{
    public class WebClientEx : WebClient
    {
        private readonly CookieContainer cookieContainer;

        public bool FormMode { get; set; }

        public WebClientEx()
        {
        }

        public WebClientEx(CookieContainer cookieContainer)
        {
            this.cookieContainer = cookieContainer;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = base.GetWebRequest(address);
            var httpRequest = request as HttpWebRequest;

            if (httpRequest != null)
            {
                httpRequest.CookieContainer = cookieContainer;

                if (FormMode)
                {
                    httpRequest.ContentType = "application/x-www-form-urlencoded";
                }
            }

            return request;
        }
    }
}


Postar um comentário