sexta-feira, 17 de julho de 2015

Get/Post C#

 public class HttpUtilities
    {
        public string Url
        { get; set; }

        public Dictionary<string, string> Parametros
        { get; set; }

        public MethodType Metodo
        { get; set; }

        public Encoding Charset
        { get; set; }

        public int Recorrencia
        { get; set; }

        public WebProxy Proxy
        { get; set; }

        public HttpUtilities()
        {
            this.Init();
        }

        public HttpUtilities(string url)
        {
            this.Init();
            this.Url = url;
        }

        public static string Execute(string url)
        {
            var http = new HttpUtilities(url);
            return http.Execute();
        }

        public string Execute()
        {
            int count = 0;
            string result = string.Empty;

            while (count <= this.Recorrencia)
            {
                try
                {
                    if (this.Metodo == MethodType.GET)
                    {
                        result = Get();
                    }
                    else
                    {
                        result = Post();
                    }

                }
                catch (Exception ex)
                {
                    result = ex.Message;
                }
                count++;
            }

            return result;
        }


        public static Int32 TimeOut
        {
            get
            {
                return !String.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["TimeOut"])
                    ? Convert.ToInt32(ConfigurationManager.AppSettings["TimeOut"])
                    : 15000;
            }
        }


        private string Get()
        {
            var request = WebRequest.Create(this.Url + FormatParametros());
            request.Timeout = TimeOut;
            request.Method = "GET";
            if (this.Proxy != null)
            {
                request.Proxy = this.Proxy;
            }

            var response = (HttpWebResponse)request.GetResponse();

            using (Stream responseStream = response.GetResponseStream())
            {
                using (var readerStream = new StreamReader(responseStream, Charset))
                {
                    return readerStream.ReadToEnd();
                }
            }

        }

        private string Post()
        {
            var request = WebRequest.Create(this.Url);
            request.Method = "POST";
            request.Timeout = TimeOut;
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] data = Charset.GetBytes(GetPostData());
            request.ContentLength = data.Length;

            if (this.Proxy != null)
            {
                request.Proxy = this.Proxy;
            }

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                var response = (HttpWebResponse)request.GetResponse();

                using (var responseStream = response.GetResponseStream())
                {
                    using (var readerStream = new StreamReader(responseStream, Charset))
                    {
                        return readerStream.ReadToEnd();
                    }
                }
            }
        }

        private string FormatParametros()
        {
            bool isFirst = true;
            string parametros = string.Empty;
            foreach (var item in this.Parametros)
            {
                if (isFirst)
                {
                    parametros = string.Format("?{0}={1}", item.Key, item.Value);
                    isFirst = false;
                }
                else
                {
                    parametros = string.Format("{0}&{1}={2}", parametros, item.Key, item.Value);
                }
            }

            return parametros;
        }

        private string GetPostData()
        {
            return FormatParametros().Replace("?", string.Empty);
        }

        private void Init()
        {
            Parametros = new Dictionary<string, string>();
            this.Metodo = MethodType.GET;
            Charset = Encoding.UTF8;
            this.Recorrencia = 1;
        }
    }

    public enum MethodType
    {
        GET,
        POST

    }
Postar um comentário