日期:2014-06-10  浏览次数:21116 次

     给一个搞经融的企业做官网,外汇汇率突然没有数据,

    汇率接口用的某网站提供的webservice接口,进入某网,打开接口,哦霍

  

 坑,然后速度的呼叫谷哥,招来度娘…….尼玛,网上大部分都是用的某网webservice接口,

哦霍,请允许我幸灾乐祸一下,虽然这不对…

  好吧,去各大财经网站搜索外汇…..

   最后跟客户沟通…取新浪财经的数据吧

http://vip.stock.finance.sina.com.cn/mkt/#jbhl_forex

哦豁,F12按起,

找到了,ok,开始搞数据了(这算不算违法啊!网上看到有的银行你搞汇率,弄接口,你得给毛爷爷)

public List<SymbolBizEntity> GetHuilv()
        {
            Random rn = new Random();
            string url = "http://hq.sinajs.cn/rn=pcr" + rn.Next(10, 1000) + "&list=EURUSD,DINIW,USDJPY,GBPUSD,USDCAD,USDCHF,AUDUSD,NZDUSD";
            string xml = GetPage(url, Encoding.UTF8);

            string[] strArr = xml.Split(';');
            List<SymbolBizEntity> list = new List<SymbolBizEntity>();
            for (int i = 0; i < strArr.Length; i++)
            {
                string[] arrList = strArr[i].Split('=');
                if (arrList.Length > 1)
                {
                    string[] arrthree = arrList[1].Split(',');
                    SymbolBizEntity entity = new SymbolBizEntity();
                    entity.BasePrice = arrthree[1].ToString();
                    entity.Name = arrList[0].Split(' ')[1].ToString();
                    list.Add(entity);
                }
            }

            return list;

        }

public string GetPage(string url, Encoding encoding)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            string html = "";
            try
            {
                System.Threading.Thread.Sleep(1000);
                request = (HttpWebRequest)WebRequest.Create(url);
                request.UserAgent = "www.svnhost.cn";

                request.Timeout = 20000;
                request.AllowAutoRedirect = false;
                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
                {
                    reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
                    html = reader.ReadToEnd();

                }
                else
                {
                    //LogHelper.Log.Info("-----------url:" + url + "- 抓不到数据------------------------------------");

                }

            }
            catch (Exception ex)
            {
                //LogHelper.Log.Info("-----------url:" + url + "- GetPage : ex:" + ex.Message + "------------------------------------");
            }
            finally
            {
                if (response != null)
                {
                    response.Close();

                    response = null;

                }
                if (reader != null)
                    reader.Close();
                if (request != null)
                    request.Abort();
                request = null;

            }
            if (html.Length <= 0)
            {
                //LogHelper.Log.Info("-----------url:" + url + "- 没有抓到数据------------------------------------");
            }
            return html;

        }

 


 

Ok,获取到了数据就可以,后面的你想怎么搞,就怎么。。。。。。

请求url中list参数可以根据各种汇率自己增加…..

"&list=EURUSD,DINIW,USDJPY,GBPUSD,USDCAD,USDCHF,AUDUSD,NZDUSD";

我这里八种,欧元美元,美元指数….

噢啦…..

(老看博客,代码也每天写,一个月过去,总感觉自个白混了,代码写了,没多久也忘了,然后重新去找….o()︿))o 唉,还是写博客吧!留下个印记,再次总结…)