Basics: Posting data to a remote server via .NET

Class for Remote Posting:

public class RemotePost
        {
            #region Fields and Properties
            private string _url;
            private string _postData;
            private byte[] _data;
    
            /// <summary>
            /// URL to which the Post is sent
            /// </summary>
            public string Url
            {
                get
                {
                    if (!_url.StartsWith("http://"))
                        return String.Format("{0}{1}", "http://", _url);
                    else
                        return _url;
                }
                set
                {
                    _url = value;
                }
            }
    
            /// <summary>
            /// The Method to Use. Can also change to "GET"
            /// </summary>
            private string Method
            {
                get
                {
                    return "POST";
                }
            }
    
            /// <summary>
            /// A string of data you want to post to a URL
            /// </summary>
            public string PostData
            {
                get
                {
                    return _postData;
                }
                set
                {
                    _postData = value;
                }
            }
    
            /// <summary>
            /// Pulls the string of PostData
            /// </summary>
            public byte[] Data
            {
                get
                {
                    if (_data != null)
                        return _data;
                    else
                        return ASCIIEncoding.Default.GetBytes(PostData);
                }
                set
                {
                    _data = value;
                }
            }
            #endregion
    
            /// <summary>
            /// Posts to remote URL, and returns the response
            /// </summary>
            /// <returns></returns>
            public string Post()
            {
                if (string.IsNullOrEmpty(Url) | string.IsNullOrEmpty(PostData))
                    return "Error with URL or Post Data, try again";
    
                HttpWebRequest req =
                    (HttpWebRequest)WebRequest.Create(Url);
                req.Method = Method;
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = Data.Length;
                Stream newStream = req.GetRequestStream();
    
                // send!
                newStream.Write(Data, 0, Data.Length);
                newStream.Close();
    
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Encoding enc = System.Text.Encoding.GetEncoding(1252);
    
                StreamReader reader = 
                    new StreamReader(resp.GetResponseStream(), enc);
    
                string response = reader.ReadToEnd();
                return response;
            }
        }

Usage:

RemotePost app2 = new RemotePost();    
    app2.PostData = "var1=encodeme&var2=encodemetoo";
    app2.Url = "http://www.example.com/ProcessingPage.aspx";    
    txtServerResponse.Text = app2.Post();

The comments and code are pretty self-explanatory. Have fun!

Related Articles