//cast HttpMessage to string private static string CastMessage(HttpMessage msg) { string response = ""; if (msg != null) { response = string.Format("{0} {1} {2}\r\n", msg.HttpVersion, msg.StatusCode, msg.Status); foreach (PropertyInfo pro in msg.GetType().GetProperties()) { var attr = pro.GetCustomAttribute(typeof(HttpHeaderAttribute)); HttpHeaderAttribute he = attr as HttpHeaderAttribute; if (he != null) { if (!string.IsNullOrEmpty(he.headerName)) { if (pro.GetValue(msg) != null) { response += string.Format("{0}:{1}\r\n", he.headerName, pro.GetValue(msg).ToString()); } } } } if (!string.IsNullOrEmpty(msg.MessageBodyResponse)) { response += "\r\n\r\n " + msg.MessageBodyResponse; } } return(response); }
//cast string to HttpMessage private static HttpMessage CastMessage(string httpQuery) { HttpMessage msg = new HttpMessage(); httpQuery = httpQuery.Replace(@"\0", ""); msg.Method = Regex.Match(@httpQuery, @"GET|POST").Value; msg.Status = Regex.Match(@httpQuery, @"HTTP\/[0-9]\.[0-9]\s[0-9]{1,3}").Value.Replace(msg.HttpVersion, ""); msg.UrlResource = GetResourceRequest(httpQuery); foreach (PropertyInfo pro in msg.GetType().GetProperties()) { var attr = pro.GetCustomAttribute(typeof(HttpHeaderAttribute)); HttpHeaderAttribute he = attr as HttpHeaderAttribute; if (he != null) { if (!string.IsNullOrEmpty(he.headerName)) { var pattern = @he.headerName + @":\s(.)*(\r\n|;)"; var match = Regex.Match(httpQuery, pattern); string value = string.IsNullOrEmpty(match.Value)?null:Regex.Replace(match.Value, @he.headerName + @":\s", ""); if (value != null) { value = value.Replace(@"\r\n", ""); } pro.SetValue(msg, value); } } } return(msg); }