获取货币汇率 [英] Get currency rate
问题描述
我想做的是,
- 从该网站获得转化率(我选择了 xe.com因为它支持所需的货币 AZN <-> USD)
- 例如,如果可以发送5 AZN"并获得它等于美元的金额.URL 如下所示:http://www.xe.com/ucc/convert/?Amount=5&From=AZN&To=USD
我从博客一个>:
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
Now, this will return something like XE.com: USD to EUR rate: 1.00 USD = 0.716372 EUR
问题是:
- 我不知道变量
buf
和BUFFER_SIZE
是什么. - 如何获得准确的结果,例如发送
5 AZN
并以美元获得结果(双倍)?http://www.xe.com/ucc/convert/?Amount=5&From=AZN&To=USD
- I have no idea what the variables
buf
andBUFFER_SIZE
are. - How can I get an exact result, for instance send
5 AZN
and get the result in USD (as double)? http://www.xe.com/ucc/convert/?Amount=5&From=AZN&To=USD
推荐答案
buf 是一个 byte[] 数组,一旦方法返回,其中包含您刚刚读取的数据.BUFFER_SIZE 是您要读取的数据的大小.如果要读取单个字节,则 BUFFER_SIZE=1.如果要读取一千字节的数据,BUFFER_SIZE=1024等.注意,如果你要求一个太大的缓冲区(例如,当数据为1KB时要求1MB),它不会有太大关系.它将读取一个 KB,然后返回.
buf is a byte[] array, which, once the method has returned, contains the data that you just Read. BUFFER_SIZE is the size of the data you want to read. If you want to read a single byte, BUFFER_SIZE=1. If you want to read one kilobyte of data, BUFFER_SIZE=1024, etc. Note that, if you ask for a too big buffer (e.g. asking for 1MB when the data is 1KB), it won't matter much. It will read a KB, and return.
您的最终字符串应如下所示,除非 XE.com 决定更改它:
Your final string should look like this, unless XE.com decides to change it:
XE.com:美元兑欧元汇率:1.00 美元 = 0.716372 欧元
XE.com: USD to EUR rate: 1.00 USD = 0.716372 EUR
您可以使用 String 方法去除不需要的东西:整个第一部分
You can use the String methods to strip things you don't need: The whole first part
(XE.com:美元兑欧元汇率:)
只需使用您的数据构建一个字符串即可轻松删除:
can be easily removed by just building a string with your data:
(string header = "XE.com: {0} to {1} rate:",currency1,currency2)
,然后调用String.Replace(header, '')
.从那里,您可以调用 String.Split('=')
,在 '=' 符号处拆分,然后从拆分的字符串中删除货币部分(同样,String.Replace()
) 最后调用 Double.TryParse()
,then calling String.Replace(header, '')
. From there, you can call String.Split('=')
, splitting at the '=' sign, then removing the currency part from the splitted strings (again, String.Replace()
) and finally calling Double.TryParse()
注意:codesparkle 的方法更简单,因为你基本上跳过了第 1 步.但是 XE.com 没有提供 API:你不能保证返回的字符串是有效的,或者将来某天不会改变.
Note: codesparkle's method is way easier, because you basically skip step 1. But XE.com are not providing an API: You have no guarantee the returned string will be valid, or won't change someday in the future.
好的,这是一些代码:
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);
}
这篇关于获取货币汇率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!