adrianvlupu
7/4/2013 - 9:38 AM

HTTP Get/POST

HTTP Get/POST

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;


    public static class HttpUtils
    {
        public static string base64Encode(string data)
        {
            byte[] encData_byte = new byte[data.Length];
            encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
            string encodedData = Convert.ToBase64String(encData_byte);
            return encodedData;
        }

        public static bool HttpDelete(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "DELETE";
            request.ContentType = "application/x-www-form-urlencoded";
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                    return true;
                else return false;
            }
            catch
            {
                return false;
            }
        }
        public static string HttpGet(string url, Dictionary<string, string> headers = null)
        {
            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            if (headers != null)
                headers.ToList().ForEach(x => webRequest.Headers.Add(x.Key, x.Value));
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method = "GET";
            webRequest.Accept = "application/json";
            webRequest.KeepAlive = false;
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            StreamReader webSource = new StreamReader(webResponse.GetResponseStream());
            string source = string.Empty;
            source = webSource.ReadToEnd();
            webResponse.Close();
            return source;
        }
        public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
        {
            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            if (headers != null)
                headers.ToList().ForEach(x => webRequest.Headers.Add(x.Key, x.Value));
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method = "GET";
            webRequest.Accept = "application/json";
            webRequest.KeepAlive = false;
            var webResponse = await webRequest.GetResponseAsync();
            StreamReader webSource = new StreamReader(webResponse.GetResponseStream());
            string source = string.Empty;
            source = webSource.ReadToEnd();
            webResponse.Close();
            return source;
        }

        public static string HttpPost(string uri, string body, Dictionary<string, string> headers = null, bool getResponse = true)
        {
            WebRequest webRequest = WebRequest.Create(uri);
            if (headers != null)
                headers.ToList().ForEach(x => webRequest.Headers.Add(x.Key, x.Value));
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method = "POST";
            byte[] bytes = Encoding.ASCII.GetBytes(body);
            Stream os = null;
            try
            {
                webRequest.ContentLength = bytes.Length;
                os = webRequest.GetRequestStream();
                os.Write(bytes, 0, bytes.Length);
            }
            catch
            {
                throw new Exception("HttpPost: Request error");
            }
            finally
            {
                if (os != null) os.Close();
            }

            if (getResponse)
            {
                try
                {
                    WebResponse webResponse = webRequest.GetResponse();
                    if (webResponse == null)
                        return null;
                    StreamReader sr = new StreamReader(webResponse.GetResponseStream());
                    return sr.ReadToEnd().Trim();
                }
                catch (WebException ex)
                {
                    throw new Exception("HttpPost: Response error");
                    throw new Exception(ex.Message);
                }
            }
            return null;
        }
    }