Esempio n. 1
0
        public string ReadAsString()
        {
            HttpContent content  = this;
            var         encoding = HttpTextEncodingHelpers.ExtractEncodingOrDefaultHttp(content.ContentType);

            if (content.HasLength())
            {
                var    preamble = encoding.GetPreamble();
                var    bytes    = content.ReadAsByteArray();
                string s;
                if (preamble.SequenceEqual(bytes.Take(preamble.Length)))
                {
                    s = encoding.GetString(bytes, preamble.Length, bytes.Length - preamble.Length);
                }
                else
                {
                    s = encoding.GetString(bytes);
                }
                return(s);
            }

            using (var stream = content.ReadAsStream())
            {
                using (var reader = new StreamReader(stream, encoding))
                {
                    return(reader.ReadToEnd());
                }
            }
        }
Esempio n. 2
0
        public static HttpContent Create(string value, string contentType)
        {
            var charsetEncoding = HttpTextEncodingHelpers.ExtractEncodingOrDefaultHttp(contentType);

            return(Create(value, charsetEncoding, contentType));
        }