HttpWebResponse得文件长度 可以用ContentLength, 也可以读headers
long lFileLength = -1; //文件长度
lFileLength = webResponse.ContentLength;
if (lFileLength == -1)
{
Match match = new Regex(@"(?<=(content-length:\s*))\d+", RegexOptions.IgnoreCase).Match(webResponse.Headers.ToString());
if (match != null)
{
try
{
lFileLength = long.Parse(match.ToString());
}catch{}
}
}
lFileLength = webResponse.ContentLength;
if (lFileLength == -1)
{
Match match = new Regex(@"(?<=(content-length:\s*))\d+", RegexOptions.IgnoreCase).Match(webResponse.Headers.ToString());
if (match != null)
{
try
{
lFileLength = long.Parse(match.ToString());
}catch{}
}
}
有时服务器确定没返回文件长度,只能慢慢读了。