GitHub Repos

Open Visual Studio 2022
New Project, C#, Windows, Console, Console App
Change the Name to "Console_Services_REST"
Change the Location to somewhere on your c drive and press Next
Check the .NET version is correct and press Create.
The solution contains one project and that project contains a single source file called program.cs
SS


REST Service JSON

We are going to use the following REST service:

https://api.github.com/users/officeaddins/repos 

Copy this into a browser and check that it works and returns JSON.







namespace consoleapp1 
{
  class Program
  {
    private static readonly
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

    private static async
    System.Threading.Tasks.Task
    Main(string[] args)
    {
      var repositories = await Method_ProcessRepositories();

      foreach (Class_Repository repo in repositories)
      {
        System.Console.WriteLine(repo.Property_Name);
        System.Console.WriteLine(repo.Property_Description);
        System.Console.WriteLine(repo.Property_HTMLUrl);
        System.Console.WriteLine();
      }
    }

    private static async
    System.Threading.Tasks.Task<System.Collections.Generic.List<Class_Repository>>
    Method_ProcessRepositories()
    {
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(
          new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
      client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

      var streamTask = client.GetStreamAsync("https://api.github.com/users/officeaddins/repos");

      var repositories =
      await System.Text.Json.JsonSerializer.DeserializeAsync<System.Collections.Generic.List<Class_Repository>>(await streamTask);

      if (repositories is not null)
      {
        return repositories;
      }
      else
      {
        return new System.Collections.Generic.List<Class_Repository>();
      }
    }
  }

  public class 
  Class_Repository
  {
    [System.Text.Json.Serialization.JsonPropertyName("name")]
    public string? Property_Name { get; set; }

    [System.Text.Json.Serialization.JsonPropertyName("description")]
    public string? Property_Description { get; set; }

    [System.Text.Json.Serialization.JsonPropertyName("html_url")]
    public System.Uri? Property_HTMLUrl { get; set; }

  }
}


public class Class1 
    {
        private const string URL = "https://sub.domain.com/objects.json?api_key=123";
        private const string DATA = @"{""object"":{""name"":""Name""}}";

        static void Main(string[] args)
        {
            Class1.CreateObject();
        }

        private static void CreateObject()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = DATA.Length;
            StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
            requestWriter.Write(DATA);
            requestWriter.Close();

             try {
                WebResponse webResponse = request.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string response = responseReader.ReadToEnd();
                Console.Out.WriteLine(response);
                responseReader.Close();
            } catch (Exception e) {
                Console.Out.WriteLine("-----------------");
                Console.Out.WriteLine(e.Message);
            }

        }
    }



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext