Programmatically Submit Form Post

On occasion it is necessary to post data via an HTTP POST to another page as though that post was submitted by a user but without the user’s interaction. In order to accomplish this use the following code:

        private void PostForm()


            WebRequest request = WebRequest.Create(_targetUrl);

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";

            string postContent = string.Format("parameter1={0}&parameter2={1}", _parameter1, _parameter2);

            byte[] postContentBytes = Encoding.ASCII.GetBytes(postContent);

            request.ContentLength = postContentBytes.Length;

            Stream writer = request.GetRequestStream();

            writer.Write(postContentBytes, 0, postContentBytes.Length);


            HttpWebResponse testResponse = (HttpWebResponse)request.GetResponse();

            if (!testResponse.StatusDescription.Equals("OK", StringComparison.InvariantCultureIgnoreCase))


                //TODO: Handle failure



Published by

Tim Clark

Experienced Business Owner, Chief Information Officer, Vice President, Chief Software Architect, Application Architect, Project Manager, Software Developer, Senior Web Developer, Graphic Designer & 3D Modeler, University Instructor, University Program Chair, Academic Director. Specialties: Ruby, Ruby on Rails, JavaScript, JQuery, AJAX, Node.js, React.js, Angular.js, MySQL, PostgreSQL, MongoDB, SQL Server, Responsive Design, HTML5, XHTML, CSS3, C#,, Project Management, System Design/Architecture, Web Design, Web Development, Adobe CS6 (Photoshop, Illustrator)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s