asp.net 获取银行货币汇率的代码
程序员文章站
2024-03-07 10:15:56
复制代码 代码如下: class exchangerate { private const string _baseurl = "网页地址"; public const s...
复制代码 代码如下:
class exchangerate
{
private const string _baseurl = "网页地址";
public const string currencycode = "货币类型以'|'隔开";//货币类型
public hashtable getvalues()
{
hashtable htreturn = new hashtable();
string url = _baseurl; //+ httputility.urlencode(tmset.tostring("yyyy/mm/dd", datetimeformatinfo.invariantinfo));
webclient wc = new webclient();
string shtml = wc.downloadstring(url);
string sxml = string.empty;
int ivaluecnt = currencycode.split(new char[] { '|' }, 10, stringsplitoptions.removeemptyentries).length;
string[] stbody = shtml.split(new string[] { "<tbody>", "</tbody>" }, stringsplitoptions.removeemptyentries);
foreach (string ss in stbody)
{
if (ss.contains("currency name"))
{
string[] sbrs = ss.split(new string[] { "</tr>" }, stringsplitoptions.removeemptyentries);
foreach (string sbr in sbrs)
{
string scur = string.empty;
double drate = getcurrencyrate(sbr, out scur);
if (drate != 0.0 && !string.isnullorempty(scur))
{
htreturn.add(scur, drate * 0.01);
if (htreturn.count >= ivaluecnt)
break;
}
}
break;
}
}
return htreturn;
}
private double getcurrencyrate(string source, out string scurrency)
{
scurrency = string.empty;
string spattern = @"<td.+?>(.+?)</td>";
foreach (match m in regex.matches(source, spattern))
{
string ss = m.groups[1].value;
if (isnumeric(ss))
return double.parse(ss);
else
{
if (currencycode.contains(ss))
scurrency = ss.trim();
else
break;
}
}
return 0.0;
}
public static bool isnumeric(string str)
{
if (string.isnullorempty(str)) return false;
system.text.regularexpressions.regex reg = new system.text.regularexpressions.regex(@"^[-]?\d+[.|,]?\d*$");
return reg.ismatch(str);
}
}
上一篇: 使用Java发送带附件的附件的示例