Example #1
0
        public ApiResult <Stream> Download <TRequest>(string api, TRequest request)
            where TRequest : Request.MediaDownloadRequest
        {
            Request  = null;
            Response = null;

            var jsonParam = JsonConvert.SerializeObject(request, Formatting.None, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                ContractResolver  = new CamelCasePropertyNamesContractResolver()
            });

            var url = string.Concat(FxiaokeConfig.BaseUrl, api);

            Request = WebRequest.Create(url).AsHttpWebRequest()
                      .SetRequestObjWithJson(jsonParam)
                      .SetTimeout(30 * 1000);

            try
            {
                Response = Request.GetResponse().AsHttpWebResponse();
                if (Response.StatusCode != HttpStatusCode.OK)
                {
                    return(new ApiResult <Stream>
                    {
                        Message = Response.StatusDescription,
                        ErrorCode = (int)Response.StatusCode,
                    });
                }

                var stream = Response.AsHttpWebResponse().GetResponseStream();
                return(new ApiResult <Stream>
                {
                    Success = true,
                    Response = stream,
                    OriginalRequest = jsonParam,
                });
            }
            catch (Exception e)
            {
                return(new ApiResult <Stream>
                {
                    Message = e.Message,
                    ErrorCode = 99999,
                    OriginalRequest = jsonParam,
                    OriginalResponse = e.ToString(),
                });
            }
        }