Post to a web page from a forms application
How to POST to a web page from a windows forms application in C#.
using System.IO; using System.Net; // declare & create the URI object that holds the URL of the web page we want to post to. System.Uri uriDest = new Uri("http://www.sample.com/testpost.aspx"); // declare & create an Http Web Request object using the URI object from above. System.Net.HttpWebRequest httpReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uriDest); // set the credentials. httpReq.Credentials = System.Net.CredentialCache.DefaultCredentials; // set the method to POST httpReq.Method = "POST"; // set the User Agent. // If this is being done from within a web application, the value // of the incoming request object could be passed into here, if desired. httpReq.UserAgent = "Custom App Client"; // Declare the data to be posted. string sPostData = "this is a test of the emergency broadcasting system."; // convert the data to a byte array. byte[] bytArray = Encoding.UTF8.GetBytes(sPostData); // Set the ContentType property of the WebRequest. httpReq.ContentType = "application/x-www-form-urlencoded"; // set the content length httpReq.ContentLength = bytArray.Length; // Get the request stream. Stream dataStream = httpReq.GetRequestStream(); // Write the data to the request stream. dataStream.Write(bytArray, 0, bytArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. WebResponse response = httpReq.GetResponse(); HttpWebResponse objResponseWeb = (HttpWebResponse)response; // Get the status. string sStatus = string.Empty; sStatus = objResponseWeb.StatusDescription; // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd();