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

时间:2016-08-02 13:20:12

标签: c# multithreading httpwebrequest httpwebresponse

我正在尝试将每个线程上的多个xml文件(每个线程1 xml数据文件)发送到Web服务器。我似乎得到的问题是它只发送一个文件并且连接已关闭,这意味着线程使用相同的连接流。我需要做什么在我的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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

替换:

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

我对这一点的竞争状况持怀疑态度。