Ejemplo n.º 1
0
        public T PostType(string route, dynamic data = default(dynamic), int userId = -1, string computerName = "")
        {
            try
            {
                ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("OperationUserId", userId.ToString());
                    client.DefaultRequestHeaders.Add("OperationComputerName", computerName);
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _parameters.Token);

                    var response = client.PostAsync(_parameters.ServiceUrl + route, new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")).Result;

                    var unused = response.StatusCode;
                    if (unused == HttpStatusCode.Unauthorized)
                    {
                        _parameters.Token = string.Empty;

                        var information = GetUserInformation();
                        if (information != null && information.UserId > decimal.Zero)
                        {
                            var token = JsonConvert.DeserializeObject <TokenModel>(HttpTokenService.RequestToken(Config.GetApiUrl(string.Empty), information.Username, CryptoHelper.Decrypt(information.Password), information.IsCustomer));
                            if (token.Error != "error" && token.AccessToken.IsNotNull())
                            {
                                var tokenModel = new TokenModelTemplate
                                {
                                    AccessToken = token.AccessToken,
                                    UserId      = token.UserId,
                                    User        = token.User
                                };

                                _parameters.Token = token.AccessToken;

                                return(PostType(route, data, userId, computerName));
                            }
                        }

                        return(default(T));
                    }
                    else
                    {
                        var dateTimeConverter = new IsoDateTimeConverter {
                            DateTimeFormat = "dd.MM.yyyy HH:mm:ss"
                        };
                        return(JsonConvert.DeserializeObject <T>(response.Content.ReadAsStringAsync().Result, dateTimeConverter));
                    }
                }
            }
            catch (Exception ex)
            {
                return(default(T));
            }
        }
Ejemplo n.º 2
0
        public T GetType(string route, dynamic data = default(dynamic), int userId = -1, string computerName = "")
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("OperationUserId", userId.ToString());
                    client.DefaultRequestHeaders.Add("OperationComputerName", computerName);

                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _parameters.Token);
                    var response = client.GetAsync(_parameters.ServiceUrl + route + (data != null ? "?" + ToQueryString(data) : default(string)), HttpCompletionOption.ResponseHeadersRead).Result;

                    var unused = response.StatusCode;
                    if (unused == HttpStatusCode.Unauthorized)
                    {
                        _parameters.Token = string.Empty;

                        var information = GetUserInformation();
                        if (information != null && information.UserId > decimal.Zero)
                        {
                            var token = JsonConvert.DeserializeObject <TokenModel>(HttpTokenService.RequestToken(Config.GetApiUrl(string.Empty), information.Username, CryptoHelper.Decrypt(information.Password), information.IsCustomer));
                            if (token.Error != "error" && token.AccessToken.IsNotNull())
                            {
                                var tokenModel = new TokenModelTemplate
                                {
                                    AccessToken = token.AccessToken,
                                    UserId      = token.UserId,
                                    User        = token.User
                                };

                                _parameters.Token = token.AccessToken;

                                return(PostType(route, data, userId, computerName));
                            }
                        }

                        return(default(T));
                    }
                    else
                    {
                        var dateTimeConverter = new IsoDateTimeConverter {
                            DateTimeFormat = "dd.MM.yyyy HH:mm:ss"
                        };
                        return(JsonConvert.DeserializeObject <T>(response.Content.ReadAsStringAsync().Result, dateTimeConverter));
                    }
                }
            }
            catch (Exception ex)
            {
                return(default(T));
            }
        }