Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.IO;
- using System.Net;
- using System.Text;
- namespace HTTP_Server
- {
- class Program
- {
- private static void Main(string[] args)
- {
- HttpListener httpListener = new HttpListener();
- string[] array = { "http://localhost:8080/", "http://127.0.0.1:8080/" };
- for (int i = 0; i < array.Length; i++) httpListener.Prefixes.Add(array[i]);
- httpListener.Start();
- Console.WriteLine("Server started on localhost port 8080");
- Console.WriteLine("Listening for clients...");
- while (true)
- {
- HttpListenerContext context = httpListener.GetContext();
- HttpListenerRequest request = context.Request;
- string text2 = request.RawUrl;
- text2 = text2.Insert(1, " ");
- string[] array2 = text2.Split(new char[] { ' ' });
- text2 = array2[1];
- string[] array3 = text2.Split(new char[] { '_' });
- string text3 = "";
- array = array3;
- string outputPage = "";
- for (int i = 0; i < array.Length; i++)
- {
- string text = array[i];
- try
- {
- string fileName = text + ".txt";
- StreamReader streamReader = new StreamReader(fileName);
- text3 += streamReader.ReadToEnd();
- Console.WriteLine("Reading from file: " + fileName);
- streamReader.Close();
- int num = 0;
- ArrayList arrayList = new ArrayList();
- while (true)
- {
- int num2 = text3.IndexOf("<td>", num) + 4;
- num = text3.IndexOf("</td>", num + 1);
- if (num < 0)
- {
- break;
- }
- string value = text3.Substring(num2, num - num2);
- arrayList.Add(value);
- }
- int suma = 0;
- for (int j = 0; j < arrayList.Count; j++)
- {
- if (j % 2 == 1)
- {
- suma += Convert.ToInt32(arrayList[j]);
- }
- }
- outputPage = "<HTML><BODY><table border='1'>" + text3 + "</table><br>Suma:" + suma + "</BODY></HTML>";
- }
- catch (FileNotFoundException e)
- {
- outputPage = "Package doesn't exist or you entered wrong name, try with this <a href=http://localhost:8080/1> http://localhost:8080/1 </a> ";
- }
- }
- HttpListenerResponse response = context.Response;
- byte[] bytes = Encoding.UTF8.GetBytes(outputPage);
- response.ContentLength64 = (long)bytes.Length;
- Stream outputStream = response.OutputStream;
- outputStream.Write(bytes, 0, bytes.Length);
- outputStream.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement