C# 获取汇率

C# 获取汇率,c#,winforms,networking,C#,Winforms,Networking,我想做的是 要从该网站获取转化率(我选择了xe.com 因为它支持所需的货币(美元) 例如,如果有可能发送“5 AZN”并获得美元等值。URL如下所示: 我从中抓取了以下片段: 现在,这将返回类似以下内容:美元兑欧元汇率:1.00美元=0.716372欧元 问题是: 我不知道变量buf和BUFFER\u SIZE是什么 如何获得准确的结果,例如发送5 AZN,并以美元(双倍)获得结果 buf是一个byte[]数组,一旦方法返回,它就包含您刚刚读取的数据。 BUFFER_SIZE是要读取的数

我想做的是

  • 要从该网站获取转化率(我选择了xe.com 因为它支持所需的货币(美元)
  • 例如,如果有可能发送“5 AZN”并获得美元等值。URL如下所示:
我从中抓取了以下片段:

现在,这将返回类似以下内容:美元兑欧元汇率:1.00美元=0.716372欧元

问题是:

  • 我不知道变量
    buf
    BUFFER\u SIZE
    是什么
  • 如何获得准确的结果,例如发送
    5 AZN
    ,并以美元(双倍)获得结果
  • buf是一个byte[]数组,一旦方法返回,它就包含您刚刚读取的数据。 BUFFER_SIZE是要读取的数据的大小。如果要读取单个字节,缓冲区大小=1。如果您想读取1 KB的数据,则缓冲区大小=1024,等等。请注意,如果您要求的缓冲区太大(例如,当数据为1KB时要求1MB),则这无关紧要。它将读取KB,然后返回

  • 除非XE.com决定对其进行更改,否则最终字符串应如下所示:

    XE.com:美元兑欧元汇率:1.00美元=0.716372欧元

    您可以使用String方法剥离不需要的内容:整个第一部分

    (XE.com:美元兑欧元汇率:)

    只需使用数据构建字符串即可轻松删除:

    (string header=“XE.com:{0}到{1}比率:”,currency1,currency2)

    ,然后调用
    String.Replace(标题“”)
    。从那里,您可以调用
    String.Split('=')
    ,在'='符号处拆分,然后从拆分的字符串中删除货币部分(同样,
    String.Replace()
    ),最后调用
    Double.TryParse()

  • 注意:codesparkle的方法更简单,因为您基本上跳过了步骤1。但是XE.com没有提供API:您不能保证返回的字符串是有效的,或者将来某一天不会更改

    好的,这里有一些代码:

    private double GetConvertedCurrencyValue(string inputCurrency, string outputCurrency, double value) 
    {
        string request = String.Format(http://www.xe.com/ucc/convert.cgi?Amount={0}&From={1}&To={2}", value, inputCurrency, outputCurrency);
    
        System.Net.WebClient wc = new System.Net.WebClient();
        string apiResponse = wc.DownloadString(request);    // This is a blocking operation.
        wc.Dispose();
    
        /* Formatting */
        // Typical response: "XE.com: curr1 to curr2 rate: x curr1 = y curr2"
        // The first part, up until "x curr1" is basically a constant
        string header = String.Format("XE.com: {0} to {2} rate:" inputCurrency, outputCurrency);
    
        // Removing the header
        // The response now looks like this: x curr1 = y curr2
        apiResponse = apiResponse.Replace(header, "");
    
        // Let's split the response at '=', to retrieve the right part
        string outValue = apiResponse.Split('=')[1];
    
        // Getting rid of the 'curr2' part
        outValue = outValue.Replace(outputCurrency, "");
    
        return Double.Parse(outValue, System.Globalization.CultureInfo.InvariantCulture);
    }
    

    删除了旧代码。这是迄今为止最准确的一个 编辑2:

    这应该尽量将每个单词解析为double,忽略等于inputvalue(需要转换的数字)的double。
    这仍然不是100%准确,就好像他们将其他单独的数字添加到字符串中,它接受该值一样。

    我建议忘记
    WebRequest
    /
    WebResponse
    ;使用或改为…@codesparkle你能展示一下你对代码的想法吗?只要看看,这不是火箭科学,不管你选择什么方法来检索html内容,别忘了这是非常不可靠的。您不能保证xe.com不会重新设计页面布局,您的整个代码都会失败。你应该考虑寻找一个提供这样功能的Web服务。考虑使用(AZN应该从明天开始,目前不可用,因为“不可靠的来源”)这是一个非常,非常,非常坏的想法。如果明天,消息变成“XE.com:”会怎么样?你只是有一个新的空格,你将尝试解析“=”。@pikzen使它变得更好了,但如果字符串可以长期更改,你就不能100%准确。当然,除非你们再次用文字分开。检查是否可以使用double.TryParse()将其解析为double,然后使其忽略您要转换的double并获取第二个double。这很可能在一个字节[]内始终有效。一个变量,比如byte[]data=新字节[1024];但是说真的,使用codesparkle的方法(使用DownloadString())我是c#的新手。你能根据codesparkles的想法修改你的答案吗?请提前解释代码的第二个答案。我很难理解。真正地Thx in advanceI在运行代码“\”\”>\n\t\n\t\t\n时出现此错误
    private double GetConvertedCurrencyValue(string inputCurrency, string outputCurrency, double value) 
    {
        string request = String.Format(http://www.xe.com/ucc/convert.cgi?Amount={0}&From={1}&To={2}", value, inputCurrency, outputCurrency);
    
        System.Net.WebClient wc = new System.Net.WebClient();
        string apiResponse = wc.DownloadString(request);    // This is a blocking operation.
        wc.Dispose();
    
        /* Formatting */
        // Typical response: "XE.com: curr1 to curr2 rate: x curr1 = y curr2"
        // The first part, up until "x curr1" is basically a constant
        string header = String.Format("XE.com: {0} to {2} rate:" inputCurrency, outputCurrency);
    
        // Removing the header
        // The response now looks like this: x curr1 = y curr2
        apiResponse = apiResponse.Replace(header, "");
    
        // Let's split the response at '=', to retrieve the right part
        string outValue = apiResponse.Split('=')[1];
    
        // Getting rid of the 'curr2' part
        outValue = outValue.Replace(outputCurrency, "");
    
        return Double.Parse(outValue, System.Globalization.CultureInfo.InvariantCulture);
    }
    
    string[] words = result.Split(' ');
    Double newresult;
    
    foreach(string i in words)
    {
        if(Double.TryParse(i) == true)
        {
            if(!Double.Parse(i).equals(inputValue))
            {
                newresult = Double.Parse(i);
                break;
            }
        }
    }