我正在使用 RestSharp.NetCore 包,并且需要调用 ExecuteAsyncPost 方法。我正在努力理解回调参数。
var client = new RestClient("url");
request.AddParameter("application/json", "{myobject}", ParameterType.RequestBody);
client.ExecuteAsyncPost(request,**callback**, "POST");
回调的类型是Action<IRestResponse,RestRequestAsyncHandler>
有人请发布一个小代码示例,展示如何使用回调参数并进行解释。
谢谢
-C
这对我使用 ExecuteAsync 进行 Get 调用很有用。希望它能为您指明正确的方向。请注意,代码和信用归于https://www.learnhowtoprogram.com/net/apis-67c53b46-d070-4d2a-a264-cf23ee1d76d0/apis-with-mvc 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)