我在做一个程序,它会下载通过HTTP文件。
I'm making a program which downloads files over http.
我找到了下载,但是我希望能够暂停下载,关闭计划在以后再次恢复它们。
I've got it downloading, however I want to be able to pause the downloads, close the program and resume them again at a later date.
我知道我是从支持下载他们的位置这一点。
I know the location i'm downloading them from supports this.
我下载该文件通过HttpWebResponse和读取使用GetResponseStream响应到流。
I'm downloading the file through HttpWebResponse and reading the response into a Stream using GetResponseStream.
当我关闭该应用程序并重新启动它,我卡作为如何恢复下载。我试着做一个寻求流,但它指出其不支持。
When i close the app and restart it, I'm stuck as to how resume the download. I've tried doing a seek on the stream but it states its not supported.
什么是做到这一点的最好方法是什么?
What would be the best way to do this?
推荐答案如果服务器支持这一点,你需要发送的使用的 HTTP头您的要求=msdn.microsoft/en-us/library/system.httpwebrequest .addrange.aspx相对=nofollow>的AddRange 方式:
If the server supports this you have to send the Range Http header with your request using the AddRange method:
request.AddRange(1024);这将指示服务器开始第一千字节后发送文件。然后,只需读取响应流是正常的。
This will instruct the server to start sending the file after the 1st kilobyte. Then just read the response stream as normal.
要测试一个服务器支持恢复您可以发送 HEAD 要求并测试它发送接受-范围:字节头
To test if a server supports resuming you can send a HEAD request and test if it sends the Accept-Ranges: bytes header.
更多推荐
该流不支持查找操作。 HttpWebResponse
发布评论