Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports System.Net
- Imports System.Security
- Imports System.Security.Cryptography.X509Certificates
- Imports System.Text
- Namespace Utility
- Public Class Http
- 'Private CookieJar As New CookieCollection
- #Region "Properties"
- Private _Cookies As New CookieContainer
- Public Property Cookies() As CookieContainer
- Get
- Return _Cookies
- End Get
- Set(ByVal value As CookieContainer)
- _Cookies = value
- End Set
- End Property
- Private _Proxy As String = String.Empty
- Public Property Proxy() As String
- Get
- Return _Proxy
- End Get
- Set(ByVal value As String)
- _Proxy = value
- End Set
- End Property
- Private _TimeOut As Integer = 7000
- Public Property TimeOut() As Integer
- Get
- Return _TimeOut
- End Get
- Set(ByVal value As Integer)
- _TimeOut = value
- End Set
- End Property
- Private _Useragent As String = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16"
- Public Property Useragent() As String
- Get
- Return _Useragent
- End Get
- Set(ByVal value As String)
- _Useragent = value
- End Set
- End Property
- Private _Referer As String = String.Empty
- Public Property Referer() As String
- Get
- Return _Referer
- End Get
- Set(ByVal value As String)
- _Referer = value
- End Set
- End Property
- #End Region
- Private Request As HttpWebRequest = Nothing
- Private Requesting As Boolean = False
- Private Const newLine As String = vbCr & vbLf
- Private Accept As String = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
- Private AcceptLanguage As String = "en-us,en;q=0.5"
- Private Shared rnd As New Random(Environment.TickCount)
- Public UploadData As New mUploadData
- Structure mUploadData
- Private uContents As Byte()
- Private uFileName As String
- Private uFieldName As String
- Public Property Contents() As Byte()
- Get
- Return uContents
- End Get
- Set(ByVal value As Byte())
- uContents = value
- End Set
- End Property
- Public Property FileName() As String
- Get
- Return uFileName
- End Get
- Set(ByVal value As String)
- uFileName = value
- End Set
- End Property
- Public Property FieldName() As String
- Get
- Return uFieldName
- End Get
- Set(ByVal value As String)
- uFieldName = value
- End Set
- End Property
- End Structure
- Public Sub New()
- System.Net.ServicePointManager.Expect100Continue = False
- System.Net.ServicePointManager.DefaultConnectionLimit = 150
- ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf CertificateValidationCallBack)
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
- End Sub
- Public Sub CancelRequest()
- Try
- If Requesting = True Then Request.Abort()
- Catch ex As Exception
- Debug.Print(ex.ToString)
- End Try
- End Sub
- Public Function GetResponse(ByVal Uri As String, Optional ByVal PostData As String = "") As Object
- Try
- Requesting = True
- Request = DirectCast(WebRequest.Create(New Uri(Uri)), HttpWebRequest)
- With Request
- .Method = IIf(String.IsNullOrEmpty(PostData), "GET", "POST")
- .UserAgent = Useragent
- .Timeout = TimeOut
- .AllowAutoRedirect = True
- .Referer = Referer
- .Accept = Accept
- .KeepAlive = True
- .AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip
- .Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
- .Headers.Add("Accept-Language: " & AcceptLanguage)
- .Proxy = Nothing
- If Not String.IsNullOrEmpty(Proxy) Then
- Dim pData() As String = Split(Proxy, ":")
- Dim myProxy As New WebProxy(pData(0), Convert.ToInt32(pData(1)))
- If pData.Count > 2 Then myProxy.Credentials = New NetworkCredential(pData(2), pData(3))
- .Proxy = myProxy
- End If
- .CookieContainer = Me.Cookies
- '.CookieContainer = AddCookies
- If Not String.IsNullOrEmpty(PostData) Then
- Dim PostDataBytes As Byte() = Encoding.UTF8.GetBytes(PostData)
- .ContentType = "application/x-www-form-urlencoded"
- .ContentLength = PostDataBytes.Length
- Dim RequestStream As Stream = .GetRequestStream()
- RequestStream.Write(PostDataBytes, 0, PostDataBytes.Length)
- End If
- 'For Each c As Cookie In DirectCast(.GetResponse, HttpWebResponse).Cookies
- ' Debug.Print("Cookie: " & c.Name & " - " & c.Value & " - " & c.Domain & " - " & c.Path & vbCrLf)
- ' CookieJar.Add(New Cookie(c.Name, c.Value, "/", c.Domain))
- 'Next
- Return .GetResponse
- End With
- Catch we As WebException
- Return we
- Catch ex As Exception
- Return ex
- Finally
- Referer = String.Empty
- Requesting = False
- Request = Nothing
- End Try
- End Function
- Public Function ProcessResponse(ByVal Response As System.Net.HttpWebResponse, Optional ByVal AddHeaders As Boolean = True) As String
- Dim Html As StringBuilder = New StringBuilder
- With Response
- 'Me.Cookies.Add(.Cookies) ' TODO: How is it possible that cookies are being stored when they aren't being adding to the container?
- Dim receiveStream As Stream = .GetResponseStream()
- Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
- Dim readStream As New StreamReader(receiveStream, encode)
- Dim read(256) As [Char]
- Dim count As Integer = readStream.Read(read, 0, 256)
- If AddHeaders = True Then Html.Append(.Headers.ToString)
- While count > 0
- Dim str As New [String](read, 0, count)
- Html.Append(str)
- count = readStream.Read(read, 0, 256)
- End While
- readStream.Close()
- .Close()
- End With
- Return Html.ToString
- End Function
- Public Sub ClearCookies()
- Me.Cookies = Nothing
- Me.Cookies = New CookieContainer
- End Sub
- 'Private Function AddCookies() As CookieContainer
- ' Dim cContainer As New CookieContainer
- ' cContainer.Add(CookieJar)
- ' CookieJar = New CookieCollection
- ' Return cContainer
- 'End Function
- Public Function ParseBetween(ByVal Source As String, ByVal Before As String, ByVal After As String, ByVal Offset As Integer) As String
- If String.IsNullOrEmpty(Source) Then Return String.Empty
- If Source.Contains(Before) = True Then
- Dim Result As String = Source.Substring(Source.IndexOf(Before) + Offset)
- If Result.Contains(After) = True Then
- If Not String.IsNullOrEmpty(After) Then Result = Result.Substring(0, Result.IndexOf(After))
- End If
- Return Result
- Else
- Return String.Empty
- End If
- End Function
- Public Function ParseBetweenRev(ByVal Source As String, ByVal Before As String, ByVal After As String, ByVal Offset As Integer) As String
- If String.IsNullOrEmpty(Source) Then Return String.Empty
- If Source.Contains(Before) = True Then
- Dim tmp As String = Source.Substring(0, Source.IndexOf(Before))
- If tmp.Contains(After) = True Then
- If Not String.IsNullOrEmpty(After) Then
- Return tmp.Substring(tmp.LastIndexOf(After) + Offset)
- End If
- End If
- Return tmp
- Else
- Return String.Empty
- End If
- End Function
- Public Function ParseForms(ByVal Source As String, Optional ByVal ValueRequired As Boolean = False) As String
- If Not String.IsNullOrEmpty(Source) Then Return String.Empty
- Dim sb As New StringBuilder()
- Dim tmp As String = Source
- Dim Part As String = String.Empty : Dim Key As String = String.Empty : Dim Value As String = String.Empty
- While tmp.Contains("<input ")
- Part = ParseBetween(tmp, "name=""", ">", 0)
- Key = ParseBetween(Part, "name=""", """", 6)
- Value = ParseBetween(Part, "value=""", """", 7)
- If ValueRequired Then
- If Not String.IsNullOrEmpty(Value) Then sb.Append(Key & "=" & Value & "&")
- Else
- sb.Append(Key & "=" & Value & "&")
- End If
- tmp = tmp.Substring(tmp.IndexOf("name=") + 6)
- End While
- If sb.Length > 1 Then sb.Remove(sb.Length - 1, 1)
- Return sb.ToString()
- End Function
- Public Function GetFormKeyAfterText(ByVal Source As String, ByVal FormType As String, ByVal Text As String) As String
- Dim tmp As String = Source.Substring(Source.IndexOf(Text) + Text.Length)
- Dim Key As String = String.Empty : Dim Value As String = String.Empty
- tmp = tmp.Substring(tmp.IndexOf("<" & FormType) + FormType.Length)
- Return ParseBetween(tmp, "name=""", """", 6)
- End Function
- Public Function GetFormAfterText(ByVal Source As String, ByVal Text As String) As String
- Dim tmp As String = Source.Substring(Source.IndexOf(Text) + Text.Length)
- Dim Key As String = String.Empty : Dim Value As String = String.Empty
- If tmp.ToLower().IndexOf("<input") < tmp.ToLower().IndexOf("<textarea") Then
- tmp = tmp.Substring(tmp.IndexOf("<input") + 5)
- Key = ParseBetween(tmp, "name=""", """", 6)
- Value = ParseBetween(tmp, "value=""", """", 7)
- Else
- tmp = tmp.Substring(tmp.IndexOf("<textarea") + 5)
- Key = ParseBetween(tmp, "name=""", """", 6)
- Value = ParseBetween(tmp, "value=""", """", 7)
- End If
- Return Key & "=" & Value
- End Function
- Public Function GetFormIdText(ByVal Source As String, ByVal Id As String) As String
- Dim tmp As String = Source
- Dim value As String = String.Empty
- Try
- tmp = tmp.Substring(tmp.IndexOf("id=""" & Id & """") + 5)
- value = ParseBetween(tmp, "value=""", """", 7)
- Catch
- End Try
- Return value
- End Function
- Public Function GetFormNameText(ByVal Source As String, ByVal Name As String) As String
- Dim tmp As String = Source
- Dim value As String = String.Empty
- Try
- tmp = tmp.Substring(tmp.IndexOf("name=""" & Name & """") + 7)
- value = ParseBetween(tmp, "value=""", """", 7)
- Catch
- End Try
- Return value
- End Function
- Private Function RandomFromArray(ByVal obj As String()) As String
- Dim value As String = String.Empty
- SyncLock obj
- Dim rndValue As Integer = rnd.[Next](0, obj.Length)
- value = DirectCast(obj(rndValue), String)
- End SyncLock
- Return value
- End Function
- Function CertificateValidationCallBack(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As Security.SslPolicyErrors) As Boolean
- Return True
- End Function
- End Class
- End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement