本文介绍了此流不支持搜索操作。 HttpWebResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个通过http下载文件的程序。



我已经下载了,但是我想能够暂停下载,关闭程序,并在以后再次恢复。



我知道我正在从他们的支持下载他们的位置。



我正在通过HttpWebResponse下载文件,并使用GetResponseStream将响应读入Stream。



当我关闭应用程序并重新启动它时,我被卡住为如何恢复下载。我试过在流上进行搜索,但表示不支持。



最好的方法是什么?

解决方案

如果服务器支持此功能,则必须发送 Http标头与您的请求使用方法:

  request.AddRange(1024) ; 

这将指示服务器在第1千字节后开始发送文件。然后只需正常读取响应流。



要测试服务器是否支持恢复,您可以发送 HEAD 请求并测试是否发送 Accept-Ranges:bytes 头。


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.

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?

解决方案

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.

To test if a server supports resuming you can send a HEAD request and test if it sends the Accept-Ranges: bytes header.

这篇关于此流不支持搜索操作。 HttpWebResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:32