RestSharp.NetCore中的ExecuteAsyncPost示例 [英] ExecuteAsyncPost Example in RestSharp.NetCore

查看:812
本文介绍了RestSharp.NetCore中的ExecuteAsyncPost示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RestSharp.NetCore包,需要调用ExecuteAsyncPost方法.我正在努力理解回调参数.

I'm working with RestSharp.NetCore package and have a need to call the ExecuteAsyncPost method. I'm struggling with the understanding the callback parameter.

    var client = new RestClient("url");
    request.AddParameter("application/json", "{myobject}",  ParameterType.RequestBody);
    client.ExecuteAsyncPost(request,**callback**, "POST");

回调类型为Action<IRestResponse,RestRequestAsyncHandler>

有人请张贴一个小代码示例,其中显示了如何使用回调参数并进行解释.

Would someone please post a small code example showing how to use the callback parameter with an explanation.

谢谢 -C

推荐答案

这对我有效,使用ExecuteAsync进行Get调用.希望它可以为您指明正确的方向.请注意,代码和信誉归于 https ://www.learnhowtoprogram.com/net/apis-67c53b46-d070-4d2a-a264-cf23ee1d76d0/apis-with-mvc

This worked for me using ExecuteAsync for a Get call. It should hopefully point you in the right direction. Note that the code and credit goes to https://www.learnhowtoprogram.com/net/apis-67c53b46-d070-4d2a-a264-cf23ee1d76d0/apis-with-mvc

public void ApiTest()
    {
        var client = new RestClient("url");
        var request = new RestRequest(Method.GET);
        var response = new RestResponse();
        Task.Run(async () =>
        {
            response = await GetResponseContentAsync(client, request) as RestResponse;
        }).Wait();
        var jsonResponse = JsonConvert.DeserializeObject<JObject>(response.Content);

    }

public static Task<IRestResponse> GetResponseContentAsync(RestClient theClient, RestRequest theRequest)
    {
        var tcs = new TaskCompletionSource<IRestResponse>();
        theClient.ExecuteAsync(theRequest, response => {
            tcs.SetResult(response);
        });
        return tcs.Task;
    }

这篇关于RestSharp.NetCore中的ExecuteAsyncPost示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆