title: Load Torrent in VB.NET with Monotorrent
body: <p>im making a simply app to download torrent files, using MonoTorrent. But i cant load the torrent properly, and I've looked all available information.</p>
  
  <p>That's the code im using: </p>
  
  <pre><code>    Imports MonoTorrent.BEncoding
  Imports MonoTorrent.Client
  Imports MonoTorrent.Client.Tracker
  Imports MonoTorrent.Common
  Imports System.IO
  Imports System.Net
  Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  
      End Sub
  End Class
  Module BitTorrentAnnounceReader
      Sub main()
  
          'load Torrentfile
          Dim TorrentFile As torrent = ("D:\youtorrent.torrent")
          Console.WriteLine(TorrentFile.Name)
  
          'generate annouce paramters
          'since we simluate a BitTornado Client double check PeerID and Key Paramter
          Dim AnnounceParameter As New AnnounceParameters
          AnnounceParameter.BytesLeft = TorrentFile.Size
          AnnounceParameter.BytesUploaded = 0
          AnnounceParameter.BytesDownloaded = 0
          AnnounceParameter.Port = 12224
          AnnounceParameter.InfoHash = TorrentFile.InfoHash
          AnnounceParameter.PeerId = "T03I-----" &amp; GenerateTorrentClientKeys(11, 1)
          AnnounceParameter.ClientEvent = TorrentEvent.Started
  
          'a torrentfile can have more than one url, we use only the first one
          'the url should have http to work for this example
          Dim AnnounceUrl As String = TorrentFile.AnnounceUrls.Item(0).Item(0).ToString
          Console.WriteLine(AnnounceUrl)
  
          'the full announceurl that will fired to tracker
          'we are simulating a BitTorando Client
          Dim FullAnnounceUrl As String = CreateAnnounceString(AnnounceParameter, AnnounceUrl, GenerateTorrentClientKeys(6))
  
          'building a webrequest for tracker request; some silly line look at comments on
          'MonoTorrent.Client.Tracker.HTTPTracker.Announce
          Dim req As HttpWebRequest = CType(WebRequest.Create(FullAnnounceUrl), HttpWebRequest)
          req.KeepAlive = False
          req.Proxy = New WebProxy
  
          'we want to simulate a BitTornado Client, so http headers
          req.UserAgent = "User-Agent: BitTornado/T-0.3.18"
  
          'to simulate full client we need also gzip but for better usage we dont use it
          ' req.Headers.Add("Accept-Encoding", "gzip")
          ' If (resp.ContentEncoding.ToLower().Contains("gzip")) Then
          ' Str = New IO.Compression.GZipStream(Str, IO.Compression.CompressionMode.Decompress)
  
          Dim response As HttpWebResponse = req.GetResponse
          Dim fs As Stream = WebResponseToStream(response)
  
          Dim peers As List(Of Peer) = AnnounceGetPeerList(fs)
          Console.WriteLine("Tracker returned:" &amp; peers.Count)
          For Each PeerInfo As Peer In peers
              Console.WriteLine(PeerInfo.ConnectionUri.Host &amp; ":" &amp; PeerInfo.ConnectionUri.Port)
          Next
  
          Console.ReadKey()
      End Sub
      ''' &lt;summary&gt;
      ''' Generate a random key depending on which TorrentClient to simulate
      ''' &lt;/summary&gt;
      ''' &lt;param name="len"&gt;&lt;/param&gt;
      ''' &lt;param name="keys"&gt;&lt;/param&gt;
      ''' &lt;returns&gt;&lt;/returns&gt;
      ''' &lt;remarks&gt;&lt;/remarks&gt;
      Function GenerateTorrentClientKeys(ByVal len As Integer, Optional ByVal keys As Integer = 1) As String
  
          Dim Chars() As String = {"abcdefghijklmnopqrstuvwxyz0123456789", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"}
          'Chars[0] = uTorrent Keys
          'Chars[1] = BitTornado Keys
          Dim str As String = ""
          Dim r As New System.Random()
  
          For i = 1 To len
              str &amp;= Chars(keys).Substring(r.Next(1, Chars(keys).Length), 1)
          Next
  
          Return str
  
      End Function
      ''' &lt;summary&gt;
      ''' A simple way of parsing a bencoded peerlist. it will autodecode compact mode request
      ''' you can find the full announce parser in the orignal file:
      ''' 
      ''' MonoTorrent.Client.Tracker.HTTPTracker.HandleAnnounce
      ''' http://anonsvn.mono-project.com/viewvc/trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Tracker/HTTPTracker.cs
      ''' &lt;/summary&gt;
      ''' &lt;param name="fs"&gt;&lt;/param&gt;
      ''' &lt;returns&gt;&lt;/returns&gt;
      ''' &lt;remarks&gt;&lt;/remarks&gt;
      Function AnnounceGetPeerList(ByVal fs As Stream) As List(Of Peer)
          'you can also use a file with a torrenrequest
          'Dim reader As New RawReader(IO.File.Open("file.req", IO.FileMode.Open), False)
  
          'decode the bencoded stream
          Dim dictionary As BEncodedDictionary = BEncodedValue.Decode(Of BEncodedDictionary)(fs)
  
          Dim peers As New List(Of Peer)
          For Each keypair In dictionary
              Select Case keypair.Key.ToString
                  Case "interval"
                      'MsgBox TimeSpan.FromSeconds(int.Parse(keypair.Value.ToString())); 
                      'MsgBox(keypair.Value.ToString)
                  Case "peers"
                      If TypeOf keypair.Value Is BEncodedList Then
                          peers.AddRange(Peer.Decode(DirectCast(keypair.Value, BEncodedList)))
                      ElseIf TypeOf keypair.Value Is BEncodedString Then
                          peers.AddRange(Peer.Decode(DirectCast(keypair.Value, BEncodedString)))
                      End If
                  Case Else
                      'MsgBox("HttpTracker - Unknown announce tag received:" &amp; keypair.Key.ToString() &amp; keypair.Value.ToString())
              End Select
          Next
  
          Return peers
      End Function
  
      ''' &lt;summary&gt;
      ''' Original CreateAnnounceString is private only and we need to modify it a little bit; this the old one out of svn
      ''' they have created here a better function that uses UriQueryBuilder but its oversized here
      ''' 
      ''' MonoTorrent.Client.Tracker.HTTPTracker.CreateAnnounceString
      ''' http://anonsvn.mono-project.com/viewvc/trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Tracker/HTTPTracker.cs?revision=141866
      ''' &lt;/summary&gt;
      ''' &lt;param name="parameters"&gt;&lt;/param&gt;
      ''' &lt;param name="Uri"&gt;AnnounceURL of the TorrentFile&lt;/param&gt;
      ''' &lt;param name="Key"&gt;a radon key paramter&lt;/param&gt;
      ''' &lt;returns&gt;url to use with a WebRequest&lt;/returns&gt;
      ''' &lt;remarks&gt;&lt;/remarks&gt;
      Private Function CreateAnnounceString(ByVal parameters As AnnounceParameters, ByVal Uri As String, ByVal Key As String) As String
          Dim sb As New System.Text.StringBuilder(256)
  
          'base.LastUpdated = DateTime.Now;
          ' FIXME: This method should be tidied up. I don't like the way it current works
          sb.Append(Uri)
          sb.Append(If(Uri.Contains("?"), "&amp;"c, "?"c))
          sb.Append("info_hash=")
          sb.Append(parameters.InfoHash.UrlEncode())
          sb.Append("&amp;peer_id=")
          sb.Append(parameters.PeerId)
          sb.Append("&amp;port=")
          sb.Append(parameters.Port)
          If parameters.SupportsEncryption Then
              sb.Append("&amp;supportcrypto=1")
          End If
          If parameters.RequireEncryption Then
              sb.Append("&amp;requirecrypto=1")
          End If
          sb.Append("&amp;uploaded=")
          sb.Append(parameters.BytesUploaded)
          sb.Append("&amp;downloaded=")
          sb.Append(parameters.BytesDownloaded)
          sb.Append("&amp;left=")
          sb.Append(parameters.BytesLeft)
          sb.Append("&amp;compact=1")
          ' Always use compact response
          sb.Append("&amp;numwant=")
          sb.Append(100)
          If Not Uri.Contains("&amp;key=") AndAlso Not Uri.Contains("?key=") Then
              sb.Append("&amp;key=")
              ' The 'key' protocol, used as a kind of 'password'. Must be the same between announces
              sb.Append(Key)
          End If
          If parameters.Ipaddress IsNot Nothing Then
              sb.Append("&amp;ip=")
              sb.Append(parameters.Ipaddress)
          End If
  
          ' If we have not successfully sent the started event to this tier, override the passed in started event
          ' Otherwise append the event if it is not "none"
          'if (!parameters.Id.Tracker.Tier.SentStartedEvent)
          '{
          '    sb.Append("&amp;event=started");
          '    parameters.Id.Tracker.Tier.SendingStartedEvent = true;
          '}
          If parameters.ClientEvent &lt;&gt; TorrentEvent.None Then
              sb.Append("&amp;event=")
              sb.Append(parameters.ClientEvent.ToString().ToLower())
          End If
  
  
          Return sb.ToString()
      End Function
      ''' &lt;summary&gt;
      ''' HttpWebResponse and GetResponseStream dont gives use a full readable stream so we must convert it
      ''' 
      ''' Look at: MonoTorrent.Client.Tracker.HTTPTracker.DecodeResponse
      ''' http://anonsvn.mono-project.com/viewvc/trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Tracker/HTTPTracker.cs
      ''' or
      ''' http://bytes.com/topic/c-sharp/answers/232436-download-binary-file-http#post949811
      ''' &lt;/summary&gt;
      ''' &lt;param name="response"&gt;&lt;/param&gt;
      ''' &lt;returns&gt;&lt;/returns&gt;
      ''' &lt;remarks&gt;&lt;/remarks&gt;
      Function WebResponseToStream(ByVal response As HttpWebResponse) As Stream
  
          Dim responseStream As Stream = response.GetResponseStream
          Dim fs As MemoryStream = New MemoryStream(256)
  
          Dim buffer As Byte() = New Byte(4095) {}
          Dim length As Integer = responseStream.Read(buffer, 0, 4096)
  
          While length &gt; 0
              fs.Write(buffer, 0, length)
              length = responseStream.Read(buffer, 0, 4096)
          End While
  
          fs.Seek(0, SeekOrigin.Begin)
  
          Return fs
      End Function
  End Module
  </code></pre>
  
  <p>The problem only appears in the load file .torrent part: </p>
  
  <pre><code>Dim TorrentFile As **torrent** = ("D:\youtorrent.torrent")
  </code></pre>
  
  <p>Vb.net throws me an error of "Type expected 'on the text <em>torrent</em>. there any solution? Thanks to all!</p>