Advertisement
Guest User

MyHttpServer

a guest
Jul 11th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using System.Net;
  6. using System.Threading;
  7. using System;
  8. using System.IO;
  9.  
  10. namespace HTTPServer
  11. {
  12.     public class MyHttpServer {
  13.         public MyHttpServer(){
  14.             start();
  15.         }
  16.  
  17.  
  18.         private void start()
  19.         {
  20.             // Create a listener.
  21.             HttpListener listener = new HttpListener();
  22.  
  23.             listener.Prefixes.Add ("http://localhost:8080/");
  24.             listener.Prefixes.Add ("http://localhost:8080/test/");
  25.             listener.Start ();
  26.             Console.WriteLine ("Listening...");
  27.  
  28.             while (true) {
  29.                 //Anfrage
  30.                 HttpListenerContext context = listener.GetContext ();
  31.                 HttpListenerRequest request = context.Request;
  32.  
  33.                 HttpListenerResponse response = context.Response;
  34.  
  35.                 //Text in byte umwandeln
  36.                 byte[] buffer = formatByte("<html><head><title>Login Service</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head><body bgcolor=\"#FFFFFF\" text=\"#000000\"><h2>Aupheron Login Service</h2>Welcome to the login server from Aupheron <input type=\"submit\" name=\"Abschicken\" value=\"Abschicken\"> </body></html>");
  37.                 response.ContentLength64 = buffer.Length;
  38.  
  39.                 System.IO.Stream output = response.OutputStream;
  40.  
  41.                 String text = null;
  42.  
  43.                 StreamReader reader = new StreamReader (request.InputStream, request.ContentEncoding);
  44.                     while(true){
  45.                         text = reader.ReadLine ();
  46.                         if(text != null){
  47.                             break;
  48.                         }
  49.                     }
  50.                        
  51.  
  52.                     Console.WriteLine (text);
  53.  
  54.                 output.Write (buffer, 0, buffer.Length);
  55.  
  56.  
  57.  
  58.  
  59.  
  60.                 // You must close the output stream.
  61.                 output.Close ();
  62.  
  63.             }
  64.         }
  65.  
  66.         private byte[] formatByte(String responseString){
  67.             return System.Text.Encoding.UTF8.GetBytes (responseString);
  68.         }
  69.  
  70.         private String formatString(byte[] buffer){
  71.             return System.Text.Encoding.UTF8.GetString (buffer);
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement