Search code examples
c#riot-games-api

First Step with Riot API in C#


I am learning C# and want to use the Riot API. I just want to receive that:

    {  
   "type":"champion",
   "version":"6.1.1",
   "data":{  
      "Thresh":{  
         "id":412,
         "key":"Thresh",
         "name":"Thresh",
         "title":"the Chain Warden"
      },
      "Aatrox":{  
         "id":266,
         "key":"Aatrox",
         "name":"Aatrox",
         "title":"the Darkin Blade"
      },...

I found this here: Deserialize JSON from Riot API C#


Solution

  • If you want to get the json string try this, this take a URL and tries to do the request and returns the response. You can find the url in the sandbox mode provided on the riot API site.

    using System.Net;
    using System.IO;
    public string GET(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    
            try
            {
                WebResponse response = request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                    return reader.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                WebResponse errorResponse = ex.Response;
                using (Stream responseStream = errorResponse.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                    String errorText = reader.ReadToEnd();
                }
                throw;
            }
        }
    

    This is the easy part :) mapping the response to a POCO is what annoys me the most. If anybody reads this and has a good solution plzz link me.