Wednesday, January 7, 2015

MultiThread HttpWebRequest in C#

this post is what I found for my experience in this 3 years... :D

based solving problem and understanding the problem
such as:
http://tulisanlain.blogspot.com/2015/01/webrequestcreate-is-slow-this-one-for.html
http://tulisanlain.blogspot.com/2014/08/the-remote-server-returned-error-417.html
http://tulisanlain.blogspot.com/2014/07/breakthrough-limit-maximum-number.html
and many more... 

here the code... ( hope you get better understand without pain process ) :D . This scenario can be used with many occasion
  

 System.Net.ServicePointManager.DefaultConnectionLimit = 1000; //or some other number > 4
 System.Net.ServicePointManager.Expect100Continue = false;
 try
        {
            List uris = new List();
            uris.Add(new Uri("http://www.google.fr"));
            uris.Add(new Uri("http://www.bing.com"));
            uris.Add(new Uri("http://www.yahoo.com"));

            Parallel.ForEach(uris, u =>
           {
               WebRequest webR = HttpWebRequest.Create(u);
      webR.Proxy = null;
               using (var response = (HttpWebResponse)webR.GetResponse())
    {
     response
    }
           });
        }
        catch (AggregateException exc)
        {
            exc.InnerExceptions.ToList().ForEach(e =>
                {
                    Console.WriteLine(e.Message);
                });
        }