下载程序中碰到困难!难!难!难!难!大侠们赶快进阿!!!!!

DataBind 2005-02-28 03:43:48
我在写一个下载的程序,我发现很多的网站Content-Length = -1 ,用Read的Count==0来判断返回流结束的,而事实上如果网比较慢,即使流没有结束 Read的Count也经常为0。而且还没有异常发生。

我有什么办法吗?

以下是我的代码,请审阅!谢谢先!


public static int BUFFER_SIZE = 32768;
public static string GetURLContentString(string url)
{
//StringBuilder buffer = new StringBuilder();
byte[] buffer = null;
try
{

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

httpWebRequest.KeepAlive = false;

HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

Stream streamResponse=httpWebResponse.GetResponseStream();
BinaryReader binaryReader = new BinaryReader(streamResponse);

buffer = null;

byte[] blockBuffer = binaryReader.ReadBytes(BUFFER_SIZE);
while (blockBuffer.Length > 0)
{

if(buffer == null)
{
buffer = new byte[blockBuffer.Length];
blockBuffer.CopyTo(buffer,0);
}
else
{
byte[] bufferTmp = new byte[blockBuffer.Length + buffer.Length];
buffer.CopyTo(bufferTmp,0);
Array.Copy(blockBuffer,0,bufferTmp,buffer.Length - 1, blockBuffer.Length);
buffer = bufferTmp;
}
if(blockBuffer.Length < BUFFER_SIZE)
break;
blockBuffer = binaryReader.ReadBytes(BUFFER_SIZE);
}
binaryReader.Close();

streamResponse.Close();

httpWebResponse.Close();

return Encoding.GetEncoding("GB2312").GetString(buffer, 0, buffer.Length);

}
catch(ArgumentException e)
{
Console.WriteLine("\nThe second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close'");
Console.WriteLine("\n{0}",e.Message);
}
catch(WebException e)
{
Console.WriteLine("WebException raised!");
Console.WriteLine("\n{0}",e.Message);
Console.WriteLine("\n{0}",e.Status);
}
catch(Exception e)
{
Console.WriteLine("Exception raised!");
Console.WriteLine("Source :{0} " , e.Source);
Console.WriteLine("Message :{0} " , e.Message);
}

return buffer.ToString();
}

...全文
184 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
jyu_730 2005-03-01
  • 打赏
  • 举报
回复
帮你up一下
cdo 2005-03-01
  • 打赏
  • 举报
回复
帮你顶一下。
白掌柜 2005-03-01
  • 打赏
  • 举报
回复
up
3gold 2005-03-01
  • 打赏
  • 举报
回复
帮顶
web_gus 2005-03-01
  • 打赏
  • 举报
回复
up
jialiang 2005-03-01
  • 打赏
  • 举报
回复
只能帮顶了
DataBind 2005-03-01
  • 打赏
  • 举报
回复
可我的确实是下不全(不是总下不全,概率是50%,一会可以,一会就下不完),还有其他的方式来实现吗?
yellowhwb 2005-03-01
  • 打赏
  • 举报
回复
你可以这样读:
Stream objStream=objResponse.GetResponseStream();
StreamReader objReader=new StreamReader(objStream,System.Text.Encoding.Default);
string strFileCode=objReader.ReadToEnd();
yellowhwb 2005-03-01
  • 打赏
  • 举报
回复
[MSDN]
ContentLength 属性包含与响应一起返回的 Content-Length 标头的值。如果响应中未设置 Content-Length 标头,则将 ContentLength 设置为值 -1。

可能是这个原因!
但是文件是能都出来的.
DataBind 2005-03-01
  • 打赏
  • 举报
回复
我用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=1

HTTP/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
DataBind 2005-03-01
  • 打赏
  • 举报
回复
我上面写错了,长度是-1.
DataBind 2005-03-01
  • 打赏
  • 举报
回复
可是,对于sina的网站http://news.sina.com.cn/old1000/news1000_20050204.shtml,无论怎么读,长度都是0 ,还有什么办法吗?
yellowhwb 2005-02-28
  • 打赏
  • 举报
回复
Content-Length = -1一般是由于请求超时,可能是Internet资源不存在或是网速慢,断掉了,你可以再多发几个请求试试!如果读了一半断了,你可以用断点续传的代码!

while (blockBuffer.Length > 0)//这样判断是不对的,因为有时候读取的就是0。你应该把所以读过的字节全都加起来和Content-Length比较!
int iFileLength=0;
while (iFileLength<Content-Length)
{
iFileLength+=blockBuffer.Length;
}
wxqq2001 2005-02-28
  • 打赏
  • 举报
回复
帮你顶
zl2006 2005-02-28
  • 打赏
  • 举报
回复
顶,你到本地测试一下看能否行
jialiang 2005-02-28
  • 打赏
  • 举报
回复
up

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧