Example of RestSharp ASYNC client.ExecuteAsync () works

后端 未结 3 2088
孤城傲影
孤城傲影 2021-02-07 05:56

Could someone please help me modify the code below:

client.ExecuteAsync(request, response => {
    Console.WriteLine(response.Content);
});

3条回答
  •  攒了一身酷
    2021-02-07 06:56

    With the help of @spender, this is what i got:

    You can add new file in RestSharp project, and add this code:

    public partial class RestClient
    {
        public Task> ExecuteAsync(IRestRequest request)
        {
            var tcs=new TaskCompletionSource>();
    
            this.ExecuteAsync(request, response => 
                {
                    tcs.SetResult(
                        Deserialize(request, response)
                    );
                });
    
        return tcs.Task;
        }       
    }
    

    This will practically return the full response, with status code and everything, so you can check if the status of the response is OK before getting the content, and you can get the content with:

    response.Content
    

提交回复
热议问题