在本文中,将探讨如何使用C#编程语言将文件上传到Rapidshare.com。Rapidshare是一个流行的文件托管服务,允许用户上传和分享大文件。将介绍不同的账户类型,包括付费账户、收藏家账户和免费用户,并展示如何使用C#实现文件上传功能。
Rapidshare支持三种类型的账户:
每种账户类型都有其特定的上传限制和功能。例如,付费账户可以上传高达2000MB的文件,而收藏家账户和免费用户则分别可以上传高达200MB的文件。
上传文件到Rapidshare.com的过程相对简单。首先,需要选择要上传的文件,然后使用相应的API进行上传。上传完成后,将看到两个链接:一个用于下载文件,另一个用于从Rapidshare.com删除文件。
下面是一个C#类的示例,展示了如何使用Rapidshare的API上传文件。这个类支持上述提到的所有账户类型。
public class QRapidshare
{
public string QUploadToRapidshare(string FilePath, string username, string password, int AccountType)
{
FileSystemInfo _file = new FileInfo(FilePath);
DateTime dateTime2 = DateTime.Now;
long l2 = dateTime2.Ticks;
string s1 = "\n----------" + l2.ToString("x");
System.Net.HttpWebRequest httpWebRequest = GetWebrequest(s1);
using (System.IO.FileStream fileStream = new FileStream(_file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Set Headers for Uploading
byte[] bArr1 = Encoding.ASCII.GetBytes("\n--" + s1 + "\n");
string s2 = GetRequestMessage(s1, _file.Name, username, password, AccountType);
byte[] bArr2 = Encoding.UTF8.GetBytes(s2);
Stream memStream = new MemoryStream();
memStream.Write(bArr1, 0, bArr1.Length);
memStream.Write(bArr2, 0, bArr2.Length);
byte[] buffer = new byte[1024];
int bytesRead = 0;
// Read File into memStream.
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
httpWebRequest.ContentLength = memStream.Length;
fileStream.Close();
Stream requestStream = httpWebRequest.GetRequestStream();
// Send File from memStream to Rapidshare.com
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
}
string tm = "";
using (Stream stream = httpWebRequest.GetResponse().GetResponseStream())
using (StreamReader streamReader = new StreamReader(stream))
{
tm = streamReader.ReadToEnd();
}
// Get Response from Rapidshare and Return the Links.
return tm;
}
private string GetRequestMessage(string boundary, string FName, string username, string password, int AccountType)
{
// Generate Headers exactly Like Rapidshare API v.1.0
System.Text.StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("\n--");
stringBuilder.Append(boundary);
stringBuilder.Append("\n");
stringBuilder.Append("Content-Disposition: form-data; name=\"toolmode2\"");
stringBuilder.Append("\n");
stringBuilder.Append("\n");
stringBuilder.Append("1");
stringBuilder.Append("\n");
stringBuilder.Append(boundary);
stringBuilder.Append("\n");
if (AccountType != 0)
{
if (AccountType == 1)
{
stringBuilder.Append("Content-Disposition: form-data; name=\"login\"");
}
else
{
stringBuilder.Append("Content-Disposition: form-data; name=\"freeaccountid\"");
}
stringBuilder.Append("\n");
stringBuilder.Append("\n");
stringBuilder.Append(username);
stringBuilder.Append("\n");
stringBuilder.Append(boundary);
stringBuilder.Append("\n");
stringBuilder.Append("Content-Disposition: form-data; name=\"password\"");
stringBuilder.Append("\n");
stringBuilder.Append("\n");
stringBuilder.Append(password);
stringBuilder.Append("\n");
}
else
{
stringBuilder.Append(boundary);
stringBuilder.Append("\n");
stringBuilder.Append("Content-Disposition: form-data; name=\"filecontent\"; filename=\"" + FName + "\"");
stringBuilder.Append("\n");
stringBuilder.Append("Content-Type: multipart/form-data");
stringBuilder.Append("\n");
stringBuilder.Append("Content-Transfer-Encoding: binary");
stringBuilder.Append("\n");
stringBuilder.Append("\n");
}
return stringBuilder.ToString();
}
private CookieContainer _cockies = new CookieContainer();
private HttpWebRequest GetWebrequest(string boundary)
{
WebClient wc = new WebClient();
Uri url0 = new Uri("http://rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver_v1");
int uploadserver = int.Parse(wc.DownloadString(url0).Trim());
System.Uri uri = new Uri("http://rs" + uploadserver + "l3.rapidshare.com/cgi-bin/upload.cgi");
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
httpWebRequest.CookieContainer = _cockies;
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest.UserAgent = "RapidUploader[v1,2]";
httpWebRequest.Referer = "http://rapidshare.com/";
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = true;
httpWebRequest.Timeout = -1;
httpWebRequest.Headers.Add("Accept-Charset", "iSO-8859-1,utf-8;q=0.7,*;q=0.7");
httpWebRequest.Headers.Add("Accept-Encoding", "identity");
httpWebRequest.Headers.Add("Accept-Language", "de-de;q=0.5,en;q=0.3");
return httpWebRequest;
}
}
这个类可以在.NET Web、Windows、WebService等应用程序中使用。在下一个版本中,计划添加上传速度控制和进度条功能。