Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ResultClientEstado
- {
- public int id;
- public int codigo_uf;
- public string estado;
- public string uf;
- }
- public class Estados
- {
- public int Id;
- public int CodigoUF;
- public string Estado;
- public string UF;
- public Estados(int id, int codigoUf, string estado, string uf)
- {
- Id = id;
- CodigoUF = codigoUf;
- Estado = estado;
- UF = uf;
- }
- }
- public IEnumerable<Estados> obterEstados(int id)
- {
- using (var client = new HttpClient())
- {
- var url = string.Format("http://api.uira.com.br/public/estados");
- var result = JsonConvert.DeserializeObject<List<ResultClientEstado>>(client.GetStringAsync(url).Result);
- //Console.WriteLine(result);
- return result.AsParallel().Select(x => new Estados(x.id, x.codigo_uf, x.estado, x.uf));
- }
- }
- public static class Rest
- {
- public static dynamic Get(string url)
- {
- using (var client = new HttpClient())
- {
- return JsonConvert.DeserializeObject<dynamic>(client.GetStringAsync(url).Result);
- }
- }
- }
- public static class ApiRest{
- public static dynamic Get(string url)
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.Method = "GET";
- request.ContentType = "application/json";
- try {
- WebResponse webResponse = request.GetResponse();
- Stream webStream = webResponse.GetResponseStream();
- StreamReader responseReader = new StreamReader(webStream);
- string response = responseReader.ReadToEnd();
- responseReader.Close();
- var result = JsonConvert.DeserializeObject<dynamic>(response);
- return result;
- } catch (Exception e) {
- //LOG
- Console.WriteLine("Deu mofo {0}", e);
- return null;
- }
- }
- public static void Response(string url)
- {
- // Create a request for the URL.
- WebRequest request = WebRequest.Create(url);
- // If required by the server, set the credentials.
- request.Credentials = CredentialCache.DefaultCredentials;
- // Get the response.
- WebResponse response = request.GetResponse();
- // Display the status.
- Console.WriteLine (((HttpWebResponse)response).StatusDescription);
- // Get the stream containing content returned by the server.
- Stream dataStream = response.GetResponseStream();
- // Open the stream using a StreamReader for easy access.
- StreamReader reader = new StreamReader(dataStream);
- // Read the content.
- string responseFromServer = reader.ReadToEnd();
- // Display the content.
- Console.WriteLine(responseFromServer);
- // Clean up the streams and the response.
- reader.Close();
- response.Close();
- }
- }
- void Main()
- {
- /*var Estados = obterEstados(12);
- foreach(var estado in Estados)
- {
- Console.WriteLine("{0} - {1} - {2} - {3}",estado.Id, estado.CodigoUF, estado.Estado, estado.UF);
- }*/
- var result = ApiRest.Get("http://api.uira.com.br/public/estados");
- var rs = Rest.Get("http://api.uira.com.br/public/estados");
- foreach(var item in rs)
- {
- Console.WriteLine("{0} - {1} - {2} - {3}",item.id, item.codigo_uf, item.estado, item.uf);
- }
- }
- // Define other methods and classes here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement