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

本文关键字:多线程 web 请求 连接 | 更新日期: 2023-09-27 17:59:39

我试图在每个线程上向web服务器发送多个xml文件(每个线程1个xml数据文件)。我似乎遇到的问题是,它只发送一个文件,并且连接已关闭,这意味着线程使用相同的连接流。在我的try/catch代码中,我需要做些什么来确保它为每个线程创建一个新的连接,使它们成为独立的连接,但我想我已经在做了,但我不知道为什么它一直关闭连接,却没有收到来自Web服务器的响应,它只响应第一个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();
            }
        }
    }
}

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

您的代码似乎还可以,只需调整一下这一点,看看会发生什么

替换:

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

我对这一点上的种族状况持怀疑态度。