Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. namespace HttpLectionDemo
  2. {
  3.     using System;
  4.     using System.Net;
  5.     using System.Net.Sockets;
  6.     using System.Text;
  7.  
  8.     public class Program
  9.     {
  10.         public static void Main()
  11.         {
  12.             TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 80);
  13.  
  14.             tcpListener.Start();
  15.  
  16.             while (true)
  17.             {
  18.                 TcpClient client = tcpListener.AcceptTcpClient();
  19.  
  20.                 using (NetworkStream stream = client.GetStream())
  21.                 {
  22.                     byte[] requestBytes = new byte[100000000];
  23.                     // Thread.Sleep(10000);
  24.                     int readBytes = stream.Read(requestBytes, 0, requestBytes.Length);
  25.                     var stringRequest = Encoding.UTF8.GetString(requestBytes, 0, readBytes);
  26.                     Console.WriteLine(new string('~', 60));
  27.                     Console.WriteLine(stringRequest);
  28.                     string responseBody = "<h1 style=\"color:blue \">Hello, friend! I'm your testing page!</h1>";
  29.                     string response = "HTTP/1.0 200 OK" + Environment.NewLine +
  30.                                       "Content-Type: text/html" + Environment.NewLine +
  31.                                       "Set-Cookie: cookie1=test; Path=/test" + Environment.NewLine +
  32.                                       "Set-Cookie: cookie2=new cookie; Path=/test" + Environment.NewLine +
  33.                                       "Server: MyCustomServer/1.0" + Environment.NewLine +
  34.                                      $"Content-Length: {responseBody.Length}" + Environment.NewLine + Environment.NewLine +
  35.                                       responseBody;
  36.                     byte[] responseBytes = Encoding.UTF8.GetBytes(response);
  37.                     stream.Write(responseBytes, 0, responseBytes.Length);
  38.                 }
  39.             }
  40.  
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement