Exemplo n.º 1
0
        public static async Task <bool> DownloadFileAsync(string from, string to)
        {
            using (var wc = new WebClient())
            {
                try
                {
                    if (File.Exists(to))
                    {
                        File.Delete(to);
                    }
                    await wc.DownloadFileTaskAsync(from, to);

                    return(true);
                }
                catch (Exception ee)
                {
                    var li = new LogItem
                    {
                        App        = "Apteka.Utils",
                        Message    = ee.Message,
                        Stacktrace = ee.GetStackTrace(5),
                        Url        = $"{from} => {to}",
                        Method     = "CInet.DownloadFileAsync"
                    };
                    CLogJson.Write(li);
                    return(false);
                }
            }
        }
Exemplo n.º 2
0
        public static string GetRemoteString(string from, string token = "")
        {
            using (var wc = new WebClient())
            {
                try
                {
                    if (token != "")
                    {
                        wc.Headers.Add("Authorization", $"bearer {token}");
                    }

                    return(wc.DownloadString(from));
                }
                catch (Exception ee)
                {
                    var li = new LogItem
                    {
                        App        = "Apteka.Utils",
                        Message    = ee.Message,
                        Stacktrace = ee.GetStackTrace(5),
                        Url        = $"{from}  {token}",
                        Method     = "CInet.GetRemoteString"
                    };
                    CLogJson.Write(li);
                    return("");
                }
            }
        }
Exemplo n.º 3
0
        public static async Task <TReturn> Post <TParam, TReturn>(this string url, TParam param) where TParam : class
        {
            try
            {
                var c         = new HttpClient();
                var ServerUrl = ConfigurationManager.AppSettings["ServerUrl"];
                c.BaseAddress = new Uri(ServerUrl);
                c.DefaultRequestHeaders.Accept.Clear();
                c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var cd       = new StringContent(JsonConvert.SerializeObject(param), Encoding.UTF8, "application/json");
                var response = await c.PostAsync(url, cd);

                if (response.IsSuccessStatusCode)
                {
                    var searchResult = await response.Content.ReadAsStringAsync();

                    return(JsonConvert.DeserializeObject <TReturn>(searchResult));
                }
                else
                {
                    var li = new LogItem
                    {
                        App     = "Apteka.Utils",
                        Message = $"Алоқада хато => {response.StatusCode} {response.ReasonPhrase}",
                        Url     = url,
                        Method  = "CRest.Get"
                    };
                    CLogJson.Write(li);
                    throw new Exception(li.Message);
                }
            }
            catch (Exception ee)
            {
                var li = new LogItem
                {
                    App        = "Apteka.Utils",
                    Stacktrace = ee.GetStackTrace(5),
                    Message    = ee.GetAllMessages(),
                    Url        = url,
                    Method     = "CRest.Get"
                };
                CLogJson.Write(li);
                throw new Exception(li.Message);
            }
        }