示例#1
0
        public static GetAPIResult <T> APIGet <T>(string resource, string accessToken)
        {
            GetAPIResult <T> result = new GetAPIResult <T>();

            try
            {
                DataManager dataManager   = new DataManager();
                var         ResultSetting = dataManager.GetSetting();
                if (ResultSetting != null)
                {
                    using (HttpClient httpClient = new HttpClient())
                    {
                        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ResultSetting.ServiceToken);
                        var methodUri = new System.Uri(new System.Uri(ResultSetting.ServiceEndPoint), resource);
                        HttpResponseMessage apiResponse = httpClient.GetAsync(methodUri).Result;
                        result.HttpStatus = apiResponse.StatusCode;
                        result.Headers    = new StringBuilder().Append(apiResponse.Headers).ToString();
                        result.Request    = new StringBuilder().Append(apiResponse.RequestMessage).ToString();
                        result.Content    = apiResponse.Content.ReadAsStringAsync().Result;
                        if (!result.Content.StartsWith("["))
                        {
                            result.Content = "[" + apiResponse.Content.ReadAsStringAsync().Result + "]";
                        }
                        bool status = apiResponse.IsSuccessStatusCode;
                        if (status)
                        {
                            if (typeof(T).IsArray && typeof(T).GetElementType() == typeof(byte))
                            {
                                result.DataColl = (List <T>)(object) result.Content;
                            }
                            else if (typeof(T) == typeof(string))
                            {
                                result.DataColl = (List <T>)(object) result.Content;
                            }
                            else if (typeof(T) == typeof(string))
                            {
                                result.DataColl = (List <T>)(object) result.Content;
                            }
                            else
                            {
                                result.DataColl = JsonConvert.DeserializeObject <List <T> >(result.Content);
                            }
                        }
                    }
                }
                else
                {
                    result.KnownException = "Base Web Api Address is not avilable";
                }
            }
            catch (Exception ex)
            {
                result.KnownException = "Please contact to Administrator";
            }
            return(result);
        }
示例#2
0
        //private async Task<List<UserDetail>> GetLoginAsync(string user)
        //{
        //    List<UserDetail> obj = null;
        //    try
        //    {
        //        GetAPIResult<UserDetail> api = CommonFunctions.APIGet<UserDetail>("GetUser/" + user, string.Empty);
        //        if (api.HttpStatus == System.Net.HttpStatusCode.OK)
        //        {
        //            obj = api.DataColl;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw;
        //    }
        //    return obj;
        //}

        private List <UserDetail> GetLogin(string user, string password)
        {
            List <UserDetail> obj = null;

            try
            {
                GetAPIResult <UserDetail> api = CommonFunctions.APIGet <UserDetail>("auth/login/" + user, string.Empty);
                if (api.HttpStatus == System.Net.HttpStatusCode.OK)
                {
                    obj = api.DataColl;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return(obj);
        }