AddRange 方法向该请求添加一个字节范围标头。如果 range 为正,则该范围从数据的开始处到 range。如果 range 为负,则该范围从 range 到数据的结束处。
你的意思是取(int)lStartPos的负数么?
HttpWebRequestAddRange 方法向该请求添加一个字节范围标头。 如果 range 为正值,则 range 参数将指定范围的起点。 服务器应该开始发送从指定的 range 参数到 HTTP 实体中数据的末尾之间的数据。 如果 range 为负值,则 range 参数将指定范围的终点。 服务器应该开始发送从 HTTP 实体中数据的开头到指定的 range 参数之间的数据。 应该取正数,没错啊,调试的时候监视lStartPos的值也是对的啊
以上摘自msdn,具体是对是错,你不会试试吗?
嗯,下面是代码using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Windows.Forms; using System.Data;namespace downloaderN { class classDownloader { private string strUrl; private string strSavePath = "d:\\aa.txt"; private long lContentLength; private long lStartPosition; //string strProgress; private FileStream fsDownload; private FileStream fsProgress; private Stream ns; private System.Net.HttpWebRequest request; private System.Net.WebRequest req; public string URL { get { return strUrl; } set { strUrl = value; } } public void StartDownload() { //打开网络连接 try { req = System.Net.HttpWebRequest.Create(strUrl); req.Method = "HEAD"; request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strUrl); System.Net.WebResponse resp = request.GetResponse(); long.TryParse(resp.Headers.Get("Content-Length"), out lContentLength); //判断文件是否已经续传或下载好了 if (File.Exists(strSavePath)) { fsDownload = File.OpenWrite(strSavePath); fsProgress = File.OpenRead(strSavePath + ".prg"); byte[] tempbytes=new byte [sizeof (long)]; fsProgress.Read(tempbytes, 0, sizeof (long)); lStartPosition = BitConverter .ToInt64 ( tempbytes,0) ; fsProgress.Close(); if (lStartPosition >= lContentLength) { MessageBox.Show("已经下好了"); return; } else { fsDownload.Seek(lStartPosition, SeekOrigin.Current); int temprange = (int)lStartPosition; MessageBox.Show(temprange .ToString ()); request.AddRange(temprange);//将下载的位置放在断点处 } } else { fsDownload = new System.IO.FileStream(strSavePath, FileMode.Create); lStartPosition = 0; fsProgress = new System.IO.FileStream(strSavePath + ".prg", FileMode.Create); fsProgress.Close(); } //向服务器请求,获得回应数据流 ns = request.GetResponse().GetResponseStream(); byte[] nbytes = new byte[512]; int nReadSize = 0; nReadSize = ns.Read(nbytes, 0, 512); fsProgress = File.OpenWrite(strSavePath + ".prg"); while (nReadSize > 0) { fsDownload.Write(nbytes, 0, nReadSize); lStartPosition += nReadSize;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Data;namespace downloaderN
{
class classDownloader
{
private string strUrl;
private string strSavePath = "d:\\aa.txt";
private long lContentLength;
private long lStartPosition;
//string strProgress;
private FileStream fsDownload;
private FileStream fsProgress;
private Stream ns;
private System.Net.HttpWebRequest request;
private System.Net.WebRequest req;
public string URL
{
get
{
return strUrl;
}
set
{
strUrl = value;
}
}
public void StartDownload()
{
//打开网络连接
try
{
req = System.Net.HttpWebRequest.Create(strUrl);
req.Method = "HEAD";
request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strUrl);
System.Net.WebResponse resp = request.GetResponse();
long.TryParse(resp.Headers.Get("Content-Length"), out lContentLength);
//判断文件是否已经续传或下载好了
if (File.Exists(strSavePath))
{
fsDownload = File.OpenWrite(strSavePath);
fsProgress = File.OpenRead(strSavePath + ".prg");
byte[] tempbytes=new byte [sizeof (long)];
fsProgress.Read(tempbytes, 0, sizeof (long));
lStartPosition = BitConverter .ToInt64 ( tempbytes,0) ;
fsProgress.Close();
if (lStartPosition >= lContentLength)
{
MessageBox.Show("已经下好了");
return;
}
else
{
fsDownload.Seek(lStartPosition, SeekOrigin.Current);
int temprange = (int)lStartPosition;
MessageBox.Show(temprange .ToString ());
request.AddRange(temprange);//将下载的位置放在断点处
}
}
else
{
fsDownload = new System.IO.FileStream(strSavePath, FileMode.Create);
lStartPosition = 0;
fsProgress = new System.IO.FileStream(strSavePath + ".prg", FileMode.Create);
fsProgress.Close();
}
//向服务器请求,获得回应数据流
ns = request.GetResponse().GetResponseStream();
byte[] nbytes = new byte[512];
int nReadSize = 0;
nReadSize = ns.Read(nbytes, 0, 512);
fsProgress = File.OpenWrite(strSavePath + ".prg");
while (nReadSize > 0)
{
fsDownload.Write(nbytes, 0, nReadSize);
lStartPosition += nReadSize;
//往记录文件里面写入当前进度
fsProgress.Position = 0;
fsProgress.Write(BitConverter .GetBytes (lStartPosition ),0,sizeof (long));
//待会儿加上进度条
string aaa = (lStartPosition.ToString() + "//" + lContentLength.ToString());
// Program.form1.label2.Text = aaa;
nReadSize = ns.Read(nbytes, 0, 512);
}
MessageBox.Show("下载完成");
fsDownload.Close();
fsProgress.Close();
File.Delete(strSavePath + ".prg");
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
fsDownload.Close();
fsProgress.Close();
ns.Close();
}
}
}
}