/// <summary> /// Parses a line from an HTTP request or response message containing a header name and value pair. /// </summary> /// <param name="line">A string containing the data to be parsed.</param> /// <param name="headers">A reference to a <see cref="System.Net.Http.Headers.HttpHeaders"/> collection to which the parsed header will be added.</param> /// <param name="contentHeaders">A reference to a <see cref="System.Net.Http.Headers.HttpHeaders"/> collection for the message content, to which the parsed header will be added.</param> private void ParseHeader(string line, System.Net.Http.Headers.HttpHeaders headers, System.Net.Http.Headers.HttpHeaders contentHeaders) { //Header format is //name: value int headerKeySeparatorIndex = line.IndexOf(":", StringComparison.OrdinalIgnoreCase); string headerName = line.Substring(0, headerKeySeparatorIndex).Trim(); string headerValue = line.Substring(headerKeySeparatorIndex + 1).Trim(); //Not sure how to determine where request headers and and content headers begin, //at least not without a known set of headers (general headers first the content headers) //which seems like a bad way of doing it. So we'll assume if it's a known content header put it there //else use request headers. IList <string> values = ParseValues(headerValue); System.Net.Http.Headers.HttpHeaders headersToAddTo = IsContentHeader(headerName) ? contentHeaders : headers; if (values.Count > 1) { headersToAddTo.TryAddWithoutValidation(headerName, values); } else { headersToAddTo.TryAddWithoutValidation(headerName, values.First()); } }