How to use RestSharp with async/await

前端 未结 2 1885
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 23:43

I\'m struggling to find a modern example of some asynchronous C# code that uses RestSharp with async and await. I know there\'s been a recent updat

2条回答
  •  春和景丽
    2020-12-08 00:29

    In my case, I had to call Task.Wait() for it to work properly. However, I used the version which does not take CancellationTokenSource as parameter.

    private static async void Main()
    {
        var client = new RestClient();
        var request = new RestRequest("http://www.google.com");
        Task t = client.ExecuteTaskAsync(request);
        t.Wait();
        var restResponse = await t;
        Console.WriteLine(restResponse.Content); // Will output the HTML contents of the requested page
    }
    

提交回复
热议问题