I coded this a while back and decided to release it here.
wrapper with between function:
This is for basically making HTTP web bots, can be used to login sites and such.
Public Class Wrapper
Public CC As New CookieContainer()
Dim responseData As String = ""
Function WebRequest(ByRef HTML As String, URL As String, _
method As String, POSTdata As String, _
Referer As String) As String
Dim hwrequest As Net.HttpWebRequest = Net.WebRequest.Create(URL)
hwrequest.Accept = "*/*"
hwrequest.AllowAutoRedirect = True
hwrequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1" 'Emulates Firefox as Agent for stealth
hwrequest.Timeout = 60000
hwrequest.Method = method
hwrequest.KeepAlive = "True" 'Keeps Session Alive
hwrequest.CookieContainer = CC 'Holds cookies, for logins etc
hwrequest.Referer = Referer 'Simulate genuine request
If hwrequest.Method = "POST" Then
hwrequest.ContentType = "application/x-www-form-urlencoded"
Dim encoding As New System.Text.UTF8Encoding 'Use UTF8Encoding for XML requests
Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)
hwrequest.ContentLength = postByteArray.Length
Dim postStream As IO.Stream = hwrequest.GetRequestStream()
postStream.Write(postByteArray, 0, postByteArray.Length)
Dim postResponse As HttpWebResponse
postResponse = DirectCast(hwrequest.GetResponse, HttpWebResponse)
CC.Add(postResponse.Cookies) 'Adds Cookie to Container
Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
Dim responseStream As IO.StreamReader = _
responseData = responseStream.ReadToEnd()
HTML = responseData
Catch e As Exception
responseData = "An error occurred: " & e.Message
Function Between(ByVal Input As String, ByVal StrStart As String, ByVal StrEnd As String, Optional ByVal Offset As Int32 = 0) As String
If (Len(Input) = 0) Then
Dim Pos1 As Int32 = Input.IndexOf(StrStart, Offset) + Len(StrStart)
If (Pos1 <> -1) Then
Dim Pos2 As Int32 = Input.IndexOf(StrEnd, Pos1) - Pos1
If (Pos2 <> -1) Then
Return Input.Substring(Pos1, Pos2)
Cannot be used on HTTPS sites, i haven't figured out how to make a wrapper for that yet or how to decrypt the data.
Now lets pretend i'm making a login code for maybe a validation for one of my programs.
GET, Webrequest(String, URL, "GET", "", "Refferer")
in order to use the wrapper we need to declare it in our class
We would first need to declares some strings, and send a "GET" request like this:
Dim wrapper As New Wrapper
Now that is done we need to fish out the token using our between function, you would add something like this:
dim html, user, pass, token as string.
wrapper.webrequest(html,"http://somesite.com/login.php", "GET", "" ,"")
'We need to visit this page because said site has a dynamic token on page load that validates login.
Now assuming we have the user info we could log in:
token = wrapper.Between(html, "<token>", "</token>")
'Our between function grabs a string between two constants generated in a page.
Almost done, generally there is a unique string of text in a webpage when a user is logged into a site, lets pretend on the home page where you login there is a link to a user control panel that isn't there when logged out.:
wrapper.WebRequest(html, "http://somesite.com/login.php", "POST", "token=" & token & "user=" & user & "pass=" & pass,"")
'use & to put together text and strings, Also use post to post Data.
wrapper.WebRequest(html, "http://somesite.com/Index.php", "GET", "", "")
If InStr(html, "Control Panel") Then
'This tells us if the login was a success.