Jump to content

add-file difficulty


Recommended Posts

Hi There,

i'm using the code below to post a file to the uTorrent WebUI. I get back an error of "The torrent is not valid bencoding". I know the original torrent file is OK (opens in uTorrent when added), and using another page that accepts file uploads (eg. PHP page) and writing the post contents to a file from the PHP page yields the exact same result as the original file, so i do not believe its an uploading problem.

I'm setting the torrent_file parameter as the filename of the file, and uploading the torrent content as file data. Is this what uTorrent API is expecting, or have I misunderstood the parameter?

w = WebRequest.Create("http://" & My.Settings.TorrentServer & ":" & My.Settings.TorrentServerPort & "/gui/?action=add-file&token=" & token)
w.Credentials = credentials
w.CookieContainer = New CookieContainer()
Dim uf() As UploadFile
uf = New UploadFile() {New UploadFile(filename)}
Dim params As New Specialized.NameValueCollection()
params.Add("torrent_file", filename)
Using r As HttpWebResponse = HttpUploadHelper.Upload(w, uf, params)
Console.WriteLine(New StreamReader(r.GetResponseStream()).ReadToEnd())
End Using

Link to comment
Share on other sites

This is a chunk of C# that works against uTorrent 3.1.2:

/// <param name="fileName">The name of the torrent file</param>
/// <param name="fileData">The torrent data to send</param>
/// <param name="fileDataBytes">The number of bytes in fileData</param>
public void AddByFile(string fileName, byte[] fileData, int fileDataBytes)
// Build targetUri
// Retrieve credentials from cache
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

HttpWebRequest postFileRequest = (HttpWebRequest)HttpWebRequest.Create(targetUri);
postFileRequest.ContentType = "multipart/form-data; boundary=" + boundary;
postFileRequest.Method = "POST";
postFileRequest.KeepAlive = true;
postFileRequest.Credentials = utorrentCredentials;
postFileRequest.Headers.Add("Cookie", CookieManagerMessageInspector.SharedCookie); // a GUID
postFileRequest.PreAuthenticate = true;
postFileRequest.ServicePoint.Expect100Continue = false;

Stream dataToSend = postFileRequest.GetRequestStream();

byte[] boundarybytes = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
dataToSend.Write(boundarybytes, 0, boundarybytes.Length);

byte[] headerbytes = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"torrent_file\"; filename=\"{0}\"\r\nContent-Type: application/x-bittorrent\r\n\r\n", fileName));
dataToSend.Write(headerbytes, 0, headerbytes.Length);

dataToSend.Write(fileData, 0, fileDataBytes);

byte[] trailer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
dataToSend.Write(trailer, 0, trailer.Length);

HttpWebResponse response = (HttpWebResponse)postFileRequest.GetResponse();

There's plenty of online converters to VB around, if you need them. I spent a while in Fiddler comparing the WebUI output to my own, but got there eventually.

Link to comment
Share on other sites

  • 11 months later...

Hi I tried this code. It doesnt seem to work. It just hangs. I don't know what utorrentCredentials & what CookieManagerMessageInspectector.SharedCookie to put in ?

Private Sub Test

Dim filename As String, fileData As Byte(), fileDataBytes As Integer

webUiUrl = ""

webUiLogin = "myusername"

webUiPassword = "mypassword"

filename = "G:\Misc\1.torrent"

Dim fr As FileStream = New FileStream(filename, FileMode.Open, FileAccess.Read)

Dim br As BinaryReader = New BinaryReader(fr)

fileDataBytes = New FileInfo(filename).Length

fileData = br.ReadBytes(fileDataBytes)

Dim boundary As String = "---------------------------" & DateTime.Now.Ticks.ToString("x")

Dim postFileRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(webUiUrl), HttpWebRequest)

postFileRequest.ContentType = "multipart/form-data; boundary=" & boundary

postFileRequest.Method = "POST"

postFileRequest.KeepAlive = True

'postFileRequest.Credentials = utorrentCredentials

'postFileRequest.Headers.Add("Cookie", CookieManagerMessageInspector.SharedCookie)

' a GUID

postFileRequest.PreAuthenticate = True

postFileRequest.ServicePoint.Expect100Continue = False

Dim dataToSend As Stream = postFileRequest.GetRequestStream()

Dim boundarybytes As Byte() = Encoding.ASCII.GetBytes("--" & boundary & vbCr & vbLf)

dataToSend.Write(boundarybytes, 0, boundarybytes.Length)

Dim headerbytes As Byte() = Encoding.ASCII.GetBytes(String.Format("Content-Disposition: form-data; name=""torrent_file""; filename=""{0}""" & vbCr & vbLf & "Content-Type: application/x-bittorrent" & vbCr & vbLf & vbCr & vbLf, fileName))

dataToSend.Write(headerbytes, 0, headerbytes.Length)

dataToSend.Write(fileData, 0, fileDataBytes)

Dim trailer As Byte() = Encoding.ASCII.GetBytes(vbCr & vbLf & "--" & boundary & "--" & vbCr & vbLf)

dataToSend.Write(trailer, 0, trailer.Length)


Dim response As HttpWebResponse = DirectCast(postFileRequest.GetResponse(), HttpWebResponse)


Catch ex As WebException



End Try

End Sub

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...