中国人民银行人民币最新汇率抓取

///


    /// 正则表达式取数组
    ///

    ///
    ///
    static int intTime = 0;
    private string[] StrToArray(string s)
    {
        intTime = System.Environment.TickCount;
        MatchCollection mc = Regex.Matches(s, "[0-9]{1,}.[0-9]{4,}");
        string[] res = new string[mc.Count];
        for (int i = 0; i < mc.Count; i++)
        {
            res[i] = mc[i].Value.Substring(0, mc[i].Value.Length);
        }
        intTime = System.Environment.TickCount - intTime;
        return res;
    }

public string GetHttpData(string Url)
    {
        string sException = null;
        string sRslt = null;
        WebResponse oWebRps = null;
        WebRequest oWebRqst = WebRequest.Create(Url);
        oWebRqst.Timeout = 50000;
        try
        {
            oWebRps = oWebRqst.GetResponse();
        }
        catch (WebException e)
        {
            sException = e.Message.ToString();
        }
        catch (Exception e)
        {
            sException = e.ToString();
        }
        finally
        {
            if (oWebRps != null)
            {
                StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("GB2312"));
                sRslt = oStreamRd.ReadToEnd();
                oStreamRd.Close();
                oWebRps.Close();
            }
        }
        return sRslt;
    }

 

 

protected void getHuiLvPrice()
    {
        string temp = string.Empty;
        string temp1 = string.Empty;
        string temp2 = string.Empty;
        string temp3 = string.Empty;
        string temp4 = string.Empty;
        string temp5 = string.Empty;
        string htmlStr = string.Empty;

        string rS = string.Empty;
        string regText = "[0-9]{1,}.[0-9]{4,}";

            string httpData = GetHttpData("http://www.pbc.gov.cn/huobizhengce/huobizhengcegongju/huilvzhengce/renminbihuilvjiaoyishoupanjia.asp");
            if (!string.IsNullOrEmpty(httpData))
            {
                string[] arry = StrToArray(httpData);
                for (int i = 0; i < 5; i++)
                {
                    htmlStr = arry.GetValue(i).ToString();
                    if (!string.IsNullOrEmpty(htmlStr))
                    {
                        htmlStr = Regex.Replace(htmlStr, "//s{3,}", "");
                        htmlStr = htmlStr.Replace("/r", "");
                        htmlStr = htmlStr.Replace("/n", "");
                        Regex Re = new Regex(regText);
                        string huilv = string.Empty;

                        Match Ma = Re.Match(htmlStr);
                        if (Ma.Success)
                        {
                            huilv = Ma.Groups[0].Value;
                        }
                        if (!string.IsNullOrEmpty(huilv))
                        {
                            float chinam = float.Parse("100") * float.Parse(huilv);
                            float chinam1 = float.Parse(huilv) / float.Parse("100");
                            if (i == 0)
                            {
                                temp1 = "美元100=" + "RMB" + chinam.ToString() + "
";
                            }
                            else if (i == 1)
                            {
                                temp2 = "欧元100=" + "RMB" + chinam.ToString() + "
";
                            }
                            else if (i == 2)
                            {
                                temp3 = "日元100=" + "RMB" + chinam.ToString() + "
";
                            }
                            else if (i == 3)
                            {
                                temp4 = "港币100=" + "RMB" + chinam.ToString() + "
";
                            }
                            else if (i == 4)
                            {
                                temp5 = "英镑100=" + "RMB" + chinam.ToString();
                            }
                        }
                    }
                }
        }

    }

你可能感兴趣的:(Asp.Net,string,regex,正则表达式,null,exception,float)