I have a program that sends a request and parse the response. I want to do 1000 of requests. After 10-15 requests, Httpclient starts to be slowig, and sends a request for about 10 seconds slower, and then slower and slower. Sample code:
class a5
{
public static HttpClient client = new HttpClient();
public async System.Threading.Tasks.Task main()
{
for (int page = 0; Position < 1000; page++)
{
//int totalpages = await frist1(link);
string full_json = await post_all_pages(page, link);
var jsons = JsonConvert.DeserializeObject<Json5>(full_json);
// Далее парсинг данных...
}
}
// запрос
private async System.Threading.Tasks.Task<string> post_all_pages(int page, string link)
{
var Other_Link = link.Substring(link.LastIndexOf("/") + 1, link.Length - (link.LastIndexOf("/") + 1));
var jsonrequest = "{\"PageSize\":" + 25 + ",\"PageNumber\":" + page + ",\"OrderBy\":\"PriceAsc\",\"HotelId\":null}";
var content = new StringContent(jsonrequest, Encoding.UTF8, "application/json");
var html = await client.PostAsync("https://intourist.ru/search/api/TourSearch/" + Other_Link, content);
Console.WriteLine("{\"PageSize\":" + 25 + ",\"PageNumber\":" + page + ",\"OrderBy\":\"PriceAsc\",\"HotelId\":null}");
Console.WriteLine(html);
return html.Content.ReadAsStringAsync().Result;
}
List<Task<string>> alltasks = new List<Task<string>>();
for (int page = 0; Position < 1000; page++)
{
//int totalpages = await frist1(link);
Task<String> t = post_all_pages(page, link);
alltasks.add(t);
//var jsons = JsonConvert.DeserializeObject<Json5>(full_json);
// Далее парсинг данных...
}
Tasks.WaitAll(alltasks.ToArray());
You may want to create a async function that will calls post_all_pages
, then await result and then does a JsonDeserialize
.
[EDIT 07/18/2020] Using WhenAll is a better solution:
await Task.WhenAll(alltasks.ToArray());