示例#1
0
        private static void Main(string[] args)
        {
            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();
            foreach (string s in args)
            {
                Console.WriteLine("Listening for connections on " + s);
            }

            while (true)
            {
                // Note: The GetContext method blocks while waiting for a request.
                HttpListenerContext context = listener.GetContext();
                HttpListenerRequest request = context.Request;
                Header header = new Header(request);

                string documentContents;
                using (Stream receiveStream = request.InputStream)
                {
                    using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
                    {
                        documentContents = readStream.ReadToEnd();
                    }
                }
                header.printAllHeader();
                header.printHeader(HttpRequestHeader.ContentType);     //MIME
                header.printHeader(HttpRequestHeader.Cookie);          //Cookie
                header.printHeader(HttpRequestHeader.UserAgent);       // L’agent utilisateur demandeur
                header.printHeader(HttpRequestHeader.AcceptEncoding);  //  les encodages de contenu admis pour la réponse
                header.printHeader(HttpRequestHeader.Authorization);   // les informations d’identification que le client doit présenter pour s’authentifier auprès du serveur
                header.printHeader(HttpRequestHeader.ContentLanguage); // langages naturels préférés pour la réponse
                header.printHeader(HttpRequestHeader.AcceptCharset);   //les jeux de caractères admis pour la réponse
                header.printHeader(HttpRequestHeader.Allow);           // le jeu de méthodes HTTP pris en charge
                Console.WriteLine($"Received request for {request.Url}");
                Console.WriteLine(documentContents);


                // Obtain a response object.
                HttpListenerResponse response = context.Response;

                // Construct a response.
                string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
                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 ...
            // listener.Stop();
        }
示例#2
0
        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();
        }