Как будет выглядить этот PHP Curl код в 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;

Ответы (1 шт):

Автор решения: aepot

Берем Visual Studio 2019 (или 2022), создаем консольный проект .NET 5 (или 6), берем json текст из ответа сервера

{"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}}

Копируем, выбираем в меню студии Edit - Paste Special - Paste JSON as classes, и в код вставится готовая модель данных, переименовав для удобства один из классов, получим.

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; }
}

Тогда код будет выглядеть вот так

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();
}

Вывод в консоль

2407
647
→ Ссылка