I coded this a while back and decided to release it here.
wrapper with between function:
Code:
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
Try
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)
postStream.Close()
Dim postResponse As HttpWebResponse
postResponse = DirectCast(hwrequest.GetResponse, HttpWebResponse)
CC.Add(postResponse.Cookies) 'Adds Cookie to Container
End If
Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
Dim responseStream As IO.StreamReader = _
New IO.StreamReader(hwresponse.GetResponseStream())
responseData = responseStream.ReadToEnd()
HTML = responseData
End If
hwresponse.Close()
Catch e As Exception
responseData = "An error occurred: " & e.Message
End Try
Return responseData
End Function
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
Return vbNullString
End If
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)
End If
End If
Return vbNullString
End Function
End Class
This is for basically making HTTP web bots, can be used to login sites and such.
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.
Usage:
Code:
GET, Webrequest(String, URL, "GET", "", "Refferer")
Now lets pretend i'm making a login code for maybe a validation for one of my programs.
in order to use the wrapper we need to declare it in our class
Code:
Dim wrapper As New Wrapper
We would first need to declares some strings, and send a "GET" request like this:
Code:
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 that is done we need to fish out the token using our between function, you would add something like this:
Code:
token = wrapper.Between(html, "<token>", "</token>")
'Our between function grabs a string between two constants generated in a page.
Now assuming we have the user info we could log in:
Code:
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.
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.:
Code:
wrapper.WebRequest(html, "http://somesite.com/Index.php", "GET", "", "")
If InStr(html, "Control Panel") Then
MsgBox("logged in!")
Else
MsgBox("Fail!")
End If
'This tells us if the login was a success.