webclient downloadstring(URI), URI has 10 mb file. Can I download a certain size of this file?

孤街浪徒 提交于 2021-02-11 14:24:08

问题


I have a 10 mb file at an URI I download it with the following codes.

WebClient wc = new WebClient(); string file = wc.DownloadString(http://www......com);

Can I download certain sizes of this file, such as 1 mb, 5 mb. not whole 10 mb. ? Then stop downloading?

Thanks.


回答1:


Add the HTTP Range header in your request. Note that this will only work if the server supports it.

See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for more information about it




回答2:


you can use httpwebrequest to getresponse. load whatever lines you want. check: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse%28v=vs.71%29.aspx

the example use ReadToEnd. you can use other methods to read just part of it.



来源:https://stackoverflow.com/questions/12976047/webclient-downloadstringuri-uri-has-10-mb-file-can-i-download-a-certain-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!