65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C# 将文件简单上传到 Rapidshare 账户的方法

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.65/5 (11投票s)

2009年4月2日

GPL3
viewsIcon

96507

downloadIcon

982

使用此 C# (.NET 2.0) 方法,您可以轻松将文件上传到 Rapidshare.com 帐户。

引言

通过使用这个类,您可以轻松地将文件上传到 Rapidshare.com。

使用 RapidShare,您可以轻松且安全地发送大文件。

这个类支持以下类型的 Rapidshare 帐户:

  • 高级帐户 (type = 1)
  • 收藏家帐户 (type = 2)
  • 免费用户 (type = 0)

选择文件并上传后,您应该在结果面板中看到两个链接,一个用于下载,另一个用于从 Rapidshare.com 删除文件。

Using the Code

您可以在演示应用程序的源代码中查看该类的完整用法。

对于这个应用程序,我使用的是 Rapidshare 版本 1 API(原始 API 是用 Perl 编写的)。

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 = "----------" + 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("\r\n--" + s1 + "\r\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("--");
            stringBuilder.Append(boundary);
            stringBuilder.Append("\r\n");
            stringBuilder.Append("Content-Disposition: form-data; name=\"toolmode2\"");
            stringBuilder.Append("\r\n");
            stringBuilder.Append("\r\n");
            stringBuilder.Append("1");
            stringBuilder.Append("\r\n");
            stringBuilder.Append(boundary);
            stringBuilder.Append("\r\n");
            if (AccountType != 0)//Free User
            {
                if (AccountType == 1) //Premium Account
                {
                    stringBuilder.Append(
                        "Content-Disposition: form-data; name=\"login\"");
                }
                else //Collector Account
                {
                    stringBuilder.Append(
                        "Content-Disposition: form-data; name=\"freeaccountid\"");
                }
                stringBuilder.Append("\r\n");
                stringBuilder.Append("\r\n");
                stringBuilder.Append(username);
                stringBuilder.Append("\r\n");
                stringBuilder.Append(boundary);
                stringBuilder.Append("\r\n");
                stringBuilder.Append
		("Content-Disposition: form-data; name=\"password\"");
                stringBuilder.Append("\r\n");
                stringBuilder.Append("\r\n");
                stringBuilder.Append(password);
                stringBuilder.Append("\r\n");
            }//else if Free User
//File Name
            stringBuilder.Append(boundary);
            stringBuilder.Append("\r\n");
            stringBuilder.Append("Content-Disposition: form-data; name=\"");
            stringBuilder.Append("filecontent");
            stringBuilder.Append("\"; filename=\"");
            stringBuilder.Append(FName);
            stringBuilder.Append("\"");
            stringBuilder.Append("\r\n");
//File Type
            stringBuilder.Append("Content-Type: ");
            stringBuilder.Append("multipart/form-data");
            stringBuilder.Append("\r\n");
            stringBuilder.Append("Content-Transfer-Encoding: ");
            stringBuilder.Append("binary");
            stringBuilder.Append("\r\n");
            stringBuilder.Append("\r\n");
            return stringBuilder.ToString();
        }

        private CookieContainer _cockies = new CookieContainer();
        private HttpWebRequest GetWebrequest(string boundary)
        {//Prepare for Uploading 
            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());
            //Find Free Upload Slot on Rapidshare servers.
            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;//Set Cookies for rapidshare
            httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            //Set Fake userAgent exactly like Rapidshare Manager
            httpWebRequest.UserAgent = "RapidUploader[v1,2]";
            //Set Fake Referer
            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;
        }
    }
//You can use this class in .NET Web, Window, WebService,...

在下一个版本中,我计划添加上传速度控制和进度条。

您可以将高达 2000 MB 的文件上传到您的高级帐户,而收藏家帐户和免费用户可以上传高达 200 MB 的文件。

关注点

您也可以将此方法用于您的 Web 应用程序,并动态地将文件上传到您的 Rapidshare.com 帐户。

历史

  • 2009 年 4 月 2 日:版本 1.0
© . All rights reserved.