请问如何用C#中的webrequest,webresponse实现断点续传

Tonny2002 2002-11-23 05:17:45
我用webrequest,webresponse写了一个下载程序,但我无法实现断点续传
因为webresponse.GetResponseStream()返回的stream的canseek为false
stream的position和length不可用该如何解决
...全文
410 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
snewxf 2002-11-26
  • 打赏
  • 举报
回复
fileStream = new FileStream(Path,FileMode.Append,FileAccess.Write);
inStream = null;
try
{

HttpWebRequest myre=(HttpWebRequest)WebRequest.Create(URL);

if(fileStream.Length == myre.ContentLength)
{
MessageBox.Show("你以完成下载该程序了","ok");
return;
}

myre.AddRange(Convert.ToInt32(fileStream.Length));//接上次下载的字节开始下载文件
HttpWebResponse response = (HttpWebResponse)myre.GetResponse();
inStream =response .GetResponseStream();
this.progressBar1.Maximum = total;
this.progressBar1.Minimum =0;
int length = 1024;
byte[] buffer = new byte[1025];
int readerLength = 0,currentLength=0;
while((readerLength =inStream.Read(buffer,0,length))> 0)
{
currentLength += readerLength ;
fileStream.Write(buffer,0,readerLength);
this.progressBar1.Value =currentLength + countByte;
}
fileStream.Close();
inStream.Close();
File.Delete(Application.StartupPath +@"\FileLength.txt");
MessageBox.Show("down 成功","ok");

socket没玩过!好运吧!!!!!!!!
Tonny2002 2002-11-25
  • 打赏
  • 举报
回复
但是我如果用socket如何实现?
yarshray 2002-11-23
  • 打赏
  • 举报
回复
断点续传首先要服务器支持

通常是用socket编写

断点续传和多线程下载 (2002年第3期)
http://www.csdn.net/magazine/download.shtm

110,536

社区成员

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

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

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