thamaraaalves
3/5/2018 - 8:26 PM

webrequest

   private static IRestRequest CreateJsonRequest(string resource)
        {
            var request = new RestRequest(resource)
            {
                RequestFormat = DataFormat.Json,
                JsonSerializer =
                {
                    ContentType = JsonContentType
                }
            };
            return request;
        }



 [HttpGet]
        [Route("AutenticationChart")]
        [ResponseType(typeof(List<AutenticationChartViewModel>))]
        public IHttpActionResult AutenticationChart()
        {
            var token = string.Empty;
            var request = CreateJsonRequest("/AutenticationChart");
            var api = new ClientRest(httpRestFul, token);
            var cards = api.Execute<ResponseWraper>(request);
            return Json(cards.resultado);
        }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
//using Mobile.Comuns.Filter;
//using Mobile.Struct;
using RestSharp;
using SendMailOrdenado.Demo.Core.ViewModels;
using System.Threading.Tasks;
using RestSharp.Authenticators;
using Newtonsoft.Json;

namespace SendMailOrdenado.Demo.WebApi.Controllers.Api
{
    public class ClientRest
    {
        #region Variáveis e inicializador

        private const string JsonContentType = "application/json; charset=utf-8";
        public string ServiceBaseUrl { get; private set; }
        public string Token { get; private set; }

        public ClientRest(string serviceBaseUrl, string token)
        {
            if (token == null) throw new ArgumentNullException("token");
            ServiceBaseUrl = serviceBaseUrl;
            Token = token;
        }

        #endregion
       
        #region Métodos GET

        public Task<T> ExecuteAssync<T>(RestRequest request) where T : new()
        {
            var client = new RestClient();
            var taskCompletionSource = new TaskCompletionSource<T>();
            client.BaseUrl = new RestClient(ServiceBaseUrl).BaseUrl;
            //client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
            //request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment);
            client.ExecuteAsync<T>(request, (response) => taskCompletionSource.SetResult(response.Data));
            return taskCompletionSource.Task;
        }


        public T Execute<T>(IRestRequest request) where T : new()
        {
            var client = new RestClient(ServiceBaseUrl);         
            request.Method = Method.GET;
            request.AddHeader("Token", Token);
            var retorno = client.Execute<T>(request);
            return retorno.Data;
        }

        #endregion
    }
}