private static void Main(string[] args) { //if HttpListener is not supported by the Framework if (!HttpListener.IsSupported) { Console.WriteLine("A more recent Windows version is required to use the HttpListener class."); return; } // Create a listener. HttpListener listener = new HttpListener(); // Add the prefixes. if (args.Length != 0) { foreach (string s in args) { listener.Prefixes.Add(s); // don't forget to authorize access to the TCP/IP addresses localhost:xxxx and localhost:yyyy // with netsh http add urlacl url=http://localhost:xxxx/ user="******" // and netsh http add urlacl url=http://localhost:yyyy/ user="******" // user="******" is language dependent, use user=Everyone in english } } else { Console.WriteLine("Syntax error: the call must contain at least one web server url as argument"); } listener.Start(); // get args foreach (string s in args) { Console.WriteLine("Listening for connections on " + s); } // Trap Ctrl-C on console to exit Console.CancelKeyPress += delegate { // call methods to close socket and exit listener.Stop(); listener.Close(); Environment.Exit(0); }; while (true) { // Note: The GetContext method blocks while waiting for a request. HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; string documentContents; using (Stream receiveStream = request.InputStream) { using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) { documentContents = readStream.ReadToEnd(); } } // get url Console.WriteLine($"Received request for {request.Url}"); //get url protocol Console.WriteLine(request.Url.Scheme); //get user in url Console.WriteLine(request.Url.UserInfo); //get host in url Console.WriteLine(request.Url.Host); //get port in url Console.WriteLine(request.Url.Port); //get path in url Console.WriteLine(request.Url.LocalPath); // parse path in url foreach (string str in request.Url.Segments) { Console.WriteLine(str); } //get params un url. After ? and between & Console.WriteLine(request.Url.Query); //parse params in url //Console.WriteLine("param1 = " + HttpUtility.ParseQueryString(request.Url.Query).Get("param1")); //Console.WriteLine("param2 = " + HttpUtility.ParseQueryString(request.Url.Query).Get("param2")); //Console.WriteLine("param3 = " + HttpUtility.ParseQueryString(request.Url.Query).Get("param3")); //Console.WriteLine("param4 = " + HttpUtility.ParseQueryString(request.Url.Query).Get("param4")); // Console.WriteLine(documentContents); // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a response. Mymethods mymethods = new Mymethods(); // Les URLs utilisées pour Le TD2 sont les URLS Suivantes : //Pour tester la méthode parseURL pour la question 4 du TD2 on utilise l'URL suivante : http://localhost:8080/cgi/parseURL?name=Ayoub&course=Eiin839 //Pour tester la méthode callExe pour la question 5 du TD2 on utilise l'URL suivante :http://localhost:8080/cgi/callExe?name=Ayoub&course=Eiin839 string str1 = (string)request.Url.Segments[request.Url.Segments.Length - 1]; string responseString = "<HTML><BODY> Hello world!</BODY></HTML>"; if (str1 != "favicon.ico") { if (str1 == "parseURL") { Type magicType = typeof(Mymethods); MethodInfo mymethod = magicType.GetMethod(str1); object[] parametersArray = new object[] { request }; responseString = (string)mymethod.Invoke(mymethods, parametersArray); } else { responseString = mymethods.callExe(); } } byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get a response stream and write the response to it. response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); // You must close the output stream. output.Close(); } // Httplistener neither stop ... But Ctrl-C do that ... // listener.Stop(); // Les URLs utilisées pour Le TD2 sont les URLS Suivantes : //Pour tester la méthode parseURL pour la question 4 du TD2 on utilise l'URL suivante : http://localhost:8080/cgi/parseURL?name=Ayoub&course=Eiin839 //Pour tester la méthode callExe pour la question 5 du TD2 on utilise l'URL suivante :http://localhost:8080/cgi/callExe?name=Ayoub&course=Eiin839 }
private static void Main(string[] args) { //if HttpListener is not supported by the Framework if (!HttpListener.IsSupported) { Console.WriteLine("A more recent Windows version is required to use the HttpListener class."); return; } // Create a listener. HttpListener listener = new HttpListener(); // Add the prefixes. if (args.Length != 0) { foreach (string s in args) { listener.Prefixes.Add(s); // don't forget to authorize access to the TCP/IP addresses localhost:xxxx and localhost:yyyy // with netsh http add urlacl url=http://localhost:xxxx/ user="******" // and netsh http add urlacl url=http://localhost:yyyy/ user="******" // user="******" is language dependent, use user=Everyone in english } } else { Console.WriteLine("Syntax error: the call must contain at least one web server url as argument"); } listener.Start(); // get args foreach (string s in args) { Console.WriteLine("Listening for connections on " + s); } // Trap Ctrl-C on console to exit Console.CancelKeyPress += delegate { // call methods to close socket and exit listener.Stop(); listener.Close(); Environment.Exit(0); }; while (true) { // Note: The GetContext method blocks while waiting for a request. HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; string documentContents; using (Stream receiveStream = request.InputStream) { using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) { documentContents = readStream.ReadToEnd(); } } // get url Console.WriteLine($"Received request for {request.Url}"); //get url protocol Console.WriteLine(request.Url.Scheme); //get user in url Console.WriteLine(request.Url.UserInfo); //get host in url Console.WriteLine(request.Url.Host); //get port in url Console.WriteLine(request.Url.Port); //get path in url Console.WriteLine(request.Url.LocalPath); //parse params in url string param1 = HttpUtility.ParseQueryString(request.Url.Query).Get("param1"); string param2 = HttpUtility.ParseQueryString(request.Url.Query).Get("param2"); string responseString = ""; Type type = typeof(Mymethods); Mymethods c = new Mymethods(param1, param2); // parse path in url foreach (string str in request.Url.Segments) { if (str.ToLower() == "mymethod") { MethodInfo mymethod = type.GetMethod("mymethod"); responseString = (string)mymethod.Invoke(c, null); Console.WriteLine(responseString); } } string[] tab = { param1, param2 }; //get params un url. After ? and between & Console.WriteLine(request.Url.Query); // Console.WriteLine(documentContents); // Call External Mehtod if (!request.Url.LocalPath.Contains("myMethod")) { MethodInfo external = type.GetMethod("callExternalMethode"); external.Invoke(c, null); } // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a response. responseString = c.mymethod(); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get a response stream and write the response to it. response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); // You must close the output stream. output.Close(); } // Httplistener neither stop ... But Ctrl-C do that ... // listener.Stop(); }
private static void Main(string[] args) { //if HttpListener is not supported by the Framework if (!HttpListener.IsSupported) { Console.WriteLine("A more recent Windows version is required to use the HttpListener class."); return; } // Create a listener. HttpListener listener = new HttpListener(); // Add the prefixes. if (args.Length != 0) { foreach (string s in args) { listener.Prefixes.Add(s); // don't forget to authorize access to the TCP/IP addresses localhost:xxxx and localhost:yyyy // with netsh http add urlacl url=http://localhost:xxxx/ user="******" // and netsh http add urlacl url=http://localhost:yyyy/ user="******" // user="******" is language dependent, use user=Everyone in english } } else { Console.WriteLine("Syntax error: the call must contain at least one web server url as argument"); } listener.Start(); // get args foreach (string s in args) { Console.WriteLine("Listening for connections on " + s); } // Trap Ctrl-C on console to exit Console.CancelKeyPress += delegate { // call methods to close socket and exit listener.Stop(); listener.Close(); Environment.Exit(0); }; while (true) { // Note: The GetContext method blocks while waiting for a request. HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; Header headers = new Header(request.Headers); string documentContents; using (Stream receiveStream = request.InputStream) { using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8)) { documentContents = readStream.ReadToEnd(); } } // get url Console.WriteLine($"Received request for {request.Url}"); //get url protocol Console.WriteLine(request.Url.Scheme); //get user in url Console.WriteLine(request.Url.UserInfo); //get host in url Console.WriteLine(request.Url.Host); //get port in url Console.WriteLine(request.Url.Port); //get path in url Console.WriteLine(request.Url.LocalPath); string responseString = ""; Mymethods m = new Mymethods(); // parse path in url foreach (string str in request.Url.Segments) { Console.WriteLine(str); Type type = typeof(Mymethods); MethodInfo method = type.GetMethod(str); if (method != null) { Mymethods c = new Mymethods(); responseString = (string)method.Invoke(c, new string[] { HttpUtility.ParseQueryString(request.Url.Query).Get("param1"), HttpUtility.ParseQueryString(request.Url.Query).Get("param2") }); } } /** * Affichage de tout les headers */ headers.printAllInfos(); /** * Affichage du user-agent (infos du browser qui fait la requête) */ headers.printHeader("User-Agent"); /** * Je renvoie une reponse approprié a mon navigateur (peut servir pour la responsivité, voire d'autres) */ if (headers.getHeaders().Get("User-Agent").Contains("Mobile")) { responseString += "<br>Tu es sur un mobile"; } else { responseString += "<br>Tu es sur un ordinateur"; } //get params un url. After ? and between & Console.WriteLine(request.Url.Query); //parse params in url Console.WriteLine("param1 = " + HttpUtility.ParseQueryString(request.Url.Query).Get("param1")); Console.WriteLine("param2 = " + HttpUtility.ParseQueryString(request.Url.Query).Get("param2")); Console.WriteLine("param3 = " + HttpUtility.ParseQueryString(request.Url.Query).Get("param3")); Console.WriteLine("param4 = " + HttpUtility.ParseQueryString(request.Url.Query).Get("param4")); Console.WriteLine(documentContents); // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a response. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get a response stream and write the response to it. response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); // You must close the output stream. output.Close(); } // Httplistener neither stop ... But Ctrl-C do that ... // listener.Stop(); }