SHARE
TWEET

Consumir API

sangueroots Jul 4th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ResultClientEstado
  2. {
  3.    public int id;
  4.    public int codigo_uf;
  5.    public string estado;
  6.    public string uf;
  7. }
  8.  
  9. public class Estados
  10. {
  11.    public int Id;
  12.    public int CodigoUF;
  13.    public string Estado;
  14.    public string UF;
  15.  
  16.    public Estados(int id, int codigoUf, string estado, string uf)
  17.    {
  18.        Id = id;
  19.        CodigoUF = codigoUf;
  20.        Estado = estado;
  21.        UF = uf;
  22.    }
  23. }
  24.  
  25. public IEnumerable<Estados> obterEstados(int id)
  26. {
  27.     using (var client = new HttpClient())
  28.     {
  29.         var url = string.Format("http://api.uira.com.br/public/estados");
  30.         var result = JsonConvert.DeserializeObject<List<ResultClientEstado>>(client.GetStringAsync(url).Result);
  31.        
  32.         //Console.WriteLine(result);
  33.    
  34.         return result.AsParallel().Select(x => new Estados(x.id, x.codigo_uf, x.estado, x.uf));
  35.     }
  36. }
  37.  
  38. public static class Rest
  39. {
  40.     public static dynamic Get(string url)
  41.     {
  42.         using (var client = new HttpClient())
  43.         {
  44.             return JsonConvert.DeserializeObject<dynamic>(client.GetStringAsync(url).Result);
  45.         }
  46.     }
  47. }
  48.  
  49. public static class ApiRest{
  50.  
  51.     public static dynamic Get(string url)
  52.     {
  53.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  54.         request.Method = "GET";
  55.         request.ContentType = "application/json";
  56.    
  57.         try {
  58.             WebResponse webResponse = request.GetResponse();
  59.             Stream webStream = webResponse.GetResponseStream();
  60.             StreamReader responseReader = new StreamReader(webStream);
  61.            
  62.             string response = responseReader.ReadToEnd();
  63.             responseReader.Close();
  64.            
  65.             var result = JsonConvert.DeserializeObject<dynamic>(response);
  66.            
  67.             return result;
  68.         } catch (Exception e) {
  69.             //LOG
  70.             Console.WriteLine("Deu mofo {0}", e);
  71.             return null;
  72.         }
  73.    }
  74.    
  75.    public static void Response(string url)
  76.    {
  77.             // Create a request for the URL.  
  78.             WebRequest request = WebRequest.Create(url);  
  79.             // If required by the server, set the credentials.  
  80.             request.Credentials = CredentialCache.DefaultCredentials;  
  81.             // Get the response.  
  82.             WebResponse response = request.GetResponse();  
  83.             // Display the status.  
  84.             Console.WriteLine (((HttpWebResponse)response).StatusDescription);  
  85.             // Get the stream containing content returned by the server.  
  86.             Stream dataStream = response.GetResponseStream();  
  87.             // Open the stream using a StreamReader for easy access.  
  88.             StreamReader reader = new StreamReader(dataStream);  
  89.             // Read the content.  
  90.             string responseFromServer = reader.ReadToEnd();  
  91.             // Display the content.  
  92.             Console.WriteLine(responseFromServer);  
  93.             // Clean up the streams and the response.  
  94.             reader.Close();  
  95.             response.Close();
  96.    }
  97. }
  98.        
  99.  
  100.  
  101. void Main()
  102. {
  103.     /*var Estados = obterEstados(12);
  104.    
  105.     foreach(var estado in Estados)
  106.     {
  107.         Console.WriteLine("{0} - {1} - {2} - {3}",estado.Id, estado.CodigoUF, estado.Estado, estado.UF);
  108.     }*/
  109.    
  110.     var result = ApiRest.Get("http://api.uira.com.br/public/estados");
  111.     var rs = Rest.Get("http://api.uira.com.br/public/estados");
  112.  
  113.     foreach(var item in rs)
  114.     {
  115.         Console.WriteLine("{0} - {1} - {2} - {3}",item.id, item.codigo_uf, item.estado, item.uf);
  116.     }
  117. }
  118.  
  119. // Define other methods and classes here
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top