What does this PHP Curl code look like in c#?



  • $curl = curl_init("https://api.vimeworld.ru/online");
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 4);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
      'Access-Token: MY_TOKEN'
    ));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($curl);
    curl_close($curl);
    
    print $response;
    


  • Take Visual Studio 2019 (or 2022), create a console project.NET 5 (or 6), take json text from the server ' s response.

    {"total":2375,"separated":{"ann":26,"bb":66,"bp":24,"bw":170,"cp":0,"dr":15,"duels":48,"gg":24,"hg":3,"kpvp":21,"lobby":670,"mw":55,"prison":549,"sw":91,"murder":60,"bridge":20,"jumpleague":0,"paintball":0,"turfwars":0,"sheep":0,"tntrun":16,"tnttag":0,"luckywars":279,"zombieclaus":0,"hide":0,"speedbuilders":55,"teamfortress":0,"fallguys":0,"eggwars":183}}
    

    Copy, we're taking the studio. EditPaste SpecialPaste JSON as classesand the code is in the form of a ready data model, renamed for a convenience of one of the classes, we'll get it.

    public class OnlineResponse
    {
        public int total { get; set; }
        public Separated separated { get; set; }
    }
    

    public class Separated
    {
    public int ann { get; set; }
    public int bb { get; set; }
    public int bp { get; set; }
    public int bw { get; set; }
    public int cp { get; set; }
    public int dr { get; set; }
    public int duels { get; set; }
    public int gg { get; set; }
    public int hg { get; set; }
    public int kpvp { get; set; }
    public int lobby { get; set; }
    public int mw { get; set; }
    public int prison { get; set; }
    public int sw { get; set; }
    public int murder { get; set; }
    public int bridge { get; set; }
    public int jumpleague { get; set; }
    public int paintball { get; set; }
    public int turfwars { get; set; }
    public int sheep { get; set; }
    public int tntrun { get; set; }
    public int tnttag { get; set; }
    public int luckywars { get; set; }
    public int zombieclaus { get; set; }
    public int hide { get; set; }
    public int speedbuilders { get; set; }
    public int teamfortress { get; set; }
    public int fallguys { get; set; }
    public int eggwars { get; set; }
    }

    Then the code will look like this.

    private static readonly HttpClient client = new HttpClient() { DefaultRequestVersion = HttpVersion.Version20 };

    static async Task Main(string[] args)
    {
    using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://api.vimeworld.ru/online");
    //сервер отвечает без токена нормально, но если надо добавить, то вот так можно
    //request.Headers.Add("Access-Token", "MY_TOKEN");
    using HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
    OnlineResponse result = await response.Content.ReadFromJsonAsync<OnlineResponse>();

    Console.WriteLine(result.total);
    Console.WriteLine(result.separated.lobby);
    
    Console.ReadKey();
    

    }

    Conclusion

    2407
    647



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2