开发者

Google Finance, how to get the JSON data streamed?

I tried to explain this earlier, but obviously failed!

So, if you have a google finance graph open, for instance:

http://www.google.com/finance?q=INDEXNASDAQ:.IXIC

I would like to somehow use the (HttpWebRequest) object in C# so that I can grab the small data which google sends to the page to update the graph.

A friend mentioned this was JSON?

I was trying to use the following code example, but even when i set the keep alive property to 'true', it still wouldnt work:

h开发者_Go百科ttp://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.keepalive.aspx#Y369


You also need to change the example's line that sets the Connection property to Close. Comment out this line (along with keeping the keep-alive property set to true):

myHttpWebRequest2.Connection = "Close";

You do that and your example should run fine.

Regarding getting the data and using HttpWebRequest to work with it, you can do that. The data returned isn't JSON - it looks like straight text and I'm guessing Google's javascript is parsing it out. (I haven't inspected the javascript on Google Finance's page, but that's my guess.)

Using Fiddler, the response from this URL:

http://www.google.com/finance/getprices?q=.IXIC&x=INDEXNASDAQ&i=120&p=10m&f=d,c,v,o,h,l&df=cpct&auto=1&ts=1307994768643

looks like this:

EXCHANGE%3DINDEXNASDAQ
MARKET_OPEN_MINUTE=570
MARKET_CLOSE_MINUTE=960
INTERVAL=120
COLUMNS=DATE,CLOSE,HIGH,LOW,OPEN,VOLUME
DATA=
TIMEZONE_OFFSET=-240
a1307994120,2641.12,2641.12,2639.96,2640.01,0
1,2638.76,2642.14,2638.76,2641.13,0
2,2638.95,2640.54,2638.74,2638.79,0
3,2639.85,2640.01,2638.08,2638.95,0
4,2640.07,2640.87,2639.31,2639.88,0
5,2640.31,2640.48,2639.42,2640.08,0
6,2641.09,2641.09,2640.3,2640.31,0

A little cryptic, but you can see how the COLUMNS line lines up with the data at the bottom. Also, the f querystring parameter seems to be indicating which columns to return (d=date, c=close,v=volume,o=open,h=high,l=low).

EDIT: I should mention that the URL I used is being sent from the finance graph page to get updated data - you can see this URL being requested at regular intervals using a tool like Fiddler. The response data that I pasted above is also output by the sample application from MSDN.

But commenting out that one line in the example from MSDN and a little fiddling with Fiddler should give you the data and clues you need to parse the return that comes from that URL.

I hope this helps!

PS - my first line in my modified MSDN example looks like this:

HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create("http://www.google.com/finance/getprices?q=.IXIC&x=INDEXNASDAQ&i=120&p=10m&f=d,c,v,o,h,l&df=cpct&auto=1&ts=1307994768643");

I made a similar change to the other WebRequest call a little further down in the example...other than that, I didn't change anything else in the example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜