How to perform a Http Web Request using C#

This is a simple example of how to perform a simple GET / POST web request in C#.
The class exposes just the Get and Post methods, which use Generics in order to allow you to cast your results (as long as it supports IConvertable that is !). It uses a simple Delegate Dictionary to specifically attempt to cast the output as a “known” type.

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

namespace ServerRequest {
    public class ServerPost {
        #region Private Variables
        private CookieContainer cookieJar          = new CookieContainer();
        private const int BUFFER_SIZE              = 1024;
        #endregion

        #region Public Variables
        public Dictionary<string, string> Headers  = new Dictionary<string, string>();
        #endregion

        #region Process Delegate
        private Dictionary<string, Func<HttpWebResponse, object>> processResponseActions = new Dictionary<string, Func<HttpWebResponse, object>>() {
            {"string", 
                (r) =>{
                    var tmp = r.GetResponseStream();
                    if (tmp != null) {
                        return StreamToString(tmp);
                    } else {
                        return string.Empty;
                    }
                }
            },
            {"xmldocument",
                (r) =>{
                    XmlDocument builder = new XmlDocument();
                    var tmp = r.GetResponseStream();
                    if (tmp != null) {
                        builder.LoadXml(StreamToString(tmp));
                        return builder;
                    } else {
                        builder.LoadXml("<NoData/>");
                        return builder;
                    }
                }
            },
            {"stream", 
                (r) =>{
                  return r.GetResponseStream();
                }
            }
        };
#endregion

        #region Public Methods
        
        private static string StreamToString(System.IO.Stream _Stream) {
            using (StreamReader streamReader = new StreamReader(_Stream, true)) {
                return streamReader.ReadToEnd();
            }
        }

        private static System.IO.Stream StringToStream(string value) {
            byte[] bytedata = Encoding.UTF8.GetBytes(value);
            return new MemoryStream(bytedata);
        }

        public T Post<T>(string URL, string postData = "", string contentType=""){
            return ServerRequest<T>(URL, postData, contentType);
        }
        public T Get<T>(string URL){
            return ServerRequest<T>(URL, string.Empty, null);
        }
        #endregion

        #region Private Methods
        private T ServerRequest<T>(string URL, string postData, string contentType=""){
            string typeName = typeof(T).Name.ToLower();
            object retVal = null;
            byte[] buffer = Encoding.UTF8.GetBytes(postData);


            if (string.IsNullOrEmpty(URL)) {throw new NullReferenceException("URL cannot be null or empty");}

            HttpWebRequest requestContext = (HttpWebRequest)WebRequest.Create(URL);
            string[] existingHeaders = requestContext.Headers.AllKeys;
            Headers.ForEach(h=> {
                if (!existingHeaders.Contains(h.Key)){
                    requestContext.Headers.Add(string.Format("{0}:{1}", h.Key, "" + h.Value));
                }
            });

            requestContext.CookieContainer = cookieJar;
            requestContext.Timeout = 200000;
            if (postData != null) {
                BuildPost(ref requestContext, buffer, contentType);
            } else {
                requestContext.Method = "GET";
            }

            if (processResponseActions.ContainsKey(typeName)){
                retVal = processResponseActions[typeName]((HttpWebResponse)requestContext.GetResponse());
            }else{
                retVal = processResponseActions["string"]((HttpWebResponse)requestContext.GetResponse());
            }
            return (T)Convert.ChangeType(retVal, typeof(T));
        }

        private void BuildPost(ref HttpWebRequest requestContext, byte[] PostBuffer, string contentType="")
        {
            requestContext.Method = "POST";
            requestContext.KeepAlive = true;
            requestContext.ContentType = string.IsNullOrEmpty(contentType) ? "application/x-www-form-urlencoded" : contentType;
            requestContext.ContentLength = PostBuffer.Length;
            using (Stream postStream = requestContext.GetRequestStream()) {
                postStream.Write(PostBuffer, 0, PostBuffer.Length);
                postStream.Close();
            }
        }
        #endregion
    }
}