码迷,mamicode.com
首页 > Windows程序 > 详细

C# 创建精简版IIS

时间:2016-02-02 18:50:03      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.IO;

namespace SampleIIS
{
    // Sample IIS class
    class SampleIIS
    {
        public void StartIIS()
        {
            string url = "http://localhost:8080/";
            string vm_ID = string.Empty;
            HttpListener httpListener = new HttpListener();

            httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
            httpListener.Prefixes.Add(url);

            // 1. Start Server
            System.Console.WriteLine("[{1}] [system] Start Server, Port {0} \r\n", url, DateTime.Now.ToLongTimeString());

            httpListener.Start();
            new Thread(new ThreadStart(delegate
            {
                while (true)
                {
                    // 2. Wating commad 
                    System.Console.WriteLine("[{0}] [system]  Wating commad ..... \r\n ", DateTime.Now.ToLongTimeString());

                    HttpListenerContext httpListenerContext = httpListener.GetContext();

                    // 3. Receive request/ do something/ create VM

                    // ceate VM and get vm_ID
                    vm_ID = string.Format("VM_ID:XiaoChen-123456-{0}-{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), DateTime.Now.Millisecond);
                    System.Console.WriteLine("[{0}] [Remote]  Receive request. ", DateTime.Now.ToLongTimeString());
                    System.Console.WriteLine("[{0}] [Remote]  Receive privillige, working...", DateTime.Now.ToLongTimeString());
                    System.Console.WriteLine("[{0}] [Remote]  {1} create successfully !", DateTime.Now.ToLongTimeString(), vm_ID);

                    // 4. Respone header / contents
                    httpListenerContext.Response.StatusCode = 200;
                    httpListenerContext.Response.Headers.Add(vm_ID); // Header, Key-Value 随意加
                    httpListenerContext.Response.ContentType = "text/plain";

                    using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                    {
                        // response as xml
                        writer.WriteLine("<?xml version=\"1.0\">");
                        writer.WriteLine("<niaoyun>");
                        writer.WriteLine("  <result>{0}</result>", vm_ID);
                        writer.WriteLine("  <message>Success</message>");
                        writer.WriteLine("  <version>V1.0</version>");
                        writer.WriteLine("  <time>{0}</time>", DateTime.Now.ToString("yyyyMMddhhmmss"));
                        writer.WriteLine("</niaoyun>");
                    }

                    // 5. Ending
                    System.Console.WriteLine("[{0}] [Remote]  Completed!\r\n ", DateTime.Now.ToLongTimeString());
                }
            })).Start();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            SampleIIS siis = new SampleIIS();
            siis.StartIIS();
        }
    }
}

 

C# 创建精简版IIS

标签:

原文地址:http://www.cnblogs.com/dzone/p/5178231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!