Пример #1
0
        public async static Task <T> PostData <T>(T model, string url, string checktoken = "true")
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Add("checktoken", checktoken);

                    var account = SessionExtensions.Get <Accounts>(httpContextAccessor.HttpContext.Session, SessionExtensions.SessionAccount);
                    if (account != null)
                    {
                        httpClient.DefaultRequestHeaders.Add("token", account.Token);
                    }
                    string        body    = JsonConvert.SerializeObject(model);
                    StringContent content = new StringContent(body, Encoding.UTF8, "application/json");
                    httpClient.DefaultRequestVersion = HttpVersion.Version10;
                    using (var response = httpClient.PostAsync(url, content))
                    {
                        string apiResponse = await response.Result.Content.ReadAsStringAsync();

                        if (!response.Result.IsSuccessStatusCode)
                        {
                            if ((int)response.Result.StatusCode == (int)StatusCodes.Status401Unauthorized)
                            {
                                httpContextAccessor.HttpContext.Response.Redirect("/Error/Error401", true);
                                await httpContextAccessor.HttpContext.Response.WriteAsync($"<script> location.href = '/Error/Error401';</script>");

                                throw new Exception("401");
                            }

                            else
                            {
                                throw new Exception(apiResponse);
                            }
                        }
                        else
                        {
                            var data = JsonConvert.DeserializeObject <T>(apiResponse);
                            return(data);
                        }
                    }
                }
            }
            catch (Exception ex)
            { throw ex; }
            return(default(T));
        }
Пример #2
0
        public static async Task <T> GetData <T>(string url, string request = "", string checktoken = "true")
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Add("checktoken", checktoken);
                    if (checktoken == "true")
                    {
                        var account = SessionExtensions.Get <Accounts>(httpContextAccessor.HttpContext.Session, SessionExtensions.SessionAccount);
                        if (account != null)
                        {
                            httpClient.DefaultRequestHeaders.Add("token", account.Token);
                        }
                    }
                    httpClient.DefaultRequestVersion = HttpVersion.Version10;
                    StringContent content = new StringContent(request, Encoding.UTF8, "application/json");
                    using (var response = await httpClient.GetAsync(string.Format("{0}?{1}", url, request)))
                    {
                        //if(response.StatusCode == (int) StatusCodes.Status401Unauthorized)
                        //{

                        //}
                        string apiResponse = await response.Content.ReadAsStringAsync();

                        if (response.IsSuccessStatusCode)
                        {
                            var data = JsonConvert.DeserializeObject <T>(apiResponse);
                            return(data);
                        }
                        else
                        {
                            if ((int)response.StatusCode == (int)StatusCodes.Status401Unauthorized)
                            {
                                //httpContextAccessor.HttpContext.Response.Redirect("/Error/Error401", true);
                                await httpContextAccessor.HttpContext.Response.WriteAsync($"<script> location.href = '/Error/Error401';</script>");

                                throw new Exception(((int)StatusCodes.Status401Unauthorized).ToString());
                            }
                            else if ((int)response.StatusCode == (int)StatusCodes.Status404NotFound)
                            {
                                throw new Exception(((int)StatusCodes.Status404NotFound).ToString());
                            }
                            else
                            {
                                throw new Exception(apiResponse);
                            }
                        }
                        //if (data.Result == "200")
                        //{
                        //    return JsonConvert.DeserializeObject<T>(data.Data);
                        //}
                        //else
                        //{
                        //    ResponseMessage message = JsonConvert.DeserializeObject<ResponseMessage>(data.Data);
                        //    throw new Exception(message.Message);
                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }