Esempio n. 1
0
        /// <summary>
        /// 传入MediaID,WeChatPublic
        /// </summary>
        public WeChatResult Download(MediaParam mediaParam)
        {
            var param = new HttpParam
            {
                { "access_token", Token.GetAccessToken(mediaParam) },
                { "media_id", mediaParam.MediaID }
            };
            var url      = string.Format("{0}?{1}", ApiList.MediaGet, param.Format());
            var request  = HttpHelper.CreateRequest(url);
            var response = request.GetResponse();

            using (var stream = response.GetResponseStream())
            {
                var disposition = response.Headers["Content-disposition"];
                if (string.IsNullOrEmpty(disposition))
                {
                    var s = new StreamReader(stream, Encoding.UTF8).ReadToEnd();
                    return(JsonConvert.DeserializeObject <WeChatResult>(s));
                }
                var fs = new FileStream(mediaParam.FilePath, FileMode.OpenOrCreate);
                try
                {
                    var buffer = new byte[128 * 1024]; //128K
                    int i;
                    while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fs.Write(buffer, 0, i);
                    }

                    return(WeChatResult.GetSuccess(mediaParam.FilePath));
                }
                finally
                {
                    fs.Close();
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 传入MediaID,WeChatPublic
        /// </summary>
        public WeChatResult Download(MediaParam mediaParam)
        {
            var param = new HttpParam
            {
                {"access_token", Token.GetAccessToken(mediaParam)},
                {"media_id", mediaParam.MediaID}
            };
            var url = string.Format("{0}?{1}", ApiList.MediaGet, param.Format());
            var request = HttpHelper.CreateRequest(url);
            var response = request.GetResponse();
            using (var stream = response.GetResponseStream())
            {
                var disposition = response.Headers["Content-disposition"];
                if (string.IsNullOrEmpty(disposition))
                {
                    var s = new StreamReader(stream, Encoding.UTF8).ReadToEnd();
                    return JsonConvert.DeserializeObject<WeChatResult>(s);
                }
                var fs = new FileStream(mediaParam.FilePath, FileMode.OpenOrCreate);
                try
                {
                    var buffer = new byte[128 * 1024]; //128K
                    int i;
                    while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fs.Write(buffer, 0, i);
                    }

                    return WeChatResult.GetSuccess(mediaParam.FilePath);
                }
                finally
                {
                    fs.Close();
                }
            }
        }