登录站点

用户名

密码

注册

兴趣联盟 - 交易系统实现及开发

  • gats

    BeeGee Post subject: 如何用Cookie从Yahoo获取实时数据(转)

    gats 2009-09-14 23:58

    BeeGee  Post subject: 如何用Cookie从Yahoo获取实时数据Posted: 9/12/09 17:59  
     

    有一个168er通过pm问我,为什么他买了yahoo的实时数据服务,可是他自己做的程序去quote数据回来一看,还是延迟了15分钟?我在pm里面回答他,结果没有写完168的网页就出问题了,写了那么长全部丢了。现在趁这个周末有空,写在这里给碰到同样问题的人参考一下。

    我曾碰到同样的问题,就在这里发帖提问,小I告诉我说yahoo是靠cookie来分辨来qote数据的用户是否付费用户的,这给我一个重要提示,不过也有人回答我说在他们的电脑上,他们自己的程序没有碰到这个问题。

    后来我根据小I的提示,用下面的方法解决了这个问题(再次感谢小I):


    1.假设你执行quote数据任务的类是MarketScanner,在MarketScanner里定义一个变量CookieContainer mycc备用;


    2.在MarketScanner里面建立一个函数(或方法):getYahooCookie(),然后在getYahooCookie()里面用HttpWebRequest类加上你自己的用户名和密码去登录yahoo的页面:https://login.yahoo.com/


    3.登录成功后,在返回的HttpWebResponse里面就包含了yahoo给你的cookie,把这个cookie保存在CookieContainer mycc里面备用。


    4.这些准备动作可以在MarketScanner的构造函数里面完成,一旦完成,你这个MarketScanner就带有yahoo的cookie,每次你需要去yahoo quote数据的时候,把这个yahoo cookie附上就可以得到实时数据,比如这样:

    String urlStr=“http://download.finance.yahoo.com/d/?s=QQQQ&f=opabl1v";
    HttpWebRequest myrequest=(HttpWebRequest)WebRequest::Create(urlStr);
    // 在这里把之前已经取得的有效yahoo cookie赋值给myrequest
    if(this.mycc!=nullptr && this.mycc.Count>0)       
        myrequest.CookieContainer=this.mycc;
    ....

    我曾经google到有人提到用电脑里存有的现成的cookie来做一些类似需要cookie的事(比如自动登录,自动发贴什么的),我用这个思路试了一下,没有成功,如果愿意尝试,应该也是一个好的思路。

     

     

    dhh  Post subject: Re: 如何用Cookie从Yahoo获取实时数据Posted: 9/12/09 19:04  
     

    Joined: 8/27/06 01:40
    Posts: 2155  我google到一个回答。

    When using a HttpWebRequest or a HttpWebResponse, you might need to send or receive cookies. If you try to access directly the Cookie property of your HttpWebRequest, no cookie will be returned.

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/MyApp/MyPage");HttpWebResponse response = (HttpWebResponse)request.GetResponse();// response.Cookies is always empty
    It's a bit tricky, but to be able to access the cookies, you need to first assign the CookieContainer of your request.

    A CookieContainer contains cookies for many requests. In fact, while the CookieCollection just store a list of Cookie, the CookieContainer store many CookieCollection, each for a specified URI. It allows storing cookies for different URI in a single place.


    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/MyApp/MyPage");CookieContainer cookieContainer = new CookieContainer();request.CookieContainer = cookieContainer;HttpWebResponse response = (HttpWebResponse)request.GetResponse();if (response.Cookies != null && response.Cookies.Count != 0){    foreach(Cookie cookie in response.Cookies)    {      // Access to each cookie here    }}else{    // No cookie}
    In some cases, you might need to perform several requests, GET or POST, toward several URLs, and pass each time the cookie information gathered from the previous requests. The easiest way to do that is to reuse your CookieContainer object. For instance:


    CookieContainer cookieContainer = new CookieContainer();HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://localhost/MyApp/MyPage1");request1.CookieContainer = cookieContainer;HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();// Do something hereHttpWebRequest request2 = (HttpWebRequest)WebRequest.Create("http://localhost/MyApp/MyPage2");request2.CookieContainer = cookieContainer;HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
    By doing this way, you share your cookies between the two requests. The cookies retrieve in response1 are store in your cookieContainer, and then sent within request2. Cookies retrieved in response2 are added or updated inside your cookieContainer, and so on.

     
     
     

你还不是该群组正式成员,不能参与讨论。 现在就加入