程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C# 創建精簡版IIS代碼

C# 創建精簡版IIS代碼

編輯:C#基礎知識

 

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();
        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved