Exemplo n.º 1
0
        public static async Task <T> Get <T>(string urlApi, string action, object dynamicPara, string applicationTag = "cms")
        {
            urlApi += "/" + action + (dynamicPara == null? "":"/?" + URLBuilder.GetQueryString(dynamicPara));
            Uri _baseAddress = URLBuilder.APIBaseAddress("webapi:" + applicationTag);

            using (var httpClient = new HttpClient())
            {
                try
                {
                    httpClient.DefaultRequestHeaders.Accept.Clear();
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(APIConstant.ContentType));
                    httpClient.BaseAddress = _baseAddress;

                    var response = await httpClient.GetAsync(apiPrefix + urlApi);

                    if (!response.IsSuccessStatusCode)
                    {
                        return(default(T));                               //Check status code
                    }
                    var data = response.Content.ReadAsAsync <T>(new[] { new ProtoBufFormatter() }).Result;

                    return(data == null ? default(T) : data);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Exemplo n.º 2
0
        public static T Authorize_GetNonAsync <T>(string token, string urlApi, string action, object dynamicPara, string applicationTag = "cms", string ars = null)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            using (var httpClient = HttpClientHelper.GetHttpClient(token))
            {
                try
                {
                    urlApi += "/" + action + (dynamicPara == null ? "" : "/?" + URLBuilder.GetQueryString(dynamicPara));
                    Uri _baseAddress = URLBuilder.APIBaseAddress("webapi:" + applicationTag);

                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(APIConstant.ContentType));
                    httpClient.BaseAddress = _baseAddress;
                    if (ars != null && ars.Length > 0)
                    {
                        httpClient.DefaultRequestHeaders.TryAddWithoutValidation(APIConstant.XAuthorizeHeaderARS, ars);
                    }

                    var response = httpClient.GetAsync(apiPrefix + urlApi).Result;

                    if (!response.IsSuccessStatusCode)
                    {
                        var errorMess = response.Content.ReadAsStringAsync().Result;
                        if (errorMess.Length > 0)
                        {
                            Exception ex = new Exception(errorMess);
                            Logger.LogError(ex);
                        }
                        throw new HttpException((int)response.StatusCode, errorMess);
                    }

                    var data = response.Content.ReadAsAsync <T>(new[] { new ProtoBufFormatter() }).Result;

                    return(data == null ? default(T) : data);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }