Advertisement
MagnusArias

PS1 | HTTP Server

May 15th, 2017
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6.  
  7. namespace HTTP_Server
  8. {
  9.     class Program
  10.     {
  11.         private static void Main(string[] args)
  12.         {
  13.             HttpListener httpListener = new HttpListener();
  14.             string[] array = { "http://localhost:8080/", "http://127.0.0.1:8080/" };
  15.             for (int i = 0; i < array.Length; i++) httpListener.Prefixes.Add(array[i]);
  16.  
  17.             httpListener.Start();
  18.             Console.WriteLine("Server started on localhost port 8080");
  19.             Console.WriteLine("Listening for clients...");
  20.  
  21.             while (true)
  22.             {
  23.                 HttpListenerContext context = httpListener.GetContext();
  24.                 HttpListenerRequest request = context.Request;
  25.  
  26.                 string text2 = request.RawUrl;
  27.                 text2 = text2.Insert(1, " ");
  28.                 string[] array2 = text2.Split(new char[] { ' ' });
  29.                 text2 = array2[1];
  30.                 string[] array3 = text2.Split(new char[] { '_' });
  31.                 string text3 = "";
  32.  
  33.                 array = array3;
  34.                 string outputPage = "";
  35.  
  36.                 for (int i = 0; i < array.Length; i++)
  37.                 {
  38.                     string text = array[i];
  39.                     try
  40.                     {
  41.                         string fileName = text + ".txt";
  42.                         StreamReader streamReader = new StreamReader(fileName);
  43.                         text3 += streamReader.ReadToEnd();
  44.                         Console.WriteLine("Reading from file: " + fileName);
  45.                         streamReader.Close();
  46.  
  47.                         int num = 0;
  48.                         ArrayList arrayList = new ArrayList();
  49.  
  50.                         while (true)
  51.                         {
  52.                             int num2 = text3.IndexOf("<td>", num) + 4;
  53.                             num = text3.IndexOf("</td>", num + 1);
  54.                             if (num < 0)
  55.                             {
  56.                                 break;
  57.                             }
  58.                             string value = text3.Substring(num2, num - num2);
  59.                             arrayList.Add(value);
  60.                         }
  61.                         int suma = 0;
  62.                         for (int j = 0; j < arrayList.Count; j++)
  63.                         {
  64.                             if (j % 2 == 1)
  65.                             {
  66.                                 suma += Convert.ToInt32(arrayList[j]);
  67.                             }
  68.                         }
  69.                         outputPage = "<HTML><BODY><table border='1'>" + text3 + "</table><br>Suma:" + suma + "</BODY></HTML>";
  70.  
  71.  
  72.                     }
  73.                     catch (FileNotFoundException e)
  74.                     {
  75.                        outputPage = "Package doesn't exist or you entered wrong name, try with this <a href=http://localhost:8080/1> http://localhost:8080/1 </a> ";
  76.                     }
  77.                 }
  78.                 HttpListenerResponse response = context.Response;
  79.                 byte[] bytes = Encoding.UTF8.GetBytes(outputPage);
  80.                 response.ContentLength64 = (long)bytes.Length;
  81.                 Stream outputStream = response.OutputStream;
  82.                 outputStream.Write(bytes, 0, bytes.Length);
  83.                 outputStream.Close();
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement