VB.NET Help!!

Discussion in 'Code Snippets and Tutorials' started by ZOMG, Apr 17, 2007.

  1. ZOMG

    ZOMG Level I

    Joined:
    Feb 26, 2007
    Messages:
    122
    Likes Received:
    0
    Location:
    Hipocresy Land
    Hey,, how i can upload an httpwrapper,, or any file to my project?
     
  2. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    There is a wrapper Mystical (Robawatic) made a while ago in this section, all you have to do to use it is copy and pasting the code in a class... Link here

    Code (Text):
    1. Imports System.Text.Regularexpressions
    2. Imports System.Net
    3. Imports System.IO
    4. Imports System.Text
    5. Imports System.IO.Compression
    6. Imports System.Runtime.Serialization.Formatters.Binary
    7. Public Class TCPWrapper
    8.     Inherits System.Windows.Forms.UserControl
    9. #Region " Windows Form Designer generated code "
    10.     Public Sub New()
    11.         MyBase.New()
    12.         'This call is required by the Windows Form Designer.
    13.         InitializeComponent()
    14.         'Add any initialization after the InitializeComponent() call
    15.     End Sub
    16.     'UserControl overrides dispose to clean up the component list.
    17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    18.         If disposing Then
    19.             If Not (components Is Nothing) Then
    20.                 components.Dispose()
    21.             End If
    22.         End If
    23.         MyBase.Dispose(disposing)
    24.     End Sub
    25.     'Required by the Windows Form Designer
    26.     Private components As System.ComponentModel.IContainer
    27.     'NOTE: The following procedure is required by the Windows Form Designer
    28.     'It can be modified using the Windows Form Designer.
    29.     'Do not modify it using the code editor.
    30.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    31.         components = New System.ComponentModel.Container
    32.     End Sub
    33. #End Region
    34.     Private colCookies As New Collection
    35.     Dim strCookies As String
    36.     Public LastPage As String
    37.     Public bGzip As Boolean
    38.     Public Function Request(ByVal Method As String, ByVal URL As String, ByVal Referer As String) As String
    39.         Dim Host As String = Nothing
    40.         Dim strFile As String = Nothing
    41.         Dim strPost As String = Nothing
    42.         Dim pos As Integer = 0
    43.         If Referer Is Nothing Then
    44.             Referer = LastPage
    45.         End If
    46.         If URL.Contains("http://") Then
    47.             Host = URL.Substring(7)
    48.         Else
    49.             Host = URL
    50.         End If
    51.         If Host.Contains("/") Then
    52.             pos = Host.IndexOf("/", 0)
    53.             strFile = Host.Substring(pos)
    54.             Host = Host.Substring(0, pos)
    55.         Else
    56.             strFile = "/"
    57.         End If
    58.         If Method = "POST" Then
    59.             pos = strFile.IndexOf("?")
    60.             If Not (pos = -1) Then
    61.                 strPost = strFile.Substring(pos + 1)
    62.                 strFile = strFile.Substring(0, pos)
    63.             Else
    64.                 strPost = Nothing
    65.             End If
    66.         End If
    67.         LastPage = URL
    68.         Dim ReqHeaders As String = Nothing
    69.         If bGzip = True Then
    70.             If Method = "GET" OrElse Method = "PIC" Then
    71.                 ReqHeaders = "GET" + " " + strFile + " HTTP/1.1" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Host: " + Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Language: en-us,en;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Encoding: gzip, deflate" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Keep-Alive: 300" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: keep-alive" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Referer: " + Referer + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Cookie: " + strCookies + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & ""
    72.             Else
    73.                 ReqHeaders = "POST " + strFile + " HTTP/1.1" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Host: " + Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Language: en-us,en;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Encoding: gzip, deflate" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Keep-Alive: 300" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: keep-alive" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Referer: " + Referer + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Cookie: " + strCookies + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Content-Type: application/x-www-form-urlencoded" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Content-Length: " + strPost.Length.ToString + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: close" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + strPost
    74.             End If
    75.             If Method = "PIC" Then
    76.                 ReqHeaders.Replace("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept: image/png,*/*;q=0.5")
    77.             End If
    78.         Else
    79.             If Method = "GET" OrElse Method = "PIC" Then
    80.                 ReqHeaders = "GET" + " " + strFile + " HTTP/1.1" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Host: " + Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Language: en-us,en;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" & "" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Keep-Alive: 300" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: keep-alive" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Referer: " + Referer + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Cookie: " + strCookies + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & ""
    81.             Else
    82.                 ReqHeaders = "POST " + strFile + " HTTP/1.1" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Host: " + Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Language: en-us,en;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Keep-Alive: 300" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: keep-alive" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Referer: " + Referer + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Cookie: " + strCookies + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Content-Type: application/x-www-form-urlencoded" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Content-Length: " + strPost.Length.ToString + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: close" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + strPost
    83.             End If
    84.             If Method = "PIC" Then
    85.                 ReqHeaders.Replace("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept: image/png,*/*;q=0.5")
    86.             End If
    87.         End If
    88.  
    89.         Dim tcp As New System.Net.Sockets.TcpClient
    90.         Dim netstream As System.Net.Sockets.NetworkStream
    91.         Dim TN(1) As Long
    92.         If Referer = "" Then Referer = LastPage
    93.         If InStr(1, URL, "http://") <> 0 Then Host = Mid$(URL, 8)
    94.         If InStr(1, Host, "/") <> 0 Then Host = Mid$(Host, 1, InStr(1, Host, "/") - 1)
    95.         If InStr(1, Host, "?") <> 0 Then Host = Mid$(Host, 1, InStr(1, Host, "?") - 1)
    96.         If Mid$(Host, 1, 4) = "www." Then Host = Mid$(Host, 5)
    97.         LastPage = URL
    98.         Try
    99.             tcp.Connect(Host, 80)
    100.         Catch ex As Exception
    101.             Return ex.Message
    102.         End Try
    103.  
    104.         Dim sendbytes As Byte()
    105.         sendbytes = System.Text.Encoding.ASCII.GetBytes(ReqHeaders)
    106.         netstream = tcp.GetStream()
    107.         netstream.Write(sendbytes, 0, sendbytes.Length)
    108.         Dim sr As StreamReader = New StreamReader(netstream, Encoding.Default)
    109.         Dim strHTML As String = sr.ReadToEnd
    110.         Dim strParts As String() = Regex.Split(strHTML, Environment.NewLine + Environment.NewLine)
    111.         strCookies = ParseCookies(strParts(0))
    112.         If strParts(0).Contains("Content-Encoding") Then
    113.             strParts(1) = DecompressGzip(strParts(1))
    114.         End If
    115.         Dim uppBound As Integer = strParts.GetUpperBound(0)
    116.         Dim strReturn As String = ""
    117.         For i As Integer = 1 To uppBound
    118.             strReturn += strParts(i)
    119.         Next
    120.         If InStr(strReturn, "aisha_signup.gif") Then
    121.             If frmMain.chkWarn.Checked = False Then
    122.                 frmMain.TimerAutobuy.Enabled = False
    123.                 frmMain.lblStatus.Text = "Logged out!"
    124.                 frmMain.GroupBox1.Enabled = True
    125.                 frmMain.cmdLogout.Enabled = False
    126.                 frmMain.cmdLogin.Enabled = True
    127.                 frmMain.cmdStop.Enabled = False
    128.                 frmMain.cmdStart.Enabled = True
    129.                 MsgBox("You have been logged out!", MsgBoxStyle.Critical, "Logged out!")
    130.                 ClearCookies()
    131.             Else
    132.                 frmMain.lblStatus.Text = "Logged out!"
    133.                 frmMain.TimerAutobuy.Enabled = False
    134.                 ClearCookies()
    135.                 Pause(5)
    136.                 frmMain.Login(frmMain.txtUsername.Text, frmMain.txtPassword.Text)
    137.                 Pause(5)
    138.                 frmMain.lblStatus.Text = "Looking for Items"
    139.                 frmMain.TimerAutobuy.Enabled = True
    140.                 frmMain.cmdStop.Enabled = True
    141.                 frmMain.cmdStart.Enabled = False
    142.             End If
    143.         End If
    144.         Return strReturn
    145.  
    146.  
    147.     End Function
    148.     Public Function DecompressGzip(ByVal compressed As String) As String
    149.         Dim memStream As MemoryStream = New MemoryStream(System.Text.Encoding.Default.GetBytes(compressed))
    150.         Dim decompressStream As GZipStream = New GZipStream(memStream, CompressionMode.Decompress)
    151.         Dim endBytes(4) As Byte
    152.         Dim position As Integer = CType(memStream.Length, Integer) - 4
    153.         memStream.Position = position
    154.         memStream.Read(endBytes, 0, 4)
    155.         memStream.Position = 0
    156.         Dim buffer(BitConverter.ToInt32(endBytes, 0) + 100) As Byte
    157.         Dim offset As Integer = 0
    158.         Dim total As Integer = 0
    159.         While True
    160.             Dim bytesRead As Integer = decompressStream.Read(buffer, offset, 100)
    161.             If bytesRead = 0 Then
    162.                 Exit While
    163.             End If
    164.             offset += bytesRead
    165.             total += bytesRead
    166.         End While
    167.         Return Encoding.ASCII.GetString(buffer)
    168.     End Function
    169.     Public Function ParseCookies(ByVal Headers As String) As String
    170.         ParseCookies = ""
    171.         Dim reg As Regex
    172.         Dim matches As MatchCollection
    173.         Dim match As Match
    174.         reg = New Regex("set-cookie:\s*([^=]+)=([^;]+);", RegexOptions.IgnoreCase)
    175.         If reg.IsMatch(Headers) Then
    176.             matches = reg.Matches(Headers)
    177.             For Each match In matches
    178.                 Try
    179.                     colCookies.Add(match.Groups(1).ToString & "=" & match.Groups(2).ToString, match.Groups(1).ToString)
    180.                 Catch ex As Exception
    181.                     colCookies.Remove(match.Groups(1).ToString)
    182.                     colCookies.Add(match.Groups(1).ToString & "=" & match.Groups(2).ToString, match.Groups(1).ToString)
    183.                 End Try
    184.             Next
    185.         End If
    186.         Dim i As Long
    187.         For i = 1 To colCookies.Count Step 1
    188.             ParseCookies = ParseCookies & colCookies.Item(i).ToString & ";"
    189.         Next
    190.     End Function
    191.     Public Function StripHeaders(ByVal strSource As String) As String
    192.         Dim strParts() As String = Regex.Split(strSource, Environment.NewLine + Environment.NewLine)
    193.         Return strParts(1)
    194.     End Function
    195.     Public Function NeoLogin(ByVal user As String, ByVal pass As String, ByRef loggedIn As Boolean) As String
    196.         ClearCookies()
    197.         Dim strHTML As String = Nothing
    198.         Request("GET", "http://Filtered Link - For the safety of our users please do not post direct links to Neopets./loginpage.phtml", "http://google.com")
    199.         Pause(1)
    200.         Request("POST", "http://www.Filtered Link - For the safety of our users please do not post direct links to Neopets./hi.phtml?destination=%2Fpetcentral.phtml&username=" + user, "http://Filtered Link - For the safety of our users please do not post direct links to Neopets./loginpage.phtml")
    201.         Pause(1)
    202.         Request("POST", "http://www.Filtered Link - For the safety of our users please do not post direct links to Neopets./login.phtml?username=" + user + "&password=" + pass + "&destination=%2Fpetcentral.phtml", "http://Filtered Link - For the safety of our users please do not post direct links to Neopets./hi.phtml")
    203.         strHTML = Request("GET", "http://www.Filtered Link - For the safety of our users please do not post direct links to Neopets./objects.phtml", "http://www.Filtered Link - For the safety of our users please do not post direct links to Neopets./petcentral.phtml")
    204.         If strHTML.Contains("Welcome,") Then
    205.             loggedIn = True
    206.             Return "Logged In"
    207.         Else
    208.             If strHTML.Contains("too many times") Then
    209.                 loggedIn = False
    210.                 Return "To Many Login Attempts"
    211.             Else
    212.                 If strHTML.Contains("not logged in") Then
    213.                     loggedIn = False
    214.                     Return "Wrong Password"
    215.                 Else
    216.                     If strHTML.Contains("frozen") Then
    217.                         loggedIn = False
    218.                         Return "Account Frozen"
    219.                     Else
    220.                         If strHTML.Contains("just a technical problem") Then
    221.                             loggedIn = False
    222.                             Return "Neopets is down for maintenance."
    223.                         Else
    224.                             loggedIn = False
    225.                             Return strHTML
    226.                         End If
    227.                     End If
    228.                 End If
    229.             End If
    230.         End If
    231.     End Function
    232.     Private Shared Sub Pause(ByVal seconds As Double)
    233.         Dim num As Double = seconds * 1000
    234.         Dim t1 As DateTime = DateTime.Now
    235.         Dim t2 As DateTime = DateTime.Now
    236.         Dim tmDiff As TimeSpan = t2 - t1
    237.         While Convert.ToDouble(tmDiff.TotalMilliseconds.ToString) < num
    238.             t2 = DateTime.Now
    239.             tmDiff = t2 - t1
    240.             Application.DoEvents()
    241.         End While
    242.     End Sub
    243.     Public Function GrabPic(ByVal strURL As String) As System.Drawing.Image
    244.         Dim memStream As New MemoryStream(System.Text.Encoding.Default.GetBytes(StripHeaders(Request("GET", strURL, LastPage))))
    245.         GrabPic = Image.FromStream(memStream)
    246.         Return GrabPic
    247.     End Function
    248.     Public Function DownloadImage(ByVal URL As String) As System.Drawing.Image
    249.         Dim imageFile As New WebClient
    250.         Dim bytes() As Byte = imageFile.DownloadData(URL)
    251.  
    252.         Dim imageStream As New MemoryStream(bytes)
    253.         Dim downloadedImage As System.Drawing.Image = Image.FromStream(imageStream)
    254.  
    255.         DownloadImage = downloadedImage
    256.     End Function
    257.     Public Sub ClearCookies()
    258.         colCookies.Clear()
    259.         strCookies = Nothing
    260.     End Sub
    261.  
    262.     Public Function GetCookie()
    263.         Return strCookies
    264.     End Function
    265. End Class
     
  3. ZOMG

    ZOMG Level I

    Joined:
    Feb 26, 2007
    Messages:
    122
    Likes Received:
    0
    Location:
    Hipocresy Land
    :shock: wt....
    Er.. thx...
    But i'll prefer a example xD
     
  4. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    Well, create a new class and name it TCPWrapper, then paste the Wrapper code in... that's not so hard... (and if I remember well you will need to put a "Private Wrapper As New TCPWrapper" In the form declarations...)