Rapidshare.com Simple Upload File Class – C#
Download
Introduction
By using this class, you can easily upload your files to Rapidshare.com.
With RapidShare, you can send big files easily and in a secure manner.
This class supports the following type of Rapidshare accounts:
- Premium accounts (type = 1)
- Collector's accounts (type = 2)
- Free users (type = 0)
After Selecting the file and uploading, you should see two links in the result Panel, one for downloading and one for deleting the file from Rapidshare.com.
Using the Code
You can see the full usage of this class in the source of the demo application.
For this application, I'm using Rapidshare version 1 API (the original API is made by Perl).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 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,... |
In the next version, I want to add an Upload Speed Control and Progress Bar.
You can upload up to 2000 MB file to your Premium account, and up to 200 MB for Collectors account and Free users.
Points of Interest
You can also use this method for your web applications and dynamically upload your files to your Rapidshare.com account.