---
# System prepended metadata

title: HTTP

---

# HTTP

```c#
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace SimpleHttpWebService
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:8080/");
            listener.Start();
            Console.WriteLine("http://localhost:8080/");

            while (true)
            {
                HttpListenerContext context = listener.GetContext();
                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;

                if (request.HttpMethod == "GET")
                {
                    // страница html с полями ввода
                    string html = @"
                        <html>
                              <head>
                                <meta charset='UTF-8'>
                              </head>
                              <body>
                                <form method='POST'>
                                    <label for='name'>Имя   :</label>
                                    <input type='text' name='name' id='name'><br>
                                    <label for='email'>Email:</label>
                                    <input type='email' name='email' id='email'><br>
                                    <label for='age'>Возраст:</label>
                                    <input type='number' name='age' id='age'><br>
                                    <input type='submit' value='Отправить'>
                                </form>
                            </body>
                        </html>";
                    byte[] buffer = Encoding.UTF8.GetBytes(html);
                    response.ContentLength64 = buffer.Length;
                    response.ContentType = "text/html";
                    response.ContentEncoding = Encoding.UTF8;
                    response.OutputStream.Write(buffer, 0, buffer.Length);
                }
                else if (request.HttpMethod == "POST")
                {
                    var reader = new StreamReader(request.InputStream, request.ContentEncoding);
                    string requestBody = reader.ReadToEnd();

                    var formData = HttpUtility.ParseQueryString(requestBody);
                    string name = formData["name"];
                    string email = formData["email"];
                    string age = formData["age"];

                    // страница html с ответом
                    string html = $@"
                        <html>
                              <head>
                                <meta charset='UTF-8'>
                              </head>
                              <body>
                                Привет, {name}! Тебе {age} и твоя почта {email}
                              </body>
                        </html>";
                    byte[] buffer = Encoding.UTF8.GetBytes(html);
                    response.ContentLength64 = buffer.Length;
                    response.ContentType = "text/html";
                    response.ContentEncoding = Encoding.UTF8;
                    response.OutputStream.Write(buffer, 0, buffer.Length);
                }

                response.Close();
            }
        }
    }
}
```