繁体   English   中英

如何从URL从Windows应用程序读取大量xml文件(从Windows应用程序向服务器发送多个请求)C#

[英]How to read numerous xml files from windows appication from URL (multiple requests to server from windows app) c#

我有一个Windows窗体,用户可以在其中下载选定日期段的所有货币汇率。 我现在有这个:

    for (DateTime d = fromDatePicker.Value.Date; d <= toDatePicker.Value.Date; d.AddDays(1))
    {
        if (d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday)
                                return;

        string url = "http://cbar.az/currencies/" + d.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture) + ".xml";
        XmlDocument doc = new XmlDocument();
        doc.Load(url);
        //other stuff
    }

URL格式取决于日期,如下所示: http : //cbar.az/currencies/15.07.2015.xml例如,如果我选择两周时间段,它将获得两天的费率,然后跳过两天,依此类推。一个错误,甚至没有达到期末:

远程服务器返回错误(503)服务器不可用

我猜想这是针对多个客户端请求的服务器端保护,但不知道如何解决此问题。

如果选择2或3天,则不会引发错误。 但是在这里它也可能无法获得所有日期的费率。

多谢您的协助。 谢谢。

这是我的整个代码:

for (DateTime d = fromDatePicker.Value.Date; d <= toDatePicker.Value.Date; d.AddDays(1))
                {
                    if (d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday)
                        continue;

                    string url = "http://cbar.az/currencies/" + d.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture) + ".xml";

                    #region read rates for the date to the DataTable
                    XmlDocument doc = new XmlDocument();
                    doc.Load(url);

                    XmlElement root = doc.DocumentElement;
                    XmlNodeList nodes = root.SelectNodes("//ValCurs/ValType");

                    DataTable tempRates = new DataTable();

                    foreach (XmlNode node in nodes)
                    {
                        if (node.Attributes["Type"].Value == "Xarici valyutalar")
                        {
                            //create temp table and load new rates
                            tempRates.Clear();
                            tempRates.Columns.Add("Code");
                            tempRates.Columns.Add("Nominal");
                            tempRates.Columns.Add("Name");
                            tempRates.Columns.Add("Value");

                            foreach (XmlNode currency in node.ChildNodes)
                            {
                                DataRow dr = tempRates.NewRow();
                                dr["Code"] = currency.Attributes["Code"].Value;

                                foreach (XmlNode currencyDetailsNode in currency.ChildNodes)
                                {
                                    dr[currencyDetailsNode.Name] = currencyDetailsNode.InnerText;
                                }

                                tempRates.Rows.Add(dr);
                            }
                        }
                    }
                    #endregion

                    DAL dal = new DAL();
                    dal.ClearCurrentRates(d);

                    //insert new values
                    foreach (DataRow currencyRow in StaticValues.dataSet.Tables["Currencies"].Rows)
                    {
                        if (currencyRow["Code"].ToString() == "AZN")
                        {
                            #region Insert the row for AZN
                            try
                            {
                                SqlParameter[] pars = new SqlParameter[3];

                                pars[0] = new SqlParameter("@Date", SqlDbType.Date);
                                pars[0].Value = d.ToShortDateString();

                                pars[1] = new SqlParameter("@CurrencyID", SqlDbType.Int);
                                pars[1].Value = currencyRow["ID"].ToString();

                                pars[2] = new SqlParameter("@Rate", SqlDbType.Decimal);
                                pars[2].Value = 1.0000;

                                dal.InsertData("CurrencyRates", pars);
                            }
                            catch (Exception ex)
                            {
                                StaticValues.WriteEventLogXML(ex, this.Text);
                                switch (StaticValues.user.Language)
                                {
                                    case "English":
                                        MessageBox.Show("Database error", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        break;
                                    case "Russian":
                                        MessageBox.Show("Ошибка базы данных", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        break;
                                    case "Azeri":
                                        MessageBox.Show("Məlumat bazası səhvi", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        break;
                                    default:
                                        break;
                                }
                            }
                            #endregion
                            continue;
                        }
                        foreach (DataRow tempRow in tempRates.Rows)
                        {
                            if (tempRow["Code"].ToString() == currencyRow["Code"].ToString())
                            {
                                #region Insert the row
                                try
                                {
                                    SqlParameter[] pars = new SqlParameter[3];

                                    pars[0] = new SqlParameter("@Date", SqlDbType.Date);
                                    pars[0].Value = d.ToShortDateString();

                                    pars[1] = new SqlParameter("@CurrencyID", SqlDbType.Int);
                                    pars[1].Value = currencyRow["ID"].ToString();

                                    pars[2] = new SqlParameter("@Rate", SqlDbType.Decimal);
                                    pars[2].Value = decimal.Parse(tempRow["Value"].ToString());

                                    dal.InsertData("CurrencyRates", pars);
                                    break;
                                }
                                catch (Exception ex)
                                {
                                    StaticValues.WriteEventLogXML(ex, this.Text);
                                    switch (StaticValues.user.Language)
                                    {
                                        case "English":
                                            MessageBox.Show("Database error", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            break;
                                        case "Russian":
                                            MessageBox.Show("Ошибка базы данных", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            break;
                                        case "Azeri":
                                            MessageBox.Show("Məlumat bazası səhvi", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                            break;
                                        default:
                                            break;
                                    }
                                    break;
                                }
                                #endregion
                            }
                        }
                    }
                    d = d.AddDays(1);
                    Thread.Sleep(1000);
                }

你的东西是完全错误的。

  1. 方法AddDays(x)不会更新您的“ d”变量,因此构造

     for (DateTime d = fromDatePicker.Value.Date; d <= toDatePicker.Value.Date; d.AddDays(1)) 

产生无限循环

2。

if (d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday)
                            return;

完全退出循环。

  1. 看起来,远程服务器在短时间内有很多请求,存在某种性能问题。 可以通过在请求之间进行一些暂停来解决此问题(例如:Thread.Sleep(2000)-两秒钟的暂停)

因此,您的代码如下所示:

for (DateTime d = fromDatePicker.Value.Date; d <= toDatePicker.Value.Date; d = d.AddDays(1))
{
    if (d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday)
                            continue;

    string url = "http://cbar.az/currencies/" + d.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture) + ".xml";
    XmlDocument doc = new XmlDocument();
    doc.Load(url);

            Thread.Sleep(2000);
}

暂无
暂无

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

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