Exemple #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());
                }
            }
        }
Exemple #2
0
        public static HttpContent Create(string value, Encoding encoding, string contentType)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

            if (contentType != null)
            {
                HttpTextEncodingHelpers.EnsureContentTypeMatches(encoding, contentType);
            }

            var bytes = encoding.GetBytes(value);

            return(new HttpContent(new StringContent(value, encoding, bytes), contentType, bytes.LongLength));
        }
Exemple #3
0
        public static HttpContent Create(string value, string contentType)
        {
            var charsetEncoding = HttpTextEncodingHelpers.ExtractEncodingOrDefaultHttp(contentType);

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