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()
.
精彩评论