Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.97 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Net
  3. Imports System.Security
  4. Imports System.Security.Cryptography.X509Certificates
  5. Imports System.Text
  6.  
  7. Namespace Utility
  8. Public Class Http
  9.  
  10. 'Private CookieJar As New CookieCollection
  11.  
  12. #Region "Properties"
  13. Private _Cookies As New CookieContainer
  14. Public Property Cookies() As CookieContainer
  15. Get
  16. Return _Cookies
  17. End Get
  18. Set(ByVal value As CookieContainer)
  19. _Cookies = value
  20. End Set
  21. End Property
  22.  
  23. Private _Proxy As String = String.Empty
  24. Public Property Proxy() As String
  25. Get
  26. Return _Proxy
  27. End Get
  28. Set(ByVal value As String)
  29. _Proxy = value
  30. End Set
  31. End Property
  32.  
  33. Private _TimeOut As Integer = 7000
  34. Public Property TimeOut() As Integer
  35. Get
  36. Return _TimeOut
  37. End Get
  38. Set(ByVal value As Integer)
  39. _TimeOut = value
  40. End Set
  41. End Property
  42.  
  43. 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"
  44. Public Property Useragent() As String
  45. Get
  46. Return _Useragent
  47. End Get
  48. Set(ByVal value As String)
  49. _Useragent = value
  50. End Set
  51. End Property
  52.  
  53. Private _Referer As String = String.Empty
  54. Public Property Referer() As String
  55. Get
  56. Return _Referer
  57. End Get
  58. Set(ByVal value As String)
  59. _Referer = value
  60. End Set
  61. End Property
  62. #End Region
  63.  
  64. Private Request As HttpWebRequest = Nothing
  65. Private Requesting As Boolean = False
  66.  
  67. Private Const newLine As String = vbCr & vbLf
  68.  
  69. Private Accept As String = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  70. Private AcceptLanguage As String = "en-us,en;q=0.5"
  71.  
  72. Private Shared rnd As New Random(Environment.TickCount)
  73.  
  74. Public UploadData As New mUploadData
  75. Structure mUploadData
  76. Private uContents As Byte()
  77. Private uFileName As String
  78. Private uFieldName As String
  79. Public Property Contents() As Byte()
  80. Get
  81. Return uContents
  82. End Get
  83. Set(ByVal value As Byte())
  84. uContents = value
  85. End Set
  86. End Property
  87. Public Property FileName() As String
  88. Get
  89. Return uFileName
  90. End Get
  91. Set(ByVal value As String)
  92. uFileName = value
  93. End Set
  94. End Property
  95. Public Property FieldName() As String
  96. Get
  97. Return uFieldName
  98. End Get
  99. Set(ByVal value As String)
  100. uFieldName = value
  101. End Set
  102. End Property
  103. End Structure
  104.  
  105. Public Sub New()
  106. System.Net.ServicePointManager.Expect100Continue = False
  107. System.Net.ServicePointManager.DefaultConnectionLimit = 150
  108. ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf CertificateValidationCallBack)
  109. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
  110. End Sub
  111.  
  112. Public Sub CancelRequest()
  113. Try
  114. If Requesting = True Then Request.Abort()
  115. Catch ex As Exception
  116. Debug.Print(ex.ToString)
  117. End Try
  118. End Sub
  119.  
  120. Public Function GetResponse(ByVal Uri As String, Optional ByVal PostData As String = "") As Object
  121. Try
  122. Requesting = True
  123. Request = DirectCast(WebRequest.Create(New Uri(Uri)), HttpWebRequest)
  124. With Request
  125.  
  126. .Method = IIf(String.IsNullOrEmpty(PostData), "GET", "POST")
  127. .UserAgent = Useragent
  128. .Timeout = TimeOut
  129. .AllowAutoRedirect = True
  130. .Referer = Referer
  131.  
  132. .Accept = Accept
  133. .KeepAlive = True
  134. .AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip
  135. .Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
  136. .Headers.Add("Accept-Language: " & AcceptLanguage)
  137.  
  138. .Proxy = Nothing
  139. If Not String.IsNullOrEmpty(Proxy) Then
  140. Dim pData() As String = Split(Proxy, ":")
  141. Dim myProxy As New WebProxy(pData(0), Convert.ToInt32(pData(1)))
  142. If pData.Count > 2 Then myProxy.Credentials = New NetworkCredential(pData(2), pData(3))
  143. .Proxy = myProxy
  144. End If
  145.  
  146. .CookieContainer = Me.Cookies
  147. '.CookieContainer = AddCookies
  148.  
  149. If Not String.IsNullOrEmpty(PostData) Then
  150. Dim PostDataBytes As Byte() = Encoding.UTF8.GetBytes(PostData)
  151. .ContentType = "application/x-www-form-urlencoded"
  152. .ContentLength = PostDataBytes.Length
  153. Dim RequestStream As Stream = .GetRequestStream()
  154. RequestStream.Write(PostDataBytes, 0, PostDataBytes.Length)
  155. End If
  156.  
  157. 'For Each c As Cookie In DirectCast(.GetResponse, HttpWebResponse).Cookies
  158. ' Debug.Print("Cookie: " & c.Name & " - " & c.Value & " - " & c.Domain & " - " & c.Path & vbCrLf)
  159. ' CookieJar.Add(New Cookie(c.Name, c.Value, "/", c.Domain))
  160. 'Next
  161.  
  162. Return .GetResponse
  163. End With
  164. Catch we As WebException
  165. Return we
  166. Catch ex As Exception
  167. Return ex
  168. Finally
  169. Referer = String.Empty
  170. Requesting = False
  171. Request = Nothing
  172. End Try
  173. End Function
  174.  
  175. Public Function ProcessResponse(ByVal Response As System.Net.HttpWebResponse, Optional ByVal AddHeaders As Boolean = True) As String
  176. Dim Html As StringBuilder = New StringBuilder
  177.  
  178. With Response
  179. 'Me.Cookies.Add(.Cookies) ' TODO: How is it possible that cookies are being stored when they aren't being adding to the container?
  180.  
  181. Dim receiveStream As Stream = .GetResponseStream()
  182. Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
  183. Dim readStream As New StreamReader(receiveStream, encode)
  184.  
  185. Dim read(256) As [Char]
  186. Dim count As Integer = readStream.Read(read, 0, 256)
  187.  
  188. If AddHeaders = True Then Html.Append(.Headers.ToString)
  189. While count > 0
  190. Dim str As New [String](read, 0, count)
  191. Html.Append(str)
  192. count = readStream.Read(read, 0, 256)
  193. End While
  194. readStream.Close()
  195. .Close()
  196. End With
  197.  
  198. Return Html.ToString
  199. End Function
  200.  
  201. Public Sub ClearCookies()
  202. Me.Cookies = Nothing
  203. Me.Cookies = New CookieContainer
  204. End Sub
  205. 'Private Function AddCookies() As CookieContainer
  206. ' Dim cContainer As New CookieContainer
  207. ' cContainer.Add(CookieJar)
  208. ' CookieJar = New CookieCollection
  209. ' Return cContainer
  210. 'End Function
  211.  
  212. Public Function ParseBetween(ByVal Source As String, ByVal Before As String, ByVal After As String, ByVal Offset As Integer) As String
  213. If String.IsNullOrEmpty(Source) Then Return String.Empty
  214. If Source.Contains(Before) = True Then
  215. Dim Result As String = Source.Substring(Source.IndexOf(Before) + Offset)
  216. If Result.Contains(After) = True Then
  217. If Not String.IsNullOrEmpty(After) Then Result = Result.Substring(0, Result.IndexOf(After))
  218. End If
  219. Return Result
  220. Else
  221. Return String.Empty
  222. End If
  223. End Function
  224. Public Function ParseBetweenRev(ByVal Source As String, ByVal Before As String, ByVal After As String, ByVal Offset As Integer) As String
  225. If String.IsNullOrEmpty(Source) Then Return String.Empty
  226. If Source.Contains(Before) = True Then
  227. Dim tmp As String = Source.Substring(0, Source.IndexOf(Before))
  228. If tmp.Contains(After) = True Then
  229. If Not String.IsNullOrEmpty(After) Then
  230. Return tmp.Substring(tmp.LastIndexOf(After) + Offset)
  231. End If
  232. End If
  233. Return tmp
  234. Else
  235. Return String.Empty
  236. End If
  237. End Function
  238. Public Function ParseForms(ByVal Source As String, Optional ByVal ValueRequired As Boolean = False) As String
  239. If Not String.IsNullOrEmpty(Source) Then Return String.Empty
  240.  
  241. Dim sb As New StringBuilder()
  242. Dim tmp As String = Source
  243. Dim Part As String = String.Empty : Dim Key As String = String.Empty : Dim Value As String = String.Empty
  244.  
  245. While tmp.Contains("<input ")
  246. Part = ParseBetween(tmp, "name=""", ">", 0)
  247. Key = ParseBetween(Part, "name=""", """", 6)
  248. Value = ParseBetween(Part, "value=""", """", 7)
  249. If ValueRequired Then
  250. If Not String.IsNullOrEmpty(Value) Then sb.Append(Key & "=" & Value & "&")
  251. Else
  252. sb.Append(Key & "=" & Value & "&")
  253. End If
  254. tmp = tmp.Substring(tmp.IndexOf("name=") + 6)
  255. End While
  256. If sb.Length > 1 Then sb.Remove(sb.Length - 1, 1)
  257. Return sb.ToString()
  258. End Function
  259.  
  260. Public Function GetFormKeyAfterText(ByVal Source As String, ByVal FormType As String, ByVal Text As String) As String
  261. Dim tmp As String = Source.Substring(Source.IndexOf(Text) + Text.Length)
  262. Dim Key As String = String.Empty : Dim Value As String = String.Empty
  263. tmp = tmp.Substring(tmp.IndexOf("<" & FormType) + FormType.Length)
  264. Return ParseBetween(tmp, "name=""", """", 6)
  265. End Function
  266. Public Function GetFormAfterText(ByVal Source As String, ByVal Text As String) As String
  267. Dim tmp As String = Source.Substring(Source.IndexOf(Text) + Text.Length)
  268. Dim Key As String = String.Empty : Dim Value As String = String.Empty
  269. If tmp.ToLower().IndexOf("<input") < tmp.ToLower().IndexOf("<textarea") Then
  270. tmp = tmp.Substring(tmp.IndexOf("<input") + 5)
  271. Key = ParseBetween(tmp, "name=""", """", 6)
  272. Value = ParseBetween(tmp, "value=""", """", 7)
  273. Else
  274. tmp = tmp.Substring(tmp.IndexOf("<textarea") + 5)
  275. Key = ParseBetween(tmp, "name=""", """", 6)
  276. Value = ParseBetween(tmp, "value=""", """", 7)
  277. End If
  278. Return Key & "=" & Value
  279. End Function
  280. Public Function GetFormIdText(ByVal Source As String, ByVal Id As String) As String
  281. Dim tmp As String = Source
  282. Dim value As String = String.Empty
  283. Try
  284. tmp = tmp.Substring(tmp.IndexOf("id=""" & Id & """") + 5)
  285. value = ParseBetween(tmp, "value=""", """", 7)
  286. Catch
  287. End Try
  288. Return value
  289. End Function
  290. Public Function GetFormNameText(ByVal Source As String, ByVal Name As String) As String
  291. Dim tmp As String = Source
  292. Dim value As String = String.Empty
  293. Try
  294. tmp = tmp.Substring(tmp.IndexOf("name=""" & Name & """") + 7)
  295. value = ParseBetween(tmp, "value=""", """", 7)
  296. Catch
  297. End Try
  298. Return value
  299. End Function
  300.  
  301. Private Function RandomFromArray(ByVal obj As String()) As String
  302. Dim value As String = String.Empty
  303. SyncLock obj
  304. Dim rndValue As Integer = rnd.[Next](0, obj.Length)
  305. value = DirectCast(obj(rndValue), String)
  306. End SyncLock
  307. Return value
  308. End Function
  309.  
  310. Function CertificateValidationCallBack(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As Security.SslPolicyErrors) As Boolean
  311. Return True
  312. End Function
  313. End Class
  314. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement