Announcement

Collapse

Welcome to the New Limpkinw.com

Hey all! I had to move servers and the old version of Vbulletin was not compatible so I did an update.

I know there is no activity here but we do get a lot of visitors, and quote a few old school Yahoo Gamers come by once in a while so I'd hate to take the site down.

Anyways I am going to try to reactivate user registration so some of the folks who forgot their login info can make new accounts.


Maybe I will get a bug up my a$$ and add back in the old arcade and stuff.

I am sorry for the UGLY theme but I will get that fixed at some point!

I hope everyone is well!!!!
See more
See less

.NET Challenge: Connect using TCPClient class

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • .NET Challenge: Connect using TCPClient class

    this is weekly but since this forum is so slow, I'll just see how long this lasts

    Objective: Set up an connection to a remote host using the .NET TCPClient class or if youre feeling even more ballsy, using the TcpClient base class (Sockets)
    I don't recommend the latter.

    Note: Do not use a winsock control, must be .NET

    Don't got VB.NET? Go here: http://www.limpkinw.com/forums/announcement.php?f=28

  • #2
    is this project still open?

    Comment


    • #3
      of course

      Comment


      • #4
        well anyone else active here that even fucks with .net besides you/

        Comment


        • #5
          all you need to do is add proxy and cookie support and your ready to do something useful with this.
          Code:
          Public Class MyWrapper
              Enum HTTPRequest
                  HTTPPOST = 1
                  HTTPGET = 2
              End Enum
              Private client As TcpClient
              Private readBuffer(READ_BUFFER_SIZE) As Byte
              Public strHTML As String
              Dim Finished As Boolean
              Public Function GetHTTP(ByVal HType As HTTPRequest, ByVal strUrl As String, Optional ByVal cookie As String = "", Optional ByVal referer As String = "", Optional ByVal data As String = "") As String
                  Dim strServer As String = ""
                  Dim strPage As String = ""
                  Dim header As String = ""
                  Dim iStart As Integer = 0
          
                  If strUrl.Contains("http://") Then
                      strUrl = strUrl.Replace("http://", "")
                  End If
          
                  iStart = strUrl.IndexOf(".", 6) + 4
                  strPage = strUrl.Substring(iStart, (strUrl.Length) - iStart)
                  strServer = strUrl.Replace(strPage, "")
                  strPage = strPage.Replace(" ", "%20")
                  client = New TcpClient(strServer, PORT_NUM)
                  client.GetStream.BeginRead(readBuffer, 0, 255, AddressOf DoRead, Nothing)
                  header = CreateHeader(HType, strServer, strPage, cookie, referer, data)
          
                  SendData(header)
          
                  Do Until Finished = True And strHTML <> ""
                      Application.DoEvents()
                  Loop
          
                  Finished = False
                  Return strHTML
                  strHTML = ""
              End Function
              Private Sub DoRead(ByVal ar As IAsyncResult)
                  Dim BytesRead As Integer
          
                  Try
                      BytesRead = client.GetStream.EndRead(ar)
                      If BytesRead < 1 Then
                          Finished = True
                          Exit Sub
                      End If
          
                      strHTML &= Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2)
          
                      client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
                  Catch e As Exception
                      MsgBox(e.Message)
                  End Try
              End Sub
              Private Sub SendData(ByVal data As String)
                  Try
                      Dim writer As New IO.StreamWriter(client.GetStream)
                      writer.Write(data & vbCr)
                      writer.Flush()
                  Catch e As Exception
                      Exit Sub
                  End Try
              End Sub
              Private Function CreateHeader(ByVal HType As HTTPRequest, ByVal strServer As String, ByVal strPage As String, Optional ByVal cookie As String = "", Optional ByVal referer As String = "", Optional ByVal data As String = "", Optional ByVal GZIP As Boolean = False) As String
                  Dim strType As String = ""
                  Dim strrequest As String = ""
                  Dim browser As String = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1"
          
                  If HType = HTTPRequest.HTTPGET Then
                      strType = "GET"
                  Else
                      strType = "POST"
                  End If
          
                  strrequest = _
                             strType & " /" & strPage & " HTTP/1.1" & vbCrLf & _
                             "Host: " & strServer & vbCrLf & _
                             "User-Agent: " & browser & vbCrLf & _
                             "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" & vbCrLf & _
                             "Accept-Language: en,en-us;q=0.5" & vbCrLf
                  If GZIP = True Then strrequest &= "Accept-Encoding: gzip, deflate" & vbCrLf
                  strrequest &= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" & vbCrLf & _
                              "Connection: close" & vbCrLf
                  If referer <> "" Then strrequest &= "Referer: " & referer & vbCrLf
                  If cookie <> "" Then strrequest &= "Cookie:" & cookie & vbCrLf
                  If data <> "" Then
                      strrequest &= "Content-Type: application/x-www-form-urlencoded" & vbCrLf
                      strrequest &= "Content-Length: " & Len(data) & vbCrLf & vbCrLf
                      strrequest &= data & vbCrLf
                  End If
          
                  strrequest &= Chr(10)
                  CreateHeader = strrequest
          
              End Function
          End Class

          Comment


          • #6
            We have a winner

            Comment


            • #7
              here is how i handle cookies
              Code:
              Imports System.Text.Regularexpressions
              Public Class Spockete
                  Inherits System.Windows.Forms.UserControl
              #Region " Windows Form Designer generated code "
                  Public Sub New()
                      MyBase.New()
                      'This call is required by the Windows Form Designer.
                      InitializeComponent()
                      'Add any initialization after the InitializeComponent() call
                  End Sub
                  'UserControl overrides dispose to clean up the component list.
                  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                      If disposing Then
                          If Not (components Is Nothing) Then
                              components.Dispose()
                          End If
                      End If
                      MyBase.Dispose(disposing)
                  End Sub
                  'Required by the Windows Form Designer
                  Private components As System.ComponentModel.IContainer
                  'NOTE: The following procedure is required by the Windows Form Designer
                  'It can be modified using the Windows Form Designer.  
                  'Do not modify it using the code editor.
                  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
                      components = New System.ComponentModel.Container
                  End Sub
              #End Region
                  Private colCookies As New Collection
                  Dim strCookies As String
                  Public LastPage As String
                  Private Function Request(ByVal Method As String, ByVal URL As String, Optional ByVal Referer As String = "") As String
                      Dim tcp As New System.Net.Sockets.TcpClient
                      Dim netstream As System.Net.Sockets.NetworkStream
                      Dim Host As String, TN(1) As Long
                      If Referer = "" Then Referer = LastPage
                      If InStr(1, URL, "http://") Then
                          Host = Mid(URL, 8)
                      End If
                      If InStr(1, URL, "/") Then
                          TN(0) = InStr(1, URL, "/")
                          Host = Mid(URL, 1, TN(0) - 1)
                      End If
                      LastPage = URL
                      Try
                          tcp.Connect(Host, 80)
                      Catch ex As Exception
                          Return ex.Message
                      End Try
              
                      Dim sendbytes As Byte(), ReqHeaders As String
                      ReqHeaders = SortHeaders(Method, URL, Referer)
                      sendbytes = System.Text.Encoding.ASCII.GetBytes(ReqHeaders)
                      MsgBox(ReqHeaders)
                      netstream = tcp.GetStream()
                      netstream.Write(sendbytes, 0, sendbytes.Length)
                      tcp.ReceiveBufferSize = 1048576
                      Dim recv(tcp.ReceiveBufferSize) As Byte
                      netstream.Read(recv, 0, CInt(tcp.ReceiveBufferSize))
                      Dim out As String
                      out = System.Text.Encoding.UTF8.GetString(recv)
                      strCookies = ParseCookies(out.ToString)
                      Return out
                  End Function
                  Private Function SortHeaders(ByVal Method As String, ByVal URL As String, ByVal Referer As String)
                      Dim Host As String, URI As String, tn(1) As Integer, PostData As String
                      If InStr(1, URL, "/") Then
                          tn(0) = InStr(1, URL, "/")
                          Host = Mid(URL, 1, tn(0) - 1)
                          URI = Mid(URL, tn(0))
                          If InStr(1, URI, "?") Then
                              tn(1) = InStr(1, URI, "?")
                              URI = Mid(URI, 1, tn(1) - 1)
                          End If
                      End If
                      If InStr(1, URL, "?") And Method = "POST" Then
                          tn(0) = InStr(1, URL, "?") + 1
                          PostData = Mid(URL, tn(0))
                      End If
                      Select Case Method
                          Case "POST"
                              SortHeaders = "POST " & URI & " HTTP/1.0" & ControlChars.CrLf & _
                                  "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*" & ControlChars.CrLf & _
                                  "Referer: " & Referer & ControlChars.CrLf & _
                                  "Accept-Language: en-gb" & ControlChars.CrLf & _
                                  "Content-Type: application/x-www-form-urlencoded" & ControlChars.CrLf & _
                                  "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6" & ControlChars.CrLf & _
                                  "Host: " & Host & ControlChars.CrLf & _
                                  "Content-Length: " & Len(PostData).ToString & ControlChars.CrLf & _
                                  "Pragma: no-cache" & ControlChars.CrLf & _
                                  "Cookie: " & strCookies & ControlChars.CrLf & _
                                  ControlChars.CrLf & _
                                  PostData & ControlChars.CrLf
                          Case Else
                              SortHeaders = Method & " " & URI & " HTTP/1.0" & ControlChars.CrLf & _
                                  "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*" & ControlChars.CrLf & _
                                  "Referer: " & Referer & ControlChars.CrLf & _
                                  "Accept-Language: en-gb" & ControlChars.CrLf & _
                                  "Content-Type: application/x-www-form-urlencoded" & ControlChars.CrLf & _
                                  "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6" & ControlChars.CrLf & _
                                  "Host: " & Host & ControlChars.CrLf & _
                                  "Connection: Keep-Alive" & ControlChars.CrLf & _
                                  "Cookie: " & strCookies & _
                                  ControlChars.CrLf & ControlChars.CrLf
                      End Select
                  End Function
                  Public Function ParseCookies(ByVal Headers As String)
                      Dim reg As Regex
                      Dim matches As MatchCollection
                      Dim match As Match
                      reg = New Regex("set-cookie:\s*([^=]+)=([^;]+);", RegexOptions.IgnoreCase)
                      If reg.IsMatch(Headers) Then
                          matches = reg.Matches(Headers)
                          For Each match In matches
                              Try
                                  colCookies.Add(match.Groups(1).ToString & "=" & match.Groups(2).ToString, match.Groups(1).ToString)
                              Catch ex As Exception
                                  colCookies.Remove(match.Groups(1).ToString)
                                  colCookies.Add(match.Groups(1).ToString & "=" & match.Groups(2).ToString, match.Groups(1).ToString)
                              End Try
                          Next
                      End If
                      Dim i As Long
                      For i = 1 To colCookies.Count Step 1
                          ParseCookies = ParseCookies & colCookies.Item(i).ToString & ";"
                      Next
                  End Function
              End Class

              Comment

              Working...
              X