简单的 Rapidshare 下载类 C#






4.09/5 (15投票s)
使用这个类,您可以一次简单地下载多个 Rapidshare 链接!(并行)

引言
通过使用 Rapidshare 下载类,您可以同时下载多个文件(并行),并控制过程的每个步骤。
规格
- 支持和控制用户身份验证
- 搜索已认证的 Cookie
- 基于用户帐户创建凭据
- 基于事件的下载类
onChanged
onError
onComplete
onAllComplete
onProgressChanged
- 每个线程在每个步骤的的状态和进度报告
- 支持安全下载取消
- 支持 ASP.NET
- 支持其他服务,如 (Megaupload, Hotfile,...)
背景
我撰写这篇文章的原因之一是,在某些情况下,您需要使用 HttpWebRequest
来授权用户访问 Rapidshare.com 等服务,然后再下载文件。 在这些情况下,如果您尝试使用 .NET 网络凭据,您会发现未生成 Authorization HTTP 标头,因此您无法授权您的用户。
因此,我撰写这篇文章是为了向您展示如何使用 C# 下载安全文件。
类图

使用方法
为这篇文章提供的源代码是在 Windows 窗体环境中下载器类的示例用法。 您也可以通过在页面上启用 "Async
" 在 ASP.NET 中使用此类。
<%@ Page Language="C#" AutoEventWireup="true" Async="true"
private QDownloadParam QDownload(QDownloadParam param)
{
//Create the HttpWebRequest Object
param.Status = "Connecting... (" + param.Link + ")";
param.Worker.ReportProgress(1, param);
var URL = new Uri(param.Link.Trim());
var req = (HttpWebRequest)WebRequest.Create(URL);
//Allow Auto Redirections, because after Authentications,
//links are going to be changed.
req.AllowAutoRedirect = true;
//check if the user set the Credentials or not
if (!string.IsNullOrEmpty(param.Username))
{
//Assigning Basic Authorization HTTP Header to HttpWebRequest
byte[] authBytes = Encoding.UTF8.GetBytes
((param.Username + ":" + param.Password).ToCharArray());
req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);
}
req.MaximumAutomaticRedirections = 4;
//User Agent Set as Firefox
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
//Accept any kind of files!
req.Accept = "*/*";
req.KeepAlive = true;
req.Timeout = 9999999; //prefRequestTimeout
//Search for the Authentication cookies
req.CookieContainer = GetUriCookieContainer(URL.AbsoluteUri);
// Get the stream from the returned web response
HttpWebResponse webresponse = null;
try
{
webresponse = (HttpWebResponse)req.GetResponse();
//Check if the Response Redirect occurred or not
if (webresponse.ResponseUri.AbsoluteUri != URL.AbsoluteUri)
{
param.Link = webresponse.ResponseUri.AbsoluteUri;
param.Status = "Redirecting... (" + param.Link + ")";
param.Worker.ReportProgress(1, param);
QDownload(param); return param;
}
param.Status = "Connected. (" + param.Link + ")";
param.Worker.ReportProgress(1, param);
}
catch (WebException e)
{
param.Status = "error: " + e;
param.Worker.ReportProgress(-1, param);
return param;
}
try
{
//Get the File and write it to path
var file = Path.GetFileName(webresponse.ResponseUri.AbsoluteUri);
if (!webresponse.ContentType.Contains("text/html"))
{
file = file.Replace(".html", "");
}
else
{
param.Status = "Check the Username or Password!";
param.Worker.ReportProgress(-1, param);
if (SkipOnError)
{
param.Status = "Download Skip. " + param.Link;
return param;
}
}
param.FileName = file;
param.Status = "Downloading File: " + file +
"(" + (webresponse.ContentLength / 1000) + "KB)";
param.Worker.ReportProgress(1, param);
string filepath;
if (param.FileDirectory.EndsWith("\\"))
filepath = param.FileDirectory + file;
else
{
filepath = param.FileDirectory + "\\" + file;
}
var writeStream = new FileStream
(filepath, FileMode.Create, FileAccess.ReadWrite);
var readStream = webresponse.GetResponseStream();
try
{
const int Length = 256;//Set the buffer Length
var buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
if (param.Worker.CancellationPending) return param;
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
int Percent = (int)((writeStream.Length * 100) /
webresponse.ContentLength);
param.Progress = Percent;
param.Worker.ReportProgress(2, param);
}
readStream.Close();
writeStream.Close();
}
catch (Exception exception)
{
param.Status = "error: " + exception;
param.Worker.ReportProgress(-1, param);
return param;
}
param.Status = "Download Finish: " + file;
param.Worker.ReportProgress(1, param);
}
catch (Exception exception)
{
param.Status = "error: " + exception;
param.Worker.ReportProgress(-1, param);
return param;
}
return param;
}
字段
FileDirectory
:下载的文件保存到的文件夹的精确路径UserName
:您的帐户 ID(用户名)Password
:您的帐户密码Links
:您想要下载的 Rapidshare 链接列表Jobs
:包含所有下载作业对象OnlineJobs
:包含当前正在运行的下载作业数量
方法
void StartAll()
:以并行模式开始下载链接列表中的所有链接int CancelAll()
:取消所有下载
事件
onChanged
:当每个下载作业的状态发生变化时,发送当前状态onError
:发送包含详细信息的每个错误消息oncompleted
:在每个下载作业完成时引发onAllCompleted
:在所有下载作业完成时引发onProgressChanged
:当下载进度百分比发生变化时,发送每个下载进度百分比
兴趣点
这个项目的有趣之处在于,您可以使用上传和下载功能来构建一个文件服务,该服务可以在不同的服务器和服务上工作,例如 Rapidshare、Megaupload、Windows Live Storage 等。
历史
版本 1.0