Exemple #1
0
        internal static string ReadBufferAsString(ArraySegment <byte> buffer, HttpContentHeaders headers)
        {
            Encoding encoding       = (Encoding)null;
            int      preambleLength = -1;
            string   charSet        = headers.ContentType?.CharSet;

            if (charSet != null)
            {
                try
                {
                    encoding       = charSet.Length <= 2 || charSet[0] != '"' || charSet[charSet.Length - 1] != '"' ? Encoding.GetEncoding(charSet) : Encoding.GetEncoding(charSet.Substring(1, charSet.Length - 2));
                    preambleLength = HttpContent.GetPreambleLength(buffer, encoding);
                }
                catch (ArgumentException ex)
                {
                    throw new InvalidOperationException(SR.net_http_content_invalid_charset, (Exception)ex);
                }
            }
            if (encoding == null && !HttpContent.TryDetectEncoding(buffer, out encoding, out preambleLength))
            {
                encoding       = HttpContent.DefaultStringEncoding;
                preambleLength = 0;
            }
            return(encoding.GetString(buffer.Array, buffer.Offset + preambleLength, buffer.Count - preambleLength));
        }