Exemplo n.º 1
0
        //post type
        public async Task <string> PostRequst <T>(string apiUrl, T t)
        {
            using (HttpClient client = new HttpClient())
            {
                HttpContent contentPost = new StringContent(ConvertJsonToObject.AsJson <T>(t), Encoding.UTF8,
                                                            "application/json");
                HttpResponseMessage response = await client.PostAsync(new Uri(apiUrl), contentPost);

                if (response.IsSuccessStatusCode)
                {
                    return("Success");
                }
                return("Failed");
            }
        }
Exemplo n.º 2
0
        //get a type
        public async Task <T> GetRequst <T>(string apiUrl)
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(apiUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync(apiUrl);

                var data = "";
                if (response.IsSuccessStatusCode)
                {
                    data = await response.Content.ReadAsStringAsync();
                }
                return(ConvertJsonToObject.AsObject <T>(data));
            }
        }