繁体   English   中英

对Web服务的HTTP发布请求。 错误:索引超出了数组的范围

[英]HTTP Post Request to web service . ERROR : Index was outside the bounds of the array

我有以下代码,用于将多个HTTP Post请求从XML文件发布到Web服务。 这里的问题是,如果我将thread.Join()放在有注释的地方,则能够成功地放置所有请求。 但是如果我将第二个内部for循环用于thread.Join() ,我会得到

索引超出数组错误的范围

thread[x] = new Thread(() => function(files[x],p));

在files [x]中。 即main for循环。 我无法找到我要去的地方。 请纠正 。 我正在使用.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();
        }
    }
}

}

这是因为表达式() => function(files[x],p) 第一个内部循环完成之后求值,并且x在该循环中递增。 因此,您总是得到x = len的超出范围的值。

为了解决这个问题,您需要在匿名函数声明之前声明另一个局部变量,并为其分配x的值,如下所示:

var localx=x;
thread[x] = new Thread(() => function(files[localx],p));

这是更深入地说明这种情况发生的原因的链接: 有人可以用简单的术语解释C#中的“访问修改后的闭包”吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM