C# POST Request.Form

This article shows you how to POST to a legacy system that using Request.Form to read the POST data. When you click the link at the bottom on this article then you will get the code simulate to the one below.

public static void PostData()


    // Create a request using a URL that can receive a post.

    WebRequest request = WebRequest.Create(http://localhost:58520/GetData);

    // Set the Method property of the request to POST.

    request.Method = “POST”;

    // Create POST data and convert it to a byte array.

    //string postData = “This is a test that posts this string to a Web server.|KFH0346”;

    string postData = String.Format(“fpx_msgToken={0}&fpx_msgType={1}”, “KFH0346”, “01”);

    //from Webform site

    //string strfpx_msgToken = Request.Form[“fpx_msgToken”];

    //string strfpx_msgType = Request.Form[“fpx_msgType”];

    byte[] byteArray = Encoding.UTF8.GetBytes(postData);

    // Set the ContentType property of the WebRequest.

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

    // Set the ContentLength property of the WebRequest.

    request.ContentLength = byteArray.Length;

    // Get the request stream.

    Stream dataStream = request.GetRequestStream();

    // Write the data to the request stream.

    dataStream.Write(byteArray, 0, byteArray.Length);

    // Close the Stream object.


    // Get the response.

    WebResponse response = request.GetResponse();

    // Display the status.


    // Get the stream containing content returned by the server.

    // The using block ensures the stream is automatically closed.

    using (dataStream = response.GetResponseStream())


    // Open the stream using a StreamReader for easy access.

        StreamReader reader = new StreamReader(dataStream);

// Read the content.

        string responseFromServer = reader.ReadToEnd();

// Display the content.



    // Close the response.



At the server endpoint I have a webform with the code behind below.

protected void Page_Load(object sender, EventArgs e)


    string strfpx_msgToken = Request.Form[“fpx_msgToken”];

    string strfpx_msgType = Request.Form[“fpx_msgType”];


Reference: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-send-data-using-the-webrequest-class/?WT.mc_id=DP-MVP-36769

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Cloud, Community, Computers and Internet and tagged . Bookmark the permalink.

1 Response to C# POST Request.Form

  1. Pingback: C# POST Request.Form — Chanmingman’s Blog – Elliot Balynn's Blog

Leave a Reply

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

WordPress.com Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.