예제 #1
0
        static async Task <String> VotePollItem(int idPoll, int idPollItem)
        {
            try
            {
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(uriApi);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var param = new ModelVotacao()
                {
                    idPoll     = idPoll,
                    idPollItem = idPollItem
                };

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

                HttpResponseMessage response = await client.PostAsync("api/enquetevotacao", content);//.Result;

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return(response.Content.ReadAsStringAsync().Result);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
예제 #2
0
        public HttpResponseMessage PostVote([FromBody] ModelVotacao votacao)
        {
            Arquivo arquivo = new Arquivo();
            var     retorno = arquivo.Vote(votacao.idPoll, votacao.idPollItem);

            if (retorno)
            {
                object obj = new
                {
                    option_id = votacao.idPollItem
                };

                return(new HttpResponseMessage()
                {
                    Content = new StringContent(JsonConvert.SerializeObject(obj))
                });
            }
            else
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
        }