简体   繁体   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#

I have a windows form where a user will be able to download all currency rates for selected date period. 我有一个Windows窗体,用户可以在其中下载选定日期段的所有货币汇率。 I have this now: 我现在有这个:

    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 format is like this depending on the date: http://cbar.az/currencies/15.07.2015.xml And for example if I select two week period, it gets rates for two days, then skips two days and etc. throwing an error not even reaching the end of the period: URL格式取决于日期,如下所示: http : //cbar.az/currencies/15.07.2015.xml例如,如果我选择两周时间段,它将获得两天的费率,然后跳过两天,依此类推。一个错误,甚至没有达到期末:

remote server returned an error (503) server unavailable 远程服务器返回错误(503)服务器不可用

I could guess that this is kind of server side protection against multiple client requests but do not know how to solve this problem. 我猜想这是针对多个客户端请求的服务器端保护,但不知道如何解决此问题。

It does not throw an error if I select period of 2 or 3 days. 如果选择2或3天,则不会引发错误。 But here it also may not get rates for all dates as well. 但是在这里它也可能无法获得所有日期的费率。

I would appreciate your help. 多谢您的协助。 Thank you. 谢谢。

Here is my whole code: 这是我的整个代码:

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);
                }

Your stuff is completely wrong. 你的东西是完全错误的。

  1. method AddDays(x) don't update your "d" variable, so construction of 方法AddDays(x)不会更新您的“ d”变量,因此构造

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

produces endless loop 产生无限循环

2. 2。

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

exits the loop completely. 完全退出循环。

  1. it seems, that the remote server has a some kind of performance problem with many requests at a short time. 看起来,远程服务器在短时间内有很多请求,存在某种性能问题。 It can be solved by some pause between requests (for example: Thread.Sleep(2000) - two seconds pause) 可以通过在请求之间进行一些暂停来解决此问题(例如:Thread.Sleep(2000)-两秒钟的暂停)

So, your code would look like this: 因此,您的代码如下所示:

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