Beispiel #1
0
        public static async Task <ObjRetorno> CallApiPostMetodo(string controller, string metodo, object dados)
        {
            try
            {
                HttpClient client = new HttpClient();

                StringContent content = new StringContent(JsonConvert.SerializeObject(dados), Encoding.UTF8, "application/json");

                HttpResponseMessage response = await client.PostAsync(
                    caminho + controller + "/" + metodo, content);

                response.EnsureSuccessStatusCode();

                string apiResponse = await response.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <ObjRetorno>(apiResponse));
            }
            catch (Exception ex)
            {
                ObjRetorno ret = new ObjRetorno();
                ret.status   = 201;
                ret.mensagem = "Erro: " + ex.Message;
                return(ret);
            }
        }
Beispiel #2
0
        public static async Task <ObjRetorno> CallApiGetMetodo(string controller, string metodo, params string[] parametros)
        {
            try
            {
                HttpClient          client   = new HttpClient();
                HttpResponseMessage response = await client.GetAsync(caminho + controller + "/" +
                                                                     metodo + "/" + MontaParamentros(parametros));

                string apiResponse = await response.Content.ReadAsStringAsync();

                ObjRetorno ret = new ObjRetorno();
                ret.status   = 200;
                ret.mensagem = "";
                ret.dados    = apiResponse;
                return(ret);
            }
            catch (Exception ex)
            {
                ObjRetorno ret = new ObjRetorno();
                ret.status   = 201;
                ret.mensagem = "Erro: " + ex.Message;
                return(ret);
            }
        }