| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 
 |  
using System;
using System.Windows.Forms;
using System.IO;
using System.Net;
 
 
namespace HttpListenerTest
{
    public partial class Form1 : Form
    {
        private System.Net.HttpListener httpListener = new System.Net.HttpListener();
        private static System.Threading.AutoResetEvent listenForNextRequest = new System.Threading.AutoResetEvent(false);
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            if (httpListener.Prefixes.Count == 0)
            {
                httpListener.Prefixes.Add("http://+:80/"); 
            }
            httpListener.Start();
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(Listen));
        }
 
        private void Listen(object state)
        {
            while (httpListener.IsListening)
            {
                httpListener.BeginGetContext(new AsyncCallback(ListenerCallback), httpListener);
                listenForNextRequest.WaitOne();
            }
        }
 
        private static void ListenerCallback(IAsyncResult ar)
        {
            System.Net.HttpListener hl = ar.AsyncState as System.Net.HttpListener;
            System.Net.HttpListenerContext context = null;
           if (hl == null) return;
            try
            {
                context = hl.EndGetContext(ar);
            }
            catch   //(Exception ex)
            {
                return;
            }
            finally
            {
                listenForNextRequest.Set();
            }
 
            if (context == null) return;
            System.Net.HttpListenerRequest request = context.Request;
 
            if (request.HasEntityBody)
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(request.InputStream, request.ContentEncoding))
                {
                    string requestData = sr.ReadToEnd();
                }
            }
 
            using (System.Net.HttpListenerResponse response = context.Response)
                {
                string path = Environment.GetCommandLineArgs()[0];
                string dirpath = path.Substring(0, path.LastIndexOf(@"\"));
                string LocalFileName = LocalFileName = dirpath + @"\" + "index.htm";
                if (request.Url.ToString().Substring(request.Url.ToString().LastIndexOf("/") + 1) != "")
                    {
                    LocalFileName = dirpath + @"\" + request.Url.ToString().Substring(request.Url.ToString().LastIndexOf("/") + 1);
                    }
                System.IO.FileStream WebFile = new System.IO.FileStream(LocalFileName, FileMode.Open);
                byte[] buffer = new byte[WebFile.Length];
                WebFile.Read(buffer, 0, buffer.Length);
                WebFile.Close();
                response.ContentLength64 = buffer.LongLength;
                response.OutputStream.Write(buffer, 0, buffer.Length);
                }
        }
 
        private void button3_Click_1(object sender, EventArgs e)
        {
            httpListener.Stop();
        }
 
 
    }
} | 
Partager