I wrote some code to handle bencoding/decoding in C#, the code is based on the specification. I created also a helper class to open/save torrent files and also compute its info_hash.
I wrote this project in C#, but the sample below is in visual basic. I don't think C# developers would have problems to understand it, but if you have any question just let me know.
Sub Main()
'opens a torrent file and display its info_hash
Dim torrentFile As BDictionary = Torrent.ParseTorrentFile("C:\Users\Bruno\Desktop\test.torrent")
Console.Write("Existing torrent's info_hash: ")
Console.WriteLine(Torrent.ComputeInfoHash(torrentFile).ToString)
'creates a new torrent file
Dim newtorrent As New BDictionary()
newtorrent.Add("created by", "Bruno Piovan")
'adds some integers
newtorrent.Add("Ticks", Environment.TickCount)
newtorrent.Add("Negative", Decimal.MinValue)
'creates an info dictionary and adds it
Dim info As New BDictionary
newtorrent.Add("info", info)
'generates some random bytes
Dim sha As SHA512 = SHA512.Create
Dim bytes() As Byte = sha.ComputeHash(Encoding.UTF8.GetBytes(Environment.TickCount.ToString))
'adds the random bytes to the info dictionary
info.Add("bytes", bytes)
'adds a list
Dim list As New BList
For Each p As Process In Process.GetProcesses
list.Add(p.ProcessName)
Next
info.Add("Processes", list)
'computes the info_hash of the new torrent
Console.Write("New torrent's info_hash: ")
Console.WriteLine(Torrent.ComputeInfoHash(newtorrent).ToString)
'saves the torrent
Torrent.SaveTorrent(newtorrent, "C:\Users\Bruno\Desktop\new-torrent.torrent")
End Sub
If you use this code, let me know if you find any bug or wrong implementation.
I recently created a service to post updates to the Twitter account of my website (http://twinsornot.com/).
I found some codes that does that, but using the HttpWebRequest.
I decided to make it more simple by using WebClient.
Here's the Visual Basic .NET code:
Public Sub PostTwitterUpdate(ByVal userName As String, ByVal password As String, ByVal updateMessage As String)
Using wc As New WebClient
wc.Credentials = New NetworkCredential(userName, password)
ServicePointManager.Expect100Continue = False
Dim updateMessageBytes = System.Text.Encoding.UTF8.GetBytes("status=" & updateMessage) 'Use UTF8 to get it properly encoded if you use characters like ç ã etc...
wc.UploadData("http://twitter.com/statuses/update.xml", updateMessageBytes)
End Using
End Sub
And here is the C# version:
public void PostTwitterUpdate(string userName, string password, string updateMessage)
{
using (WebClient wc = new WebClient())
{
wc.Credentials = new NetworkCredential(userName, password);
ServicePointManager.Expect100Continue = false;
byte[] updateMessageBytes = System.Text.Encoding.UTF8.GetBytes("status=" + updateMessage); //Use UTF8 to get it properly encoded if you use characters like ç ã etc...
wc.UploadData("http://twitter.com/statuses/update.xml", updateMessageBytes);
}
}