Multiple HTTP request in C#

后端 未结 5 1001
北海茫月
北海茫月 2021-02-15 01:56

I need to send about 200 HTTP requests in parallel to different servers and get response. I use HttpWebRequest class in C#. But I don\'t see good time enhancement when requests

5条回答
  •  名媛妹妹
    2021-02-15 02:07


    you can try this :

    try
            {
                List uris = new List();
                uris.Add(new Uri("http://www.google.fr"));
                uris.Add(new Uri("http://www.bing.com"));
    
                Parallel.ForEach(uris, u =>
               {
                   WebRequest webR = HttpWebRequest.Create(u);
                   HttpWebResponse webResponse = webR.GetResponse() as HttpWebResponse;
               });
            }
            catch (AggregateException exc)
            {
                exc.InnerExceptions.ToList().ForEach(e =>
                    {
                        Console.WriteLine(e.Message);
                    });
            }
    

提交回复
热议问题