悉野小楼

导航

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{}
      }
}
有时用fiddler, httpwatch都能看到content-length,但.net的contentlength就是读不出来,可以用正则表达式自己找出来。

有时服务器确定没返回文件长度,只能慢慢读了。

posted on 2011-10-28 14:40  悉野  阅读(3846)  评论(0编辑  收藏  举报