tags:

views:

11

answers:

0

I would like to write an application that uploads torrents to a tbsource site.

I have the below but it doesn't seem to work.

I am not that good with webrequest so have no idea what I'm doing here

Please could some1 help me find out what I am doing wrong with the below code.

Imports System.Net
Imports System.Text
Imports System.IO

Public Class Form1

Public cookies As New CookieContainer
Public cookieHeader As String

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

   Dim formUrl As String = "http://localhost/tb/takelogin.php?" 
   ' NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
   Dim formParams As String = String.Format("username={0}&password={1}", "user001", "password100")

   Dim req As HttpWebRequest = WebRequest.Create(formUrl)
   req.AllowAutoRedirect = False
   req.CookieContainer = New CookieContainer()
   cookies = req.CookieContainer
   req.ContentType = "application/x-www-form-urlencoded"
   req.Method = "POST"
   Dim bytes As Byte() = Encoding.ASCII.GetBytes(formParams)
   req.ContentLength = bytes.Length
   Using os As Stream = req.GetRequestStream()
       os.Write(bytes, 0, bytes.Length)
   End Using
   Dim resp As HttpWebResponse = req.GetResponse()
   resp.Cookies = req.CookieContainer.GetCookies(req.RequestUri)
   'cookieHeader = resp.Headers("Set-cookie")
   cookieHeader = resp.Cookies(0).ToString
   ' MsgBox(cookieHeader)

   Using sr As New StreamReader(resp.GetResponseStream())
       ' MsgBox(sr.ReadToEnd())
   End Using




   Dim filepath As String = "c:\ubuntu-10.10-desktop-i386.iso.torrent"
   Dim url As String = "http://localhost/tb/takeupload.php?"
   Dim newLine As String = vbCr & vbLf
   Dim boundary As String = "-----------------------------" & IO.Path.GetRandomFileName
   Dim header As New System.Text.StringBuilder()

   header.AppendLine("-----------------------------" & boundary)
   'header.AppendLine()
   header.AppendLine("Content-Disposition: form-data; name=""name""")
   header.AppendLine()
   header.AppendLine()
   header.AppendLine("TorrentName")
   'header.AppendLine()


   header.AppendLine("-----------------------------" & boundary)
   'header.AppendLine()
   header.AppendLine("Content-Disposition: form-data; name=""descr""")
   header.AppendLine()
   header.AppendLine()
   header.AppendLine("TorrentDescrition")
   ' header.AppendLine()

   header.AppendLine("-----------------------------" & boundary)
   ' header.AppendLine()
   header.AppendLine("Content-Disposition: form-data; name=""type""")
   header.AppendLine()
   header.AppendLine()
   header.AppendLine("7")
   ' header.AppendLine()

   header.AppendLine("-----------------------------" & boundary)
   'header.AppendLine()
   header.AppendLine("Content-Disposition: form-data; name=""MAX_FILE_SIZE""")
   header.AppendLine()
   header.AppendLine()
   header.AppendLine("1000000")
   ' header.AppendLine()

   header.AppendLine("-----------------------------" & boundary)
   header.Append("Content-Disposition: form-data; name=""file"";")
   header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(filepath))
   header.AppendLine()
   header.AppendLine("Content-Type: application/x-bittorrent")
   'header.AppendLine()


   Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)
   Dim endboundarybytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "-----------------------------" & boundary & "--" & newLine)

   req = Net.HttpWebRequest.Create(url)
          req.UserAgent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10"""
   req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"""
   req.Headers.Add("Accept-Language: en-gb,en;q=0.5")
   req.Headers.Add("Accept-Encoding: gzip, deflate")
   req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7")
   req.KeepAlive = "115"
   req.Connection = "keep(-alive)"
   req.Referer = "http://fools-pc/tb/upload.php"
   req.Headers.Add("Cookie", cookieHeader)
   req.CookieContainer = cookies 'CookieContainer()
   req.ContentType = "multipart/form-data; boundary=" & boundary
   req.ContentLength = headerbytes.Length + New IO.FileInfo(filepath).Length + endboundarybytes.Length
   req.Method = "POST"

   Dim s As IO.Stream = req.GetRequestStream
   s.Write(headerbytes, 0, headerbytes.Length)
   Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filepath)
   s.Write(filebytes, 0, filebytes.Length)
   s.Write(endboundarybytes, 0, endboundarybytes.Length)
   s.Close()


 End Sub
End Class