private HttpRequest GetRequest(Stream inputStream) { //Read Request Line string requestLine = StreamUtilities.ReadLine(inputStream); string[] tokens = requestLine.Split(' '); if (tokens.Length != 3) { throw new Exception("Invalid http request line"); } RequestMethod method = (RequestMethod)Enum.Parse(typeof(RequestMethod), tokens[0].ToUpper()); string url = tokens[1]; string protocolVersion = tokens[2]; //Read Headers Header header = new Header(HeaderTypes.HttpRequest); string line; while ((line = StreamUtilities.ReadLine(inputStream)) != null) { if (line.Equals("")) { break; } int separator = line.IndexOf(':'); if (separator == -1) { throw new Exception("Invalid http header line: " + line); } string name = line.Substring(0, separator); int pos = separator + 1; while ((pos < line.Length) && (line[pos] == ' ')) { pos++; } string value = line.Substring(pos, line.Length - pos); if (name == "Cookie") { string[] cookieSaves = value.Split(';'); foreach (var cookieSave in cookieSaves) { string[] cookiePair = cookieSave.Split('=').Select(x => x.Trim()).ToArray(); var cookie = new Cookie(cookiePair[0], cookiePair[1]); header.Cookies.Add(cookie); } } else if (name == "Content-Length") { header.ContentLength = value; } else { header.OtherParameters.Add(name, value); } } string content = null; if (header.ContentLength != null) { int totalBytes = Convert.ToInt32(header.ContentLength); int bytesLeft = totalBytes; byte[] bytes = new byte[totalBytes]; while (bytesLeft > 0) { byte[] buffer = new byte[bytesLeft > 1024 ? 1024 : bytesLeft]; int n = inputStream.Read(buffer, 0, buffer.Length); buffer.CopyTo(bytes, totalBytes - bytesLeft); bytesLeft -= n; } content = Encoding.ASCII.GetString(bytes); } var request = new HttpRequest() { Method = method, Url = url, Header = header, Content = content }; Console.WriteLine("-REQUEST-----------------------------"); Console.WriteLine(request); Console.WriteLine("------------------------------"); return(request); }