Пример #1
0
        /// <summary>
        /// 使用 POST 方式提交请求,需要调用方自行释放响应对象
        /// </summary>
        /// <param name="uri">请求路径</param>
        /// <param name="configuration">HTTP 配置</param>
        public static T Delete <T>(string uri, HttpConfiguration configuration)
        {
            if (configuration == null)
            {
                configuration = new HttpConfiguration <T>();
            }
            configuration.Method = HttpMethod.Delete;
            var response = WebHelper.Send(uri, configuration);

            return(WebHelper.ReadAsResult <T>(response, configuration));
        }
Пример #2
0
        // 从响应流中读取响应为实体
        static async Task <T> ReadAsResultAsync <T>(HttpResponseMessage response, HttpConfiguration configuration)
        {
            Stream stream = null;

            try
            {
                //Encoding encoding = null;
                //var content = response.Content;
                //if (content != null && content.Headers != null && content.Headers.ContentType != null && !string.IsNullOrEmpty(content.Headers.ContentType.CharSet))
                //    encoding = Encoding.GetEncoding(response.Content.Headers.ContentType.CharSet);
                //stream = await response.Content.ReadAsStreamAsync();
                //return WebHelper.ReadAsResult<T>(stream, encoding, deserializer);

                var      content      = response.Content;
                var      conf         = configuration as HttpConfiguration <T>;
                var      deserializer = conf != null ? conf.Deserializer : null;
                Encoding encoding     = configuration.Encoding;
                if (encoding == null)
                {
                    encoding = content != null && content.Headers != null && content.Headers.ContentType != null && !string.IsNullOrEmpty(content.Headers.ContentType.CharSet)
                        ? Encoding.GetEncoding(content.Headers.ContentType.CharSet)
                        : null;
                }

                stream = await response.Content.ReadAsStreamAsync();

                if (content.Headers != null && content.Headers.ContentEncoding != null && content.Headers.ContentEncoding.Contains("gzip"))
                {
                    stream = new GZipStream(stream, CompressionMode.Decompress);
                }
                else if (content.Headers != null && content.Headers.ContentEncoding != null && content.Headers.ContentEncoding.Contains("deflate"))
                {
                    stream = new DeflateStream(stream, CompressionMode.Decompress);
                }
                return(WebHelper.ReadAsResult <T>(stream, encoding, deserializer));
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
Пример #3
0
        // 从响应流中读取响应为实体
        static T ReadAsResult <T>(HttpWebResponse response, HttpConfiguration configuration)
        {
            Stream stream = null;

            try
            {
                var      conf         = configuration as HttpConfiguration <T>;
                var      deserializer = conf != null ? conf.Deserializer : null;
                Encoding encoding     = configuration.Encoding;
                if (encoding == null)
                {
                    encoding = !string.IsNullOrEmpty(response.CharacterSet)
                        ? Encoding.GetEncoding(response.CharacterSet)
                        : null;
                }

                stream = response.GetResponseStream();
                if (response.ContentEncoding == "gzip")
                {
                    stream = new GZipStream(stream, CompressionMode.Decompress);
                }
                else if (response.ContentEncoding == "deflate")
                {
                    stream = new DeflateStream(stream, CompressionMode.Decompress);
                }
                return(WebHelper.ReadAsResult <T>(stream, encoding, deserializer));
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
        }