Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace HttpLectionDemo
- {
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- public class Program
- {
- public static void Main()
- {
- TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 80);
- tcpListener.Start();
- while (true)
- {
- TcpClient client = tcpListener.AcceptTcpClient();
- using (NetworkStream stream = client.GetStream())
- {
- byte[] requestBytes = new byte[100000000];
- // Thread.Sleep(10000);
- int readBytes = stream.Read(requestBytes, 0, requestBytes.Length);
- var stringRequest = Encoding.UTF8.GetString(requestBytes, 0, readBytes);
- Console.WriteLine(new string('~', 60));
- Console.WriteLine(stringRequest);
- string responseBody = "<h1 style=\"color:blue \">Hello, friend! I'm your testing page!</h1>";
- string response = "HTTP/1.0 200 OK" + Environment.NewLine +
- "Content-Type: text/html" + Environment.NewLine +
- "Set-Cookie: cookie1=test; Path=/test" + Environment.NewLine +
- "Set-Cookie: cookie2=new cookie; Path=/test" + Environment.NewLine +
- "Server: MyCustomServer/1.0" + Environment.NewLine +
- $"Content-Length: {responseBody.Length}" + Environment.NewLine + Environment.NewLine +
- responseBody;
- byte[] responseBytes = Encoding.UTF8.GetBytes(response);
- stream.Write(responseBytes, 0, responseBytes.Length);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement