C# TCPListener Get POST request data

Mihai Floares 1 Reputation point

I have this simple TCPListener HTTP request:
TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 44394);

            while (true)

                TcpClient client = tcpListener.AcceptTcpClient();
                using (NetworkStream stream = client.GetStream())
                    byte[] requestBytes = new byte[100000];

                    int readBytes = stream.Read(requestBytes, 0, requestBytes.Length);

                    var requestResult = Encoding.UTF8.GetString(requestBytes, 0, readBytes);
                    Console.WriteLine("------------Begin of Request------------");
                    Console.WriteLine("------------End of Request------------");
                    TextReader fileReader = new StreamReader(websitePath);
                    string html = fileReader.ReadToEnd();

                    StreamWriter writer = new StreamWriter(stream);
                    writer.Write("HTTP/1.1 200 Success");
                    writer.Write($"Content-Type: text/html");
                    writer.Write("Content-Length: " + html.Length);



And this index.html:
<h1>Hello world</h1>
<form action="" method=POST>
<input id=data type=text>
<input id=submit type = submit>

How to get in the c# the content of the input with the id data as a POST request?

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,182 questions
Windows 10 Network
Windows 10 Network
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Network: A group of devices that communicate either wirelessly or via a physical connection.
2,266 questions
{count} votes

2 answers

Sort by: Most helpful
  1. jona varque 1 Reputation point

    @Viorel I am doing pretty much the same with TcpListener. I did write up a test using HttpListener but it has to be run in admin mode to work. Since I can not do that I have coded up a test with TcpListener. Unless you know how to get HttpListener to work without admin mode? So now, I have the same question. How to properly parse the request with the same functionality as HttpListener. You're right though.. it is easier with HttpListener!

  2. Bruce (SqlWork.com) 54,626 Reputation points

    the browser posts a form with the content-type of "application/x-www-form-urlencoded" or "multipart/form-data" (used with input type="file"), for url encoded form the body is a collection of name / value pairs. the name/value is separated with an "=", and the pairs are separated with a "&". both the name and value are url encoded. the name matches the name attribute of the form field, the value its value.

    only enabled form fields with a name are included in the post data. in addition a radio or checkbox is only included if it is checked. the clicked button will be included if it has a name.

    in your sample form, none of the form fields have a name attribute, so the post data will be empty.

    for example the form:

    <form method="post">
      <input name="a" value="1">
      <input name="a" value="2">
      <input name="b" value="3">
      <input name="c" type="submit" value="click">

    will post the data (if button clicked)


    note: the post data follows the blank line after the headers

    0 comments No comments