首页 / C# / C# 使用HTTP下载文件,支持续传
C# 使用HTTP下载文件,支持续传
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# 使用HTTP下载文件,支持续传,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3400字,纯文字阅读大概需要5分钟。
内容图文
![C# 使用HTTP下载文件,支持续传](/upload/InfoBanner/zyjiaocheng/1195/05b9d55c20754fd292ffa9b1a0a597df.jpg)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace Flames.Utils.Net
{
public class Http
{
/// <summary>
/// 下载进度
/// </summary>
public event Action<int> ShowDownloadPercent;
/// <summary>
/// 一次下载量
/// </summary>
private int ByteSize = 1000000;
/// <summary>
/// 下载中的后缀,下载完成去掉
/// </summary>
private const string Suffix = ".downloading";
/// <summary>
/// Http方式下载文件
/// </summary>
/// <param name="url">http地址</param>
/// <param name="localfile">本地文件</param>
/// <returns></returns>
public int DownloadFile(string url, string localfile)
{
int ret = 0;
string localfileReal = localfile;
string localfileWithSuffix = localfileReal + Suffix;
try
{
long startPosition = 0;
FileStream writeStream = null;
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(localfileReal)) return 1;
if (File.Exists(localfileReal)) return 0;
//取得远程文件长度
long remoteFileLength = GetHttpLength(url);
if (remoteFileLength == 0) return 2;
//判断要下载的文件是否存在
if (File.Exists(localfileWithSuffix))
{
writeStream = File.OpenWrite(localfileWithSuffix);
startPosition = writeStream.Length;
if (startPosition > remoteFileLength)
{
writeStream.Close();
File.Delete(localfileWithSuffix);
writeStream = new FileStream(localfileWithSuffix, FileMode.Create);
}
else if (startPosition == remoteFileLength)
{
DownloadFileOk(localfileReal, localfileWithSuffix);
writeStream.Close();
return 0;
}
else
{
writeStream.Seek(startPosition, SeekOrigin.Begin);
}
}
else
{
writeStream = new FileStream(localfileWithSuffix, FileMode.Create);
}
HttpWebRequest req = null;
HttpWebResponse rsp = null;
try
{
req = (HttpWebRequest)HttpWebRequest.Create(url);
if (startPosition > 0) req.AddRange((int)startPosition);
rsp = (HttpWebResponse)req.GetResponse();
using (Stream readStream = rsp.GetResponseStream())
{
byte[] btArray = new byte[ByteSize];
long currPostion = startPosition;
int contentSize = 0;
while ((contentSize = readStream.Read(btArray, 0, btArray.Length)) > 0)
{
writeStream.Write(btArray, 0, contentSize);
currPostion += contentSize;
ShowDownloadPercent?.Invoke((int)(currPostion * 100 / remoteFileLength));
}
}
}
catch (Exception ex)
{
ret = 3;
}
finally
{
if (writeStream != null)
writeStream.Close();
if (rsp != null)
rsp.Close();
if (req != null)
req.Abort();
if (ret == 0)
DownloadFileOk(localfileReal, localfileWithSuffix);
}
}
catch (Exception ex)
{
//Log.Info("获取远程文件失败!exception:\n" + ex.ToString());
ret = 4;
}
return ret;
}
/// <summary>
/// 下载完成
/// </summary>
private void DownloadFileOk(string localfileReal, string localfileWithSuffix)
{
try
{
//去掉.downloading后缀
FileInfo fi = new FileInfo(localfileWithSuffix);
fi.MoveTo(localfileReal);
}
catch (Exception ex)
{
}
finally
{
//通知完成
if (ShowDownloadPercent != null)
ShowDownloadPercent(100);
}
}
// 从文件头得到远程文件的长度
private long GetHttpLength(string url)
{
long length = 0;
HttpWebRequest req = null;
HttpWebResponse rsp = null;
try
{
req = (HttpWebRequest)HttpWebRequest.Create(url);
rsp = (HttpWebResponse)req.GetResponse();
if (rsp.StatusCode == HttpStatusCode.OK)
length = rsp.ContentLength;
}
catch (Exception ex)
{
}
finally
{
if (rsp != null)
rsp.Close();
if (req != null)
req.Abort();
}
return length;
}
}
}
原文:https://www.cnblogs.com/flamegreat/p/14620733.html
内容总结
以上是互联网集市为您收集整理的C# 使用HTTP下载文件,支持续传全部内容,希望文章能够帮你解决C# 使用HTTP下载文件,支持续传所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。