У меня возникли некоторые трудности с многопоточными веб-запросами. Я хочу выполнить 100 запросов GET для URL-адреса за один раз, используя следующий код:
static void Main(string[] args)
{
for (int i = 0; i <= 100; i++)
{
Console.WriteLine("RUN TASK: " + i);
Task.Run(() =>
{
makeRequest();
});
}
Console.Read();
}
public static void makeRequest()
{
string html = string.Empty;
string url = @"https://192.168.205.50/api/v1/status";
Console.WriteLine("GET ON:" + url);
ServicePointManager.UseNagleAlgorithm = true;
ServicePointManager.Expect100Continue = true;
ServicePointManager.CheckCertificateRevocationList = true;
ServicePointManager.DefaultConnectionLimit = 1000;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ServicePoint.ConnectionLimit = 1000;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
Console.WriteLine("Got response");
html = reader.ReadToEnd();
}
}
URL-адрес спит в течение 30 секунд.результат: код открывает URL только каждую 1 секунду вместо 100 сразу.