C# .net WebRequest google 财务
C# .net WebRequest google finance
我正在使用 google 财务将一种货币转换为另一种货币。
我正在使用的代码如下所示,运行良好。但是,今天,我遇到了 IndexOutofrange 异常,并且在下面搜索的索引中得到了 -1 的结果(这意味着我的结果不包含 CONVERTED VALUE,它在记录后是 100% 正确的。
然后我转到同一个 Web 请求调用,为其提供相同的参数,然后从 Web 浏览器检查源代码,我得到了 VALUE 。
您认为可能是什么问题?我从网络浏览器得到了整个结果,而从我的应用程序得到的结果缺少转换值字段。
private static string CurrencyConvert(decimal amount, string fromCurrency, string toCurrency)
{
try
{
//Grab your values and build your Web Request to the API
string apiURL = String.Format("https://www.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}", amount, fromCurrency, toCurrency, Guid.NewGuid().ToString());
//Make your Web Request and grab the results
var request = WebRequest.Create(apiURL);
//Get the Response
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.ASCII);
//Grab your converted value (ie 2.45 USD)
String temp = streamReader.ReadToEnd().ToString();
int pFrom = temp.IndexOf("<span class=bld>") + ("<span class=bld>").Length;
int pTo = temp.LastIndexOf("</span>");
System.Windows.MessageBox.Show(pFrom.ToString() + " " + pTo.ToString());
String result = temp.Substring(pFrom, pTo - pFrom);
// string result = Regex.Matches(streamReader.ReadToEnd(), "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value;
//Get the Result
return result;
}
catch(Exception ex )
{
return "";
}
}
URL 有问题。使用这个:https://finance.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}
不需要元参数
https://finance.google.com/finance/converter?a={0}&from={1}&to={2} 也可以正常工作
我正在使用 google 财务将一种货币转换为另一种货币。 我正在使用的代码如下所示,运行良好。但是,今天,我遇到了 IndexOutofrange 异常,并且在下面搜索的索引中得到了 -1 的结果(这意味着我的结果不包含 CONVERTED VALUE,它在记录后是 100% 正确的。
然后我转到同一个 Web 请求调用,为其提供相同的参数,然后从 Web 浏览器检查源代码,我得到了 VALUE 。
您认为可能是什么问题?我从网络浏览器得到了整个结果,而从我的应用程序得到的结果缺少转换值字段。
private static string CurrencyConvert(decimal amount, string fromCurrency, string toCurrency)
{
try
{
//Grab your values and build your Web Request to the API
string apiURL = String.Format("https://www.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}", amount, fromCurrency, toCurrency, Guid.NewGuid().ToString());
//Make your Web Request and grab the results
var request = WebRequest.Create(apiURL);
//Get the Response
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.ASCII);
//Grab your converted value (ie 2.45 USD)
String temp = streamReader.ReadToEnd().ToString();
int pFrom = temp.IndexOf("<span class=bld>") + ("<span class=bld>").Length;
int pTo = temp.LastIndexOf("</span>");
System.Windows.MessageBox.Show(pFrom.ToString() + " " + pTo.ToString());
String result = temp.Substring(pFrom, pTo - pFrom);
// string result = Regex.Matches(streamReader.ReadToEnd(), "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value;
//Get the Result
return result;
}
catch(Exception ex )
{
return "";
}
}
URL 有问题。使用这个:https://finance.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}
不需要元参数 https://finance.google.com/finance/converter?a={0}&from={1}&to={2} 也可以正常工作