标签:
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(); } } }
标签:
原文地址:http://www.cnblogs.com/dzone/p/5178231.html