Argentini.Halide.H3HttpPost.PostData C# (CSharp) Method

PostData() public method

Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
public PostData ( String url, String postData, String contentType, Int32 timeout, string>.Dictionary headerProps ) : String
url String the url to post to.
postData String The data to post.
contentType String Content type to use (e.g. "application/x-www-form-urlencoded")
timeout System.Int32 Number of milliseconds before the request times out.
headerProps string>.Dictionary Dictionary of header key and value pairs, or null for none
return String
        public String PostData(String url, String postData, String contentType, Int32 timeout, Dictionary<string,string> headerProps)
        {
            Url = url;

            HttpWebRequest request = null;
            HttpWebResponse response = null;
            String result = String.Empty;

            if (m_type == PostTypeEnum.Post)
            {
                Uri uri = new Uri(url);
                request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "POST";
                request.ContentType = contentType;
                request.ContentLength = postData.Length;
                request.Timeout = timeout;

                if (headerProps != null)
                {
                    foreach (KeyValuePair<string, string> item in headerProps)
                    {
                        request.Headers.Add(item.Key, item.Value);
                    }
                }

                using (Stream writeStream = request.GetRequestStream())
                {
                    UTF8Encoding encoding = new UTF8Encoding();
                    byte[] bytes = encoding.GetBytes(postData);
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }

            else
            {
                Uri uri = new Uri(url + "?" + postData);
                request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "GET";

                if (headerProps != null)
                {
                    foreach (KeyValuePair<string, string> item in headerProps)
                    {
                        request.Headers.Add(item.Key, item.Value);
                    }
                }
            }

            try
            {
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            result = readStream.ReadToEnd();
                        }
                    }

                    if (response != null)
                    {
                        LastStatus = ((Int32)response.StatusCode).ToString();
                    }
                }
            }

            catch (WebException ex)
            {
                LastStatus = result = ex.ToString();
            }

            return result;
        }

Same methods

H3HttpPost::PostData ( String url, String postData, String contentType, Int32 timeout ) : String