public bool AddTorrent(string fileName, string label)
{
//First, get excisting torrents from uTorrent, in case some have been added from outside this service...
GetTorrents(true);
Stream TorrentStream = File.OpenRead(fileName);
HttpWebRequest PostReq = (HttpWebRequest)(HttpWebRequest.Create(host + "?action=add-file&token=" + token));
PostReq.KeepAlive = false;
PostReq.Credentials = credentials;
string guid = Guid.NewGuid().ToString("N");
PostReq.ContentType = "multipart/form-data; boundary=" + guid;
PostReq.Method = "POST";
PostReq.Headers.Add("Cookie", cookie);
using (BinaryWriter Writer = new BinaryWriter(PostReq.GetRequestStream()))
{
byte[] FileBytes = new byte[TorrentStream.Length];
TorrentStream.Read(FileBytes, 0, FileBytes.Length);
Writer.Write(Encoding.ASCII.GetBytes(String.Format("--{0}\r\nContent-Disposition: form-data; name=\"torrent_file\"; filename=\"{0}\"\r\nContent-Type: application/x-bittorrent\r\n\r\n", guid, fileName)));
Writer.Write(FileBytes, 0, FileBytes.Length);
Writer.Write(Encoding.ASCII.GetBytes(String.Format("\r\n--{0}\r\n", guid)));
}
HttpWebResponse response = (HttpWebResponse)PostReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string t = sr.ReadToEnd();
response.Close();
foreach (Torrent newTorrent in NewTorrents())
{
if (newTorrent.Label == "")
{
HttpWebRequest addLabel = (HttpWebRequest)(HttpWebRequest.Create(host + "?action=setprops&token=" + token + "&hash="+newTorrent.Hash+"&s=label&v="+label));
addLabel.Credentials = credentials;
addLabel.Headers.Add("Cookie", cookie);
HttpWebResponse _response = (HttpWebResponse)addLabel.GetResponse();
sr = new StreamReader(_response.GetResponseStream());
t = sr.ReadToEnd();
_response.Close();
Console.WriteLine("Added " + fileName + " with label " + label);
Console.WriteLine();
return true;
}
}
return false;
}