大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
英文原文
There’s an existing StackOverflow question and example that calls ExecuteAsync on RestSharp.NetCore.
I successfully used that example when referencing RestSharp.NetCore 105.2.3 with Newtonsoft.Json 9.0.2-beta2.
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
public async Task TestPost(ObjectFoo foo)
{
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
RestClient restClient = new RestClient(API_URL);
RestRequest request = new RestRequest(“SOME_METHOD”, Method.POST);
request.AddHeader(“Accept”, “application/json”);
string jsonObject = JsonConvert.SerializeObject(foo, Formatting.Indented, jsonSerializerSettings);
request.AddParameter(“application/json”, jsonObject, ParameterType.RequestBody);
TaskCompletionSource taskCompletion = new TaskCompletionSource();
RestRequestAsyncHandle handle = restClient.ExecuteAsync(
request, r => taskCompletion.SetResult(r));
RestResponse response = (RestResponse)(await taskCompletion.Task);
return JsonConvert.DeserializeObject(response.Content);
}
中文翻译
现有的StackOverflow问题和示例在RestSharp.NetCore上调用ExecuteAsync。
在使用Newtonsoft.Json 9.0.2-beta2引用RestSharp.NetCore 105.2.3时,我成功使用了该示例。
使用System.Threading.Tasks;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Serialization;
使用RestSharp;
public async Task< SomeObject> TestPost(ObjectFoo foo)
{
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
RestClient restClient = new RestClient(API_URL);
RestRequest request = new RestRequest(” SOME_METHOD” ,Method.POST);
request.AddHeader(” Accept” ,” application / json” );
string jsonObject = JsonConvert.SerializeObject(foo,Formatting.Indented,jsonSerializerSettings);
request.AddParameter(” application / json” ,jsonObject,ParameterType.RequestBody);
TaskCompletionSource< IRestResponse> taskCompletion = new TaskCompletionSource< IRestResponse>();
RestRequestAsyncHandle handle = restClient.ExecuteAsync(
请求,r => taskCompletion.SetResult(R));
RestResponse response =(RestResponse)(等待taskCompletion.Task);
返回JsonConvert.DeserializeObject< SomeObject>(response.Content);
}
There’s an existing StackOverflow question and example that calls ExecuteAsync on RestSharp.NetCore.
I successfully used that example when referencing RestSharp.NetCore 105.2.3 with Newtonsoft.Json 9.0.2-beta2.
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
public async Task TestPost(ObjectFoo foo)
{
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
RestClient restClient = new RestClient(API_URL);
RestRequest request = new RestRequest(“SOME_METHOD”, Method.POST);
request.AddHeader(“Accept”, “application/json”);
string jsonObject = JsonConvert.SerializeObject(foo, Formatting.Indented, jsonSerializerSettings);
request.AddParameter(“application/json”, jsonObject, ParameterType.RequestBody);
TaskCompletionSource taskCompletion = new TaskCompletionSource();
RestRequestAsyncHandle handle = restClient.ExecuteAsync(
request, r => taskCompletion.SetResult(r));
RestResponse response = (RestResponse)(await taskCompletion.Task);
return JsonConvert.DeserializeObject(response.Content);
}
现有的StackOverflow问题和示例在RestSharp.NetCore上调用ExecuteAsync。
在使用Newtonsoft.Json 9.0.2-beta2引用RestSharp.NetCore 105.2.3时,我成功使用了该示例。
使用System.Threading.Tasks;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Serialization;
使用RestSharp;
public async Task< SomeObject> TestPost(ObjectFoo foo)
{
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
RestClient restClient = new RestClient(API_URL);
RestRequest request = new RestRequest(” SOME_METHOD” ,Method.POST);
request.AddHeader(” Accept” ,” application / json” );
string jsonObject = JsonConvert.SerializeObject(foo,Formatting.Indented,jsonSerializerSettings);
request.AddParameter(” application / json” ,jsonObject,ParameterType.RequestBody);
TaskCompletionSource< IRestResponse> taskCompletion = new TaskCompletionSource< IRestResponse>();
RestRequestAsyncHandle handle = restClient.ExecuteAsync(
请求,r => taskCompletion.SetResult(R));
RestResponse response =(RestResponse)(等待taskCompletion.Task);
返回JsonConvert.DeserializeObject< SomeObject>(response.Content);
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/196022.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...