C# WP8-多个并发WebClient调用

C# WP8-多个并发WebClient调用,c#,json,windows-phone-8,webclient,C#,Json,Windows Phone 8,Webclient,我正在尝试从Windows Phone 8应用程序的在线RESTful源下载数据。总共有大约700个调用,每个调用下载一个长度可变的json字符串。 我不断遇到TargetInvocationExceptions,告诉我在大约65-80次调用之后发生了http错误404,每个后续调用都会引发此异常,但只有在我调用循环中的DownloadStringAsync methon时才会发生。下载json数据本身工作完美,因此在线源代码可用,内容也不会太大而无法处理。 我想我在执行时间上遇到了问题,但到目

我正在尝试从Windows Phone 8应用程序的在线RESTful源下载数据。总共有大约700个调用,每个调用下载一个长度可变的json字符串。 我不断遇到TargetInvocationExceptions,告诉我在大约65-80次调用之后发生了http错误404,每个后续调用都会引发此异常,但只有在我调用循环中的DownloadStringAsync methon时才会发生。下载json数据本身工作完美,因此在线源代码可用,内容也不会太大而无法处理。 我想我在执行时间上遇到了问题,但到目前为止我还没有找到解决办法,这仍然只是一个猜测。欢迎任何建议

这是我的代码MainPage.xaml.cs:

namespace MyApp
{

    static MyViewModel ViewModel;

    public MainPage()
    {
        InitializeComponent();
        ViewModel = new MyViewModel();
        this.DataContext = ViewModel;
        GetData();
    }

    void GetData()
    {
        int id = 1;

        while (id <= 717)
        {
            GetJson(id++);
        }
    }

    void GetJson(int id)
    {
        string url = Connector.BaseURL + id + "/";
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(AddModelToViewModel);
        client.DownloadStringAsync(new Uri(url));
    }

    void AddModelToViewModel(object sender, DownloadStringCompletedEventArgs e)
    {
        try
        {
            string json = e.Result;

            MyModel Model = JsonConvert.DeserializeObject<MyModel>(json);
            ViewModel.Models.Add(Model);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());                
        }
    }
}
我的ViewModel只包含一个

ObservableCollection<MyModel> Models
绑定到主页视图中的列表视图

我还不熟悉Windows Phone编程,请原谅我的风格不好:

编辑:这肯定是一次有太多的请求。从按下按钮开始,将GetData方法一次拆分为50个请求的块,不会出现问题。正如下面的评论所述,我认为服务器不会阻止我的请求。Windowsphone侧面是否有限制

工作的控制台代码:

    static int count = 0;
    static void Main(string[] args)
    {
        for (int i = 1; i <= 717; i++)
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DL);
            client.DownloadStringAsync(new Uri(BaseURL + i + "/"));
        }

        Console.ReadKey();
    }

    static void DL(object sender, DownloadStringCompletedEventArgs e)
    {
        Console.WriteLine("#" + ++count + ": " + (e.Error == null ? "No error." : "Error!"));
    }

你确定问题来自你的应用程序吗?服务器可能有某种防水蛭保护,检测到您在短时间内发送了异常多的请求,并阻止下一个请求requests@KooKiz我只是尝试在控制台应用程序中实现相同的代码。在这种情况下,它可以工作,因此服务器应该可以处理许多请求。