【问题标题】:Getting exchange rates from the internet从互联网上获取汇率
【发布时间】:2012-07-08 05:32:47
【问题描述】:

我想做的是,从互联网上获取汇率。 经过长时间的研究,我发现了这个功能。

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
        string xmlResult = null;
        string url;
        url = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=" + TextBox1.Text + "&ToCurrency=" + TextBox2.Text + "";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader resStream = new StreamReader(response.GetResponseStream());
        XmlDocument doc = new XmlDocument();
        xmlResult = resStream.ReadToEnd();
        doc.LoadXml(xmlResult);
        Label1.Text = "Current Exchange Rate for " + TextBox1.Text.ToUpper() + " ---> " + TextBox2.Text.ToUpper() + " value " + doc.GetElementsByTagName("double").Item(0).InnerText;
        }
        catch(Exception ex)
        {
            Label1.Text="Not a valid Currency or Try again later";
        }
    } 

http://www.webservicex.net/ 不支持 AZN(阿塞拜疆马纳特)到美元的转换,反之亦然。我想做的是,如果可以连接到互联网并获得费率。否则使用书面函数进行转换(我已经写过)。

您有什么建议,我怎样才能获得美元和 AZN 的当前汇率(或者只是通过发送美元或 AZN 获得结果)?有没有办法从 Windows 窗体应用程序中获取它?

【问题讨论】:

    标签: c# windows winforms visual-studio-2010 visual-studio


    【解决方案1】:

    这个简单的算法将在键值对列表中为您提供所需的一切。

    public static List<KeyValuePair<string, decimal>> GetCurrencyListFromWeb(out DateTime   currencyDate)
        {
            List<KeyValuePair<string, decimal>> returnList = new List<KeyValuePair<string, decimal>>();
            string date = string.Empty;
            using (XmlReader xmlr = XmlReader.Create(@"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"))
            {
                xmlr.ReadToFollowing("Cube");
                while (xmlr.Read())
                {
                    if (xmlr.NodeType != XmlNodeType.Element) continue;
                    if (xmlr.GetAttribute("time") != null)
                    {
                        date = xmlr.GetAttribute("time");
                    }
                    else returnList.Add(new KeyValuePair<string, decimal>(xmlr.GetAttribute("currency"), decimal.Parse(xmlr.GetAttribute("rate"), CultureInfo.InvariantCulture)));
                }
                currencyDate = DateTime.Parse(date);
            }
            returnList.Add(new KeyValuePair<string, decimal>("EUR", 1));
            return returnList;
        }
    

    【讨论】:

    • 如需更深入的解释请留言。
    • 一段不错的代码,但是否有理由不选择通常的Dictionary&lt;&gt;
    【解决方案2】:

    http://www.webservicex.net/ 不支持 AZN(阿塞拜疆马纳特)转美元,反之亦然

    所以?计算通过另一种货币的交叉汇率。

    AZN 可能是一种交易量或敞口非常有限的边缘货币。询问 OANDA (http://www.oanda.com) 我收到了一些报价,包括美元兑换 (http://www.oanda.com/currency/cross-rate/result?quotes=GBP&quotes=EUR&quotes=JPY&quotes= CHF&quotes=USD&quotes=AZN&go=Get+my+Table+%3E)

    可能 webservicesx.net 只是没有主要货币以外的东西的价格。

    使用另一个引用。 FXCM 和 Oanda 可能有您可以订阅的 API - 可能需要付费。

    另外,您可以查看是否可以计算交叉 - 如果有价格,则从 AZN 转到另一种货币,然后从那里转到美元。这在外汇交易中经常发生 - 不错 - 美元大多不需要交叉汇率计算。

    有没有办法从 Windows 窗体应用程序中获取它?

    当你在网上询问一个API时,无论是winforms、webforms、powershell还是vb脚本,API支持与否,都与你使用的UI技术无关。

    【讨论】:

      【解决方案3】:

      也许这会有所帮助。我用谷歌搜索了一些替代的网络服务,但我看到的那些不支持 AZN。但我没有花很多时间做那件事,那是你的工作。我确实找到了这个:
      http://www.transfermate.com/en/free_currency_converter.asp

      您可以将其添加到您的应用程序中,可能是通过添加浏览器控件并将其嵌入自定义页面并将结果检索到您的主窗体。但最终,你自己回答了这个问题:

      其他使用书面函数进行转换(我已经写过)。

      如果您已经找不到解决方案,请自行构建。

      也可以试试: https://developers.google.com/finance/http://openexchangerates.org/

      【讨论】:

        猜你喜欢
        • 2013-06-14
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 2016-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多