private static void readBody(DataObject response, Socket socket) { // header is received, parsing content length string contentLengthMatch = MessageParser.getHeaderValue(response.getStringRepresentation(), HttpHeaders.ContentLength); string transferEncodingMatch = MessageParser.getHeaderValue(response.getStringRepresentation(), HttpHeaders.TransferEncoding); if (contentLengthMatch != string.Empty) { Console.WriteLine(contentLengthMatch); int contentLength = int.Parse(contentLengthMatch); byte[] bodyBuff; int receivedLength = 0; // read the body for (int i = 0; i < contentLength; i++) { bodyBuff = new byte[1]; receivedLength = socket.Receive(bodyBuff, 0, 1, 0); response.appendStringRepresentation(bodyBuff); } } else if (transferEncodingMatch == "chunked") { byte[] bodyBuff; byte[] lengthBuff; DataObject currentLength = new DataObject(); // while there are available bytes of data to receive while (socket.Available > 0) { // receiving length of chunk lengthBuff = new byte[1]; socket.Receive(lengthBuff, 0, 1, 0); currentLength.appendStringRepresentation(lengthBuff); // if length of chunk is known if (currentLength.getStringRepresentation().Contains("\r\n")) { // format length of chunk from hexadecimal to decimal string hexNumberFormat = currentLength.getStringRepresentation().Substring( 0, currentLength.getStringRepresentation().IndexOf("\r\n")); int length = Convert.ToInt32(hexNumberFormat, 16); // end of body if (length == 0) { break; } int receivedLength = 0; // receiving chunk for (int i = 0; i < length + 2; i++) { bodyBuff = new byte[1]; receivedLength += socket.Receive(bodyBuff, 0, 1, 0); response.appendStringRepresentation(bodyBuff); } // clear length of next chunk currentLength.clear(); } } } }