각 스레드 (스레드 당 1ml 데이터 파일)에서 여러 xml 파일을 웹 서버로 보내려고합니다. 내가 겪고있는 것 같습니다 그것은 단지 하나의 파일을 보내고 스레드가 동일한 연결 스트림을 사용하고 있다는 의미 연결이 닫힙니다. 내가 뭘 try/catch 코드를 각각의 스레드에 대한 새 연결을 만들 수 있도록 할 필요가 그래서 그들은 독립적 인 연결 있지만 나는 이미 그것을하고 있지만 내가 왜 연결을 닫고 응답을받지 않는지 모르겠다 생각 Logged 웹 서버에서 첫 xml 스레드에만 응답하고 때로는 모두에 응답합니다.동일한 연결을 사용하는 다중 스레드 웹 요청
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);
}
내가 경쟁 조건에 대한 의심 해요
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();
}
}
}
}
그런 다음 "WebRequest.Create"를 호출하여 새 연결을 만듭니다 ... –
[여러 개의 WebRequest를 Parallel.For에서 보내기] (http://stackoverflow.com/questions/7477261/send-multiple-webrequest- in-parallel-for) – Sinatr
거기에 그것입니까? try catch 부분에서 각 스레드에서 실행됩니까? 어쨌든 새로운 것을 만들지 않겠습니까? 하지만 그것은 – Freon