c# Http下载

1.首先是服务器上发布资源,如果资源时自定义格式,比如 .zidingyi结尾的后缀文件,需要在MIME类型上添加处理方式

需要注意的是.net里面要设置

System.Net.ServicePointManager.DefaultConnectionLimit = 50;

2.客户端采用WebRequest进行请求,WebRequest处理响应   

/// <summary>
/// Http协议下载助手
/// </summary>
public class HttpDownLoadHelper
{
#region 变量,属性
private HttpWebRequest request;
private HttpWebResponse response;
public string SavePath { get; set; }
public string URL { get; set; }
public string State { get; set; }
private int iReConnectTimes = 0;//重连次数
public long DownLoadedSize { get; set; }//已下载大小
private bool IFAbortExec = true;//是否异常中断
FileStream fs;
Stream reader;
#endregion

#region 构造函数
public HttpDownLoadHelper()
{
}

public HttpDownLoadHelper(string vsServerFilePath, string vsSavePath)
{
//request = (HttpWebRequest)HttpWebRequest.Create(new Uri(vsServerFilePath));
this.URL = vsServerFilePath;
SavePath = vsSavePath;
}
#endregion

#region 方法
/// <summary>
/// 读取已下载文件大小
/// </summary>
public void Init()
{
if (File.Exists(SavePath))//断点续传
{
fs = File.OpenWrite(SavePath);
DownLoadedSize = fs.Length;
fs.Close();
}
}

public void DownLoadFile(string vsServerFilePath, string vsSavePath)
{
URL = vsServerFilePath;
SavePath = vsSavePath;
DownLoadFile();
}

public void DownLoadFile()
{
try
{
request = (HttpWebRequest)HttpWebRequest.Create(new Uri(URL));
request.KeepAlive = true;
request.Timeout = 1000 * 10;

request.ReadWriteTimeout = 1000 * 20;//不知道为啥,流read的时候会超时
long startPos = 0;
if (File.Exists(SavePath))//断点续传
{
fs = File.OpenWrite(SavePath);
if (fs.Length > 0)//对于已下载的索引文件,重新下载最后一个字节,否则AddRange会出错,因为文件大小获取是总的
{
fs.Seek(fs.Length - 1, SeekOrigin.Current);
startPos = fs.Length - 1;
}
else
{
fs.Seek(fs.Length, SeekOrigin.Current);
startPos = fs.Length;
}
}
else//全新下载
{
string vsDir = Path.GetDirectoryName(SavePath);
if (!Directory.Exists(vsDir))
{
Directory.CreateDirectory(vsDir);
}
fs = new FileStream(SavePath, FileMode.Create);
startPos = 0;
}
DownLoadedSize = startPos;
if (!string.IsNullOrEmpty(State) && State.ToUpper() == "PAUSED")
{
fs.Close();
return;
}
if (startPos > 0)
{
request.AddRange(startPos);
}
byte[] buffer = new byte[1024 * 32];
response = (HttpWebResponse)request.GetResponse();
reader = response.GetResponseStream();
int iRead = reader.Read(buffer, 0, buffer.Length);
DownLoadedSize += iRead;
while (iRead > 0)
{
fs.Write(buffer, 0, iRead);
iRead = reader.Read(buffer, 0, buffer.Length);
DownLoadedSize += iRead;
}
fs.Close();
reader.Close();

if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}

}
catch (Exception ex)
{
if (IFAbortExec)
{
Logger.LogError(ex);
}
//断线重连
++iReConnectTimes;
if (iReConnectTimes > 5 || !IFAbortExec)//清理资源
{
fs.Close();
if (reader != null)
{
reader.Close();
}

if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
}
else if (IFAbortExec)//异常中断才可重连,先清理资源
{

fs.Close();
if (reader != null)
{
reader.Close();
}

if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
DownLoadFile();
}


}
}

public void GetDownLoadedSize()
{
long startPos = 0;
if (File.Exists(SavePath))//断点续传
{
fs = File.OpenWrite(SavePath);
fs.Seek(fs.Length, SeekOrigin.Current);
startPos = fs.Length;
}
else//全新下载
{
fs = new FileStream(SavePath, FileMode.Create);
startPos = 0;
}
DownLoadedSize = startPos;
}

public void Abort()
{
try
{
IFAbortExec = false;//非异常中断
if (request != null)
{
request.Abort();
}
if (response != null)
{
response.Close();
}
if (fs != null)
{
fs.Close();
}
if (reader != null)
{
reader.Close();
}

}
catch (Exception ex)
{
Logger.LogError(ex);
}
}

public void Cancel()
{

Abort();
//删除文件
try
{
if (File.Exists(SavePath))
{
File.Delete(SavePath);
}
}
catch (Exception ex)
{
Logger.LogError(ex);
}

}
#endregion
}

 

posted @ 2014-03-20 17:03  大侠的哥哥是菜鸟  阅读(583)  评论(0编辑  收藏  举报