开发者

Lazy WebClient download

开发者 https://www.devze.com 2023-02-15 07:11 出处:网络
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?

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

精彩评论

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