使用 RestSharp 的多个请求(分页)
Posted
技术标签:
【中文标题】使用 RestSharp 的多个请求(分页)【英文标题】:Multiple requests (pagination) with RestSharp 【发布时间】:2021-12-14 13:36:51 【问题描述】:我是 C# 新手,我需要帮助!我需要通过客户端休息发送分页数据。由于数据量很大,我想同时触发几个请求,但是只有一小部分数据到达目的地。你能帮我解决什么问题吗?
public void SendResultsByFireForget(string env, string transactionId, int days)
var client = new RestClient();
var proxyDefinition = new WebProxy("xxx.xxx.xxx.xxx");
proxyDefinition.UseDefaultCredentials = true;
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
client.Proxy = proxyDefinition;
if (String.IsNullOrEmpty(transactionId))
throw new ArgumentException("Parameter transactionId is null or empty!");
if (String.IsNullOrEmpty(env))
throw new ArgumentException("Parameter uri is null or empty!");
List<Result> listOfResults = Repository.GetResultsByIntervalPerDay(-31);
if (listOfResults.Count() == 0 || listOfResults == null)
throw new Exception("The list of results is empty or null");
var tasks = new List<Task>();
foreach (List<Result> page in GetPages(listOfResults, 500))
var currentPageCopy = page;
var task = Task.Run(() =>
var currentPage = JSonExtensions.ToJSon(currentPageCopy);
var request = new RestRequest(
$"env.Trim()/results1/?transaction_id=transactionId.Trim()",
Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(currentPage);
client.ExecuteAsync(request);
);
tasks.Add(task);
Task.WaitAll(tasks.ToArray());
【问题讨论】:
您可以尝试在client.ExecuteAsync(request);
之前添加await
,并在此处添加async
:Task.Run(async () =>
。
【参考方案1】:
如果你可以得到第一个结果的总结果数,你可以这样做。
// RestClient is extented restsharp client you can do your own .
List<User> UserList = new();
// inline method
UserList getUser(int startCount,int limit)
var Data = RestClient.GetData(Url);
return Data;
// another inline for getting total count
int GetOrderCounts()
// get total counts
var FData = getUser(0,0);
return FData.TotalRecordCount;
var GetTotalCount = GetOrderCounts();
for (int ONumber = 0; ONumber <= GetOrderCount; ONumber += 100)
var Return = GetOrders(OrderNumber, 100);
if (Return != null)
UserList.AddRange(Return.Data);
;
【讨论】:
以上是关于使用 RestSharp 的多个请求(分页)的主要内容,如果未能解决你的问题,请参考以下文章