Skip to content

Instantly share code, notes, and snippets.

@mvgolom
Last active September 27, 2018 22:29
Show Gist options
  • Select an option

  • Save mvgolom/8cd4d4de3e5ba02e51bf28a0ba0dfbfb to your computer and use it in GitHub Desktop.

Select an option

Save mvgolom/8cd4d4de3e5ba02e51bf28a0ba0dfbfb to your computer and use it in GitHub Desktop.
//do tutorial
dotnet new console
dotnet restore
dotnet run
//para adicionar um pacote de manipulacao de json
dotnet add package Newtonsoft.Json
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Sample{
//Modelo do Json
public class Word{
public string Name { get; set; }
public List<string> Anagrams { get; set; }
}
class Test
{
public static void Main(string[] args){
WebRequest request = WebRequest.Create("http://localhost:3000/newgame");
WebResponse response = request.GetResponse();
//Console.WriteLine (((HttpWebResponse)response).StatusDescription);
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
Console.WriteLine("Response:\n");
//Converte a String do request em json
Word w = Newtonsoft.Json.JsonConvert.DeserializeObject<Word>(responseFromServer);
Console.WriteLine(w.Name+":");
foreach (string elemento in w.Anagrams){
Console.WriteLine(elemento);
}
reader.Close ();
dataStream.Close ();
response.Close ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment