Esempio n. 1
0
        public static byte[] GetHtmlPage(HttpListenerRequest request) {
            string format = @"<!DOCTYPE html>
                                <html lang=""en""> 
                                    <head><meta charset=""UTF-8"">{0}</head> 
                                    <body>{1}</body> 
                                </html>";
            string head = "<title>Test WebServer</title>";
            var body = new StringBuilder();
            body.Append("<h1>Request Info</h1>");
            body.Append("<h2>Request Header:</h2>");
            
            // Header infomation
            var headers = from key in request.Headers.AllKeys 
                    select $"<div>{key} : {string.Join(",", request.Headers.GetValues(key))}</div>";
            body.Append(string.Join("", headers));

            //Extract request properties
            body.Append("<h2>Request properties:</h2>");
            var properties = request.GetType().GetProperties();
            foreach (var property in properties)
            {
                var name_pro = property.Name;
                string value_pro;
                try {
                    value_pro = property.GetValue(request).ToString();
                }
                catch (Exception e)  {
                    value_pro = e.Message;
                }
                body.Append($"<div>{name_pro} : {value_pro}</div>");
                
            };
            string html = string.Format(format, head, body.ToString());                    
            return Encoding.UTF8.GetBytes(html);
        }