HttpWebResponse的Accept-Ranges标头值无效

时间:2016-11-25 16:21:41

标签: c# httpwebrequest partial

我有一个简单的代码来获取URL的Accept-Ranges标头值。最后,我尝试了一个测试代码的链接,它返回了错误的值。

  

方法1:此代码将 false 作为RangeAllowed返回。

string url = "https://cdn.gomlab.com/gretech/player/new/GOMPLAYERGLOBALSETUP.EXE";

HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
req.Credentials = CredentialCache.DefaultCredentials;
req.Accept = "*/*";
req.KeepAlive = false;
req.Timeout = 5000;
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
req.AllowAutoRedirect = true;
req.ProtocolVersion = HttpVersion.Version10;
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;

bool rangeAllowed = resp.GetResponseHeader("Accept-Ranges") == "bytes";
Console.WriteLine(rangeAllowed);

req.Abort();
resp.Close();
  

方法2: WebRequest.AddRange返回正确的PartialContent true

string url = "https://cdn.gomlab.com/gretech/player/new/GOMPLAYERGLOBALSETUP.EXE";

HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
req.Credentials = CredentialCache.DefaultCredentials;
req.Accept = "*/*";
req.KeepAlive = false;
req.Timeout = 5000;
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
req.AllowAutoRedirect = true;
req.ProtocolVersion = HttpVersion.Version10;

req.AddRange(0, 99);
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;

bool rangeAllowed = (resp.ContentLength == 100);
Console.WriteLine(rangeAllowed);

req.Abort();
resp.Close();

对我来说是不对的?如果没有,这个问题的原因是什么?

0 个答案:

没有答案