Esempio n. 1
0
        public async Task <T> GetByAsync <T>(string appSetting, string apiController, Dictionary <string, string> paramList)
        {
            HttpClient client        = new HttpClient();
            var        urlAppSetting = SettingLib.GetAppSetting(appSetting);

            var builder = new UriBuilder(urlAppSetting.ToString() + apiController);

            builder.Port = -1;
            var query = HttpUtility.ParseQueryString(builder.Query);

            foreach (var param in paramList)
            {
                query[param.Key] = param.Value;
            }
            builder.Query = query.ToString();
            string url = builder.ToString();

            HttpResponseMessage response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                return(await response.Content.ReadAsAsync <T>());
            }
            return(await Task.FromResult(default(T)));
        }
Esempio n. 2
0
        public async Task <bool> PutXmlAsync <T>(string appSetting, string apiController, T data)
        {
            HttpClient client        = new HttpClient();
            var        httpContent   = new StringContent(ToXML(data));
            var        urlAppSetting = SettingLib.GetAppSetting(appSetting);
            var        result        = await client.PutAsync(urlAppSetting + apiController, httpContent);

            return(result.IsSuccessStatusCode);
        }
Esempio n. 3
0
        public async Task PostAsync <T>(string appSetting, string apiController, T data)
        {
            HttpClient client        = new HttpClient();
            var        urlAppSetting = SettingLib.GetAppSetting(appSetting);

            var myContent   = JsonConvert.SerializeObject(data);
            var buffer      = System.Text.Encoding.UTF8.GetBytes(myContent);
            var byteContent = new ByteArrayContent(buffer);

            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            await client.PostAsync(urlAppSetting + apiController, byteContent);
        }
Esempio n. 4
0
        public async Task <T> GetByIdAsync <T>(string appSetting, string apiController, int id)
        {
            HttpClient          client        = new HttpClient();
            var                 urlAppSetting = SettingLib.GetAppSetting(appSetting);
            HttpResponseMessage response      = await client.GetAsync(urlAppSetting + apiController + id.ToString());

            if (response.IsSuccessStatusCode)
            {
                return(await response.Content.ReadAsAsync <T>());
            }
            return(await Task.FromResult(default(T)));
        }
Esempio n. 5
0
        public async Task PutAsync <T>(string appSetting, string apiController, int id, T data)
        {
            HttpClient client        = new HttpClient();
            var        urlAppSetting = SettingLib.GetAppSetting(appSetting);

            var myContent   = JsonConvert.SerializeObject(data);
            var buffer      = System.Text.Encoding.UTF8.GetBytes(myContent);
            var byteContent = new ByteArrayContent(buffer);

            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var result = await client.PutAsync(urlAppSetting + apiController + "/" + id, byteContent);

            if (!result.IsSuccessStatusCode)
            {
            }
        }
Esempio n. 6
0
        public async Task DeleteAsync(string appSetting, string apiController, Dictionary <string, string> paramList)
        {
            HttpClient client        = new HttpClient();
            var        urlAppSetting = SettingLib.GetAppSetting(appSetting);

            var builder = new UriBuilder(urlAppSetting.ToString() + apiController);

            builder.Port = -1;
            var query = HttpUtility.ParseQueryString(builder.Query);

            foreach (var param in paramList)
            {
                query[param.Key] = param.Value;
            }
            builder.Query = query.ToString();
            string url    = builder.ToString();
            var    result = await client.DeleteAsync(url);
        }