<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Bruno Piovan</title><subtitle type="html" /><id>http://weblogs.asp.net/brunopiovan/atom.aspx</id><link rel="alternate" type="text/html" href="http://weblogs.asp.net/brunopiovan/default.aspx" /><link rel="self" type="application/atom+xml" href="http://weblogs.asp.net/brunopiovan/atom.aspx" /><generator uri="http://communityserver.org" version="3.0.20510.895">Community Server</generator><updated>2009-04-06T13:32:00Z</updated><entry><title>BEncoding and Torrent files with .Net</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/brunopiovan/archive/2009/05/03/bencoding-and-torrent-files-with-net.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="9680" href="http://weblogs.asp.net/brunopiovan/attachment/7071406.ashx" /><id>http://weblogs.asp.net/brunopiovan/archive/2009/05/03/bencoding-and-torrent-files-with-net.aspx</id><published>2009-05-03T22:36:00Z</published><updated>2009-05-03T22:36:00Z</updated><content type="html">&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;&lt;PRE style="WIDTH: auto; OVERFLOW: scroll"&gt;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&lt;/PRE&gt;
&lt;P&gt;If you use this code, let me know if you find any bug or wrong implementation.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7071406" width="1" height="1"&gt;</content><author><name>brunopiovan</name><uri>http://weblogs.asp.net/members/brunopiovan.aspx</uri></author><category term="ASP.NET" scheme="http://weblogs.asp.net/brunopiovan/archive/tags/ASP.NET/default.aspx" /><category term="Visual Basic" scheme="http://weblogs.asp.net/brunopiovan/archive/tags/Visual+Basic/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/brunopiovan/archive/tags/C_2300_/default.aspx" /><category term=".NET" scheme="http://weblogs.asp.net/brunopiovan/archive/tags/.NET/default.aspx" /><category term="Torrent" scheme="http://weblogs.asp.net/brunopiovan/archive/tags/Torrent/default.aspx" /></entry><entry><title>How to post updates to Twitter using Visual Basic.NET and C#</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/brunopiovan/archive/2009/04/06/how-to-post-updates-to-twitter-using-visual-basic-net.aspx" /><id>http://weblogs.asp.net/brunopiovan/archive/2009/04/06/how-to-post-updates-to-twitter-using-visual-basic-net.aspx</id><published>2009-04-06T17:32:00Z</published><updated>2009-04-06T17:32:00Z</updated><content type="html">&lt;P mce_keep="true"&gt;I recently created a service to post updates to the &lt;A href="http://twitter.com/TwinsOrNot" target=_blank mce_href="http://twitter.com/TwinsOrNot"&gt;Twitter account&lt;/A&gt; of my &lt;A href="http://twinsornot.com/" target=_blank mce_href="http://twinsornot.com"&gt;website&lt;/A&gt;&amp;nbsp;(&lt;A href="http://twinsornot.com/"&gt;http://twinsornot.com/&lt;/A&gt;).&lt;/P&gt;
&lt;P mce_keep="true"&gt;I found some codes that does that, but using the &lt;A href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx"&gt;HttpWebRequest&lt;/A&gt;.&lt;/P&gt;
&lt;P mce_keep="true"&gt;I decided to make it more simple by using &lt;A href="http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx"&gt;WebClient&lt;/A&gt;.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Here's the Visual Basic .NET code:&lt;/P&gt;&lt;PRE style="OVERFLOW: scroll"&gt;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=" &amp;amp; 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&lt;/PRE&gt;
&lt;P mce_keep="true"&gt;And here is the C# version:&lt;/P&gt;&lt;PRE style="OVERFLOW: scroll"&gt;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);
    }
}&lt;/PRE&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7030346" width="1" height="1"&gt;</content><author><name>brunopiovan</name><uri>http://weblogs.asp.net/members/brunopiovan.aspx</uri></author><category term="Twitter" scheme="http://weblogs.asp.net/brunopiovan/archive/tags/Twitter/default.aspx" /><category term="ASP.NET" scheme="http://weblogs.asp.net/brunopiovan/archive/tags/ASP.NET/default.aspx" /><category term="Visual Basic" scheme="http://weblogs.asp.net/brunopiovan/archive/tags/Visual+Basic/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/brunopiovan/archive/tags/C_2300_/default.aspx" /><category term=".NET" scheme="http://weblogs.asp.net/brunopiovan/archive/tags/.NET/default.aspx" /></entry></feed>
