[英]HTTP Post Request to web service . ERROR : Index was outside the bounds of the array
I have the following code for posting multiple HTTP Post requests to a web service from XML files. 我有以下代码,用于将多个HTTP Post请求从XML文件发布到Web服务。 The problem here is if I put
thread.Join()
in the place where it is commented , I am able to put all the requests successfully . 这里的问题是,如果我将
thread.Join()
放在有注释的地方,则能够成功地放置所有请求。 But if i use the second inner for loop for thread.Join()
, i get 但是如果我将第二个内部for循环用于
thread.Join()
,我会得到
Index was outside the bounds of the array error
索引超出数组错误的范围
in 在
thread[x] = new Thread(() => function(files[x],p));
in files[x] . 在files [x]中。 ie the main for loop .
即main for循环。 I am unable to get where i am going wrong .
我无法找到我要去的地方。 Please correct .
请纠正 。 I am using .NET4.0 CODE :
我正在使用.NET4.0代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Xml;
using System.Net;
namespace ConsoleApplication4
{
class Program
{
int flag = 1;
string destination;
static void Main(string[] args)
{
int n = 0;
Program p = new Program();
Console.WriteLine("Enter the number");
string s = Console.ReadLine();
int.TryParse(s, out n);
Console.WriteLine("Enter Destination");
p.destination = Console.ReadLine();
string path = "D:\\temp";
string[] files = null;
files = Directory.GetFiles(path, "*.xml", SearchOption.TopDirectoryOnly);
Thread[] thread = new Thread[files.Length];
int x;
int len = files.Length;
for (int i = 0; i<len; i+=n)
{
x = i;
for (int j = 0; j < n && x < len; j++)
{
thread[x] = new Thread(() => function(files[x],p));
thread[x].Start();
//thread[x].Join();
x++;
}
int y = x - n;
for (; y < x; y++)
{
int t = y;
thread[t].Join();
}
}
// thread[0] = new Thread(() => function(files[0]));
//thread[0].Start();
Console.ReadKey();
}
public static void function(string temp,Program p)
{
XmlDocument doc = new XmlDocument();
doc.Load(temp);
string final_d=p.destination + "response " + p.flag + ".xml";
p.flag++;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.76.22.135/wpaADws/ADService.asmx");
request.ContentType = "text/xml;charset=\"utf-8\"";
request.Accept = "text/xml";
request.Method = "POST";
Stream stream = request.GetRequestStream();
doc.Save(stream);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
doc.LoadXml(soapResult);
File.WriteAllText(final_d, doc.DocumentElement.InnerText);
//XmlTextWriter xml=new XmlTextWriter(
Console.WriteLine(soapResult);
//Console.ReadKey();
}
}
}
} }
It happens because the expression () => function(files[x],p)
is evaluated after the first inner loop is complete, and x is incremented in this loop. 这是因为表达式
() => function(files[x],p)
在第一个内部循环完成之后求值,并且x在该循环中递增。 So you always get the out-of-range value of x=len. 因此,您总是得到x = len的超出范围的值。
To solve this, you need to declare another local variable and assign value of x to it, right before the anonymous function declaration, like this: 为了解决这个问题,您需要在匿名函数声明之前声明另一个局部变量,并为其分配x的值,如下所示:
var localx=x;
thread[x] = new Thread(() => function(files[localx],p));
Here is a link to a more in-depth explanatio of why this happens: Can someone explain "access to modified closure" in C# in simple terms? 这是更深入地说明这种情况发生的原因的链接: 有人可以用简单的术语解释C#中的“访问修改后的闭包”吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.