usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Threading;usingSystem.Net;usingSystem.IO;namespacesampleiis{//Sample IIS class classSampleiis { Public voidStartiis () {stringURL ="http://localhost:8080/"; stringvm_id =string. Empty; HttpListener HttpListener=NewHttpListener (); Httplistener.authenticationschemes=authenticationschemes.anonymous; HTTPLISTENER.PREFIXES.ADD (URL); //1. Start ServerSystem.Console.WriteLine ("[{1}] [System] Start Server, Port {0} \ r \ n", URL, DateTime.Now.ToLongTimeString ()); Httplistener.start (); NewThread (NewThreadStart (Delegate { while(true) { //2. wating CommadSystem.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_idvm_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/contentsHttpListenerContext.Response.StatusCode = $; HTTPLISTENERCONTEXT.RESPONSE.HEADERS.ADD (vm_id); //Header, Key-value random addHttpListenerContext.Response.ContentType ="Text/plain"; using(StreamWriter writer =NewStreamWriter (HttpListenerContext.Response.OutputStream)) { //Response as XMLWriter. 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. EndingSystem.Console.WriteLine ("[{0}] [Remote] completed!\r\n", DateTime.Now.ToLongTimeString ()); } })). Start (); } } classProgram {Static voidMain (string[] args) {Sampleiis Siis=NewSampleiis (); Siis. Startiis (); } }}
C # Create a lite version of IIS