Csharp/C Sharp by API/System.Net/HttpListener

Материал из .Net Framework эксперт
Версия от 12:12, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

HttpListener.AuthenticationSchemes

  

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("<P>Hello, {0}</P>", name);
            
            writer.WriteLine("<ul>");
            foreach (string header in ctx.Request.Headers.Keys)
            {
                writer.WriteLine("<li><b>{0}:</b> {1}</li>",header, ctx.Request.Headers[header]);
            }
            writer.WriteLine("</ul>");
            writer.Close();
            ctx.Response.Close();
            listener.Stop();
        }
    }
}


HttpListener.GetContext()

  

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("<P>Hello, {0}</P>", name);
            
            writer.WriteLine("<ul>");
            foreach (string header in ctx.Request.Headers.Keys)
            {
                writer.WriteLine("<li><b>{0}:</b> {1}</li>",header, ctx.Request.Headers[header]);
            }
            writer.WriteLine("</ul>");
            writer.Close();
            ctx.Response.Close();
            listener.Stop();
        }
    }
}


HttpListener.Prefixes.Add

  
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
class WebServer {
    HttpListener myListener;
    string _baseFolder;     
    public WebServer(string uriPrefix, string baseFolder) {
        System.Threading.ThreadPool.SetMaxThreads(50, 1000);
        System.Threading.ThreadPool.SetMinThreads(50, 50);
        myListener = new HttpListener();
        myListener.Prefixes.Add(uriPrefix);
        _baseFolder = baseFolder;
    }
    public void Start() {                       
        myListener.Start();
        while (true)
            try {
                HttpListenerContext request = myListener.GetContext();
                ThreadPool.QueueUserWorkItem(ProcessRequest, request);
            } catch (HttpListenerException) { break; }  
            catch (InvalidOperationException) { break; }
    }
    public void Stop() { 
       myListener.Stop(); 
    }
    void ProcessRequest(object listenerContext) {
        try {
            var context = (HttpListenerContext)listenerContext;
            string filename = Path.GetFileName(context.Request.RawUrl);
            string path = Path.rubine(_baseFolder, filename);
            byte[] msg;
            if (!File.Exists(path)) {
                context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                msg = Encoding.UTF8.GetBytes("Sorry, that page does not exist");
            } else {
                context.Response.StatusCode = (int)HttpStatusCode.OK;
                msg = File.ReadAllBytes(path);
            }
            context.Response.ContentLength64 = msg.Length;
            using (Stream s = context.Response.OutputStream)
                s.Write(msg, 0, msg.Length);
        } catch (Exception ex) { Console.WriteLine("Request error: " + ex); }
    }
}