如何从URL(从windows应用程序到服务器的多个请求)从windows应用程序读取大量xml文件
本文关键字:应用程序 windows 读取 请求 文件 xml URL 服务器 | 更新日期: 2023-09-27 18:02:49
我有一个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);
}
你的东西完全错了。
-
方法AddDays(x)没有更新你的"d"变量,所以构造
for (DateTime d = fromDatePicker.Value.Date; d <= toDatePicker.Value.Date; d.AddDays(1))
produces endless loop
2.
if (d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday)
return;
完全退出循环。
- 似乎远程服务器在短时间内处理许多请求时出现了某种性能问题。它可以通过请求之间的一些暂停来解决(例如: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);
}