开发者

Lazy WebClient download

I'm downloading a开发者_Python百科 ~3MB text file using WebClient.DownloadString(), but I only need the first twenty lines of that file. Is there a way to get them without downloading the whole file?


Get hold of the HttpWebResponse, call GetResponseStream, call Read on it until you fetched what you want.

http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream.aspx

This doesn't necessarily reduce the load on the server.


You can use WebClient.OpenStream as in the example in the link.
Just replace sr.ReadToEnd() with 20x sr.ReadLine().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜