使用相同连接的多线程 Web 请求

Multi Threaded web requests using same connection

我试图在每个线程上发送多个 xml 文件(每个线程 1 xml 个数据文件)到 Web 服务器。我似乎遇到的问题是它只发送一个文件并且连接已关闭,这意味着线程正在使用相同的连接流。我需要在我的 try/catch 代码中做什么,以确保它为每个线程创建一个新连接,以便它们是独立的连接,但我认为我已经这样做了,但我不知道为什么它一直关闭连接而不接收来自网络服务器的响应,它只响应第一个 xml 线程,有时它会响应所有线程。

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();
            }
        }
    }
}

您的代码似乎没问题,只需调整一下看看会发生什么

替换:

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

与:

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

我怀疑那个位的竞争条件。