Search code examples
c#multithreadinghttpwebrequesthttpwebresponse

Multi Threaded web requests using same connection


I am trying to send multiple xml files on each thread(1 xml datafile per thread) to a web server. The problem i am seem to be getting is that it only sends one files and the connection is closed, which means that the threads are using the same connection stream. What do i need to do in my try/catch code to make sure it creates a new connection for each thread so they are independant connection but i think im doing that already but i dont know why it keeps closing the connection and not receiving the response from the webserver, it only responds to first xml thread and sometimes it responds to them all .

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace XMLSender
{
    class Program
    {
       private static string serverUrl;
       static void Main(string[] args)
        {
            Console.WriteLine("Please enter the URL to send the XML File");
            serverUrl = Console.ReadLine();


            List<Thread> threads = new List<Thread>();
            List<string> names = new List<string>();
            string fileName = "";

            do
            {
                Console.WriteLine("Please enter the XML File you Wish to send");
                fileName = Console.ReadLine();
                if (fileName != "start")
                {
                    Thread t = new Thread(new ParameterizedThreadStart(send));
                    threads.Add(t);
                    names.Add(fileName);
                }
            }
            while (fileName != "start");

            foreach (Thread t in threads)
            {
                t.Start(names[0]);
                names.RemoveAt(0);
            }
            foreach (Thread t in threads)
            {
                t.Join();
            }

        }
        static private void send(object data)
        {
            try
            {
                //Set the connection limit of HTTP
                System.Net.ServicePointManager.DefaultConnectionLimit = 10000;

                //ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };               
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(serverUrl));

                byte[] bytes;

                //Load XML data from document 
                XmlDocument doc = new XmlDocument();
                doc.Load((string)data);
                string xmlcontents = doc.InnerXml;

                //Send XML data to Webserver
                bytes = Encoding.ASCII.GetBytes(xmlcontents);
                request.ContentType = "text/xml; encoding='utf-8'";
                request.ContentLength = bytes.Length;
                request.Method = "POST";
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();

                // Get response from Webserver
                HttpWebResponse response;
                response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                string responseStr = new StreamReader(responseStream).ReadToEnd();
                Console.Write(responseStr + Environment.NewLine);

            }
            catch (Exception e)
            {
                Console.WriteLine("An Error Occured" + Environment.NewLine + e);
                Console.ReadLine();
            }
        }
    }
}

Solution

  • Your code seems OK, just tweak this bit see what happens

    Replace:

    foreach (Thread t in threads)
    {
        t.Start(names[0]);
        names.RemoveAt(0);
    }
    

    With:

    for(int i = 0; i < threads.Count; i++))
    {
        var t= threads[i];
        var name=names[i];
        t.Start(name);
    }
    

    I'm suspicious about the race condition in that bit.