Content-Length = -1一般是由于请求超时,可能是Internet资源不存在或是网速慢,断掉了,你可以再多发几个请求试试!如果读了一半断了,你可以用断点续传的代码!while (blockBuffer.Length > 0)//这样判断是不对的,因为有时候读取的就是0。你应该把所以读过的字节全都加起来和Content-Length比较!
int iFileLength=0;
while (iFileLength<Content-Length)
{
    iFileLength+=blockBuffer.Length;
}

解决方案 »

  1.   

    可是,对于sina的网站http://news.sina.com.cn/old1000/news1000_20050204.shtml,无论怎么读,长度都是0 ,还有什么办法吗?
      

  2.   

    我用ie请求了一下,请求和应答的http头如下。
    服务器确实没有返回内容的长度,我改怎么办呢?GET /old1000/news1000_20050204.shtml HTTP/1.1
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
    Accept-Language: zh-cn
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)
    Host: news.sina.com.cn
    Connection: Keep-Alive
    Cookie: UNIPROPATH=|*||pid:1-0-0-0-0|news.sina.com.cn/|st:0|et:1109296474419||hp:N|*|; UNIPROCT=1-5-1:2|1-0-0:5|1-5-4:1|2-0-0:1|59-23-290:1|1-4-2:1|1-6-4:1; icast_42418_57=1HTTP/1.0 200 OK
    Date: Tue, 01 Mar 2005 07:41:51 GMT
    Server: Apache/2.0.52 (Unix)
    Last-Modified: Fri, 04 Feb 2005 03:01:07 GMT
    ETag: "39255e-1fd5ce-b19e02c0"
    Accept-Ranges: bytes
    X-Powered-By: mod_xlayout_jh/0.0.1e
    Vary: Accept-Encoding
    Content-Encoding: gzip
    Cache-Control: max-age=120
    Expires: Tue, 01 Mar 2005 07:43:51 GMT
    Content-Type: text/html
    X-Cache: MISS from sqsh-238.sina.com.cn
      

  3.   

    [MSDN]
    ContentLength 属性包含与响应一起返回的 Content-Length 标头的值。如果响应中未设置 Content-Length 标头,则将 ContentLength 设置为值 -1。可能是这个原因!
    但是文件是能都出来的.
      

  4.   

    你可以这样读:
    Stream objStream=objResponse.GetResponseStream();
    StreamReader objReader=new StreamReader(objStream,System.Text.Encoding.Default);
    string strFileCode=objReader.ReadToEnd();