获取货币汇率

时间:2012-07-08 17:00:11

标签: c# winforms networking

我想做的是,

我从a blog获取了以下代码段:

string xeString = String.Format("http://www.xe.com/ucc/convert.cgi?Amount=1&From={0}&To={1}", srcCurrency, dstCurrency);
System.Net.WebRequest wreq = System.Net.WebRequest.Create(new Uri(xeString));
System.Net.WebResponse wresp = wreq.GetResponse();
Stream respstr = wresp.GetResponseStream();
int read = respstr.Read(buf, 0, BUFFER_SIZE);
result = Encoding.ASCII.GetString(buf, 0, read);

现在,这将返回XE.com: USD to EUR rate: 1.00 USD = 0.716372 EUR

之类的内容

问题是:

  1. 我不知道变量bufBUFFER_SIZE是什么。
  2. 如何获得准确的结果,例如发送5 AZN并以美元(双倍)获得结果? http://www.xe.com/ucc/convert/?Amount=5&From=AZN&To=USD

2 个答案:

答案 0 :(得分:1)

  1. http://msdn.microsoft.com/fr-fr/library/system.io.stream.read.aspx

    buf是一个byte []数组,一旦返回该方法,就会包含刚刚读取的数据。 BUFFER_SIZE是您要读取的数据的大小。如果要读取单个字节,BUFFER_SIZE = 1。如果你想读一千字节的数据,BUFFER_SIZE = 1024等。注意,如果你要求一个太大的缓冲区(例如,当数据是1KB时要求1MB),它就不会有多大意义。它将读取KB,然后返回。

  2. 您的最终字符串应如下所示,除非XE.com决定更改它:

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

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

    (XE.com: USD to EUR rate:)

    只需使用您的数据构建一个字符串即可轻松删除

    (string header = "XE.com: {0} to {1} rate:", currency1, currency2)

    ,然后调用String.Replace(header, '')。在那里,您可以拨打String.Split('='),在' ='签名,然后从拆分的字符串中删除货币部分(再次,String.Replace()),最后调用Double.TryParse()

  3. 注意: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);
    }
    

答案 1 :(得分:-2)

删除旧代码。这是迄今为止最准确的一个 EDIT2:

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

这应该试着解析每个单词加倍并忽略等于inputvalue的double(需要转换的数字)。 这仍然不是100%准确,好像他们将其他单独的数字添加到它所取的那个值的字符串。