使用HttpWebRequest方法检查恢复能力

时间:2012-09-03 01:30:59

标签: c# httpwebrequest download httpwebresponse

这是我正在使用的代码:

private bool CheckPartialDL(String url)
{
    HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(url);
    hwr.Method = "HEAD";
    hwr.AddRange(0);

    try
    {
        HttpWebResponse hwbresp = (HttpWebResponse)hwr.GetResponse();
        if (hwbresp.StatusCode == HttpStatusCode.PartialContent)
        {
            hwbresp.Close();
            return true;
        }

    }//end of try

    catch (WebException wbe)
    {
        MessageBox.Show(wbe.Message, "Error");
    }//end of catch

    return false;

}//end of Check

但是服务器返回了以下错误:

远程服务器返回错误:(416)请求的范围不可满足。

我要下载以下文件格式:

http://windowsclient.net/sitefiles/1000/wpf/videos/source-code/creatingavideoapp-part1.zip

当我使用IDM检查恢复功能时,idm显示它可用。

所以我提出的代码错了?

2 个答案:

答案 0 :(得分:1)

这是一个包含一些信息的链接

http://www.checkupdown.com/status/E416.html

现在我对Resumable下载及其工作方式有一点了解,所以我看到的是 您没有为文件提供正确的字节范围。

假设文件大小为1500字节。 您必须将范围设置为150到324之间的范围。 当然,范围不得超过1500:)

所以我认为你没有为它提供合适的数据范围。

更多,请查看此链接问题,它指定范围  的字节数 HTTP Request with multiple Ranges

答案 1 :(得分:1)

我知道这个问题现在已经有几年了,但我只是遇到了完全相同的问题(和类似的代码),问题似乎是你不能同时拥有“Method ='HEAD'”和AddRange( )因为删除对AddRange的调用工作正常。

编辑 - 此post确认:

  

“服务器必须忽略随请求收到的Range标头字段   GET以外的方法“

相关问题