i am developing a program which lets you pick multiple xml files and sends it to a webserver.
Each xml file data sent would be running on a thread so the data sending runs in parallel. The problem i have is that when i enter the first file it responds early without even entering the next couple of files.
I dont know how to make all the threads wait until i type start to send all of them at once on individual threads.I tried a implementation below but it responds too early after i type the first file.
Heres my code:
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>();
string fileName = "";
do
{
Console.WriteLine("Please enter the XML File you Wish to send");
fileName = Console.ReadLine();
Thread t = new Thread(new ParameterizedThreadStart(send));
threads.Add(t);
}
while (fileName != "start"); //Ends if user enters an empty line
foreach (Thread t in threads)
{
t.Start();
}
foreach (Thread t in threads)
{
t.Join();
}
}
static private void send(object data)
{
try
{
//ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(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();
}
}
}
}