Csharp/CSharp Tutorial/Network/Web HTTP Server — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:20, 26 мая 2010

A simple HTTP echo server

<source lang="csharp">using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.IO.rupression; using System.Net; using System.Net.Mail; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Xml; public class MainClass {

   public static void Main()
   {
       using (HttpListener listener = new HttpListener())
       {
           listener.AuthenticationSchemes = AuthenticationSchemes.Negotiate;
           listener.Prefixes.Add("http://localhost:8080/");
           listener.Prefixes.Add("https://localhost/");
           listener.Start();
           HttpListenerContext ctx = listener.GetContext();
           ctx.Response.StatusCode = 200; 
           string name = ctx.Request.QueryString["name"];
           StreamWriter writer = new StreamWriter(ctx.Response.OutputStream);
writer.WriteLine("

Hello, {0}

", name); writer.WriteLine("
    "); foreach (string header in ctx.Request.Headers.Keys) { writer.WriteLine("
  • {0}: {1}
  • ",header, ctx.Request.Headers[header]);
               }
    
    writer.WriteLine("
");
           writer.Close();
           ctx.Response.Close();
           listener.Stop();
       }
   }

}</source>