Advertisement
Guest User

httpwrapper vb.net

a guest
Oct 7th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.93 KB | None | 0 0
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.Text
  4. Imports System.Text.RegularExpressions
  5. Imports System.IO
  6. Imports System.IO.Compression
  7. Imports System.Net.Sockets
  8.  
  9. Public Class httpwrapper
  10. 'Httpwrapper Credits to glurak and whoever converted this to vb.net
  11. 'This is not my work except for some slight chagnges and tweaks where i see fit
  12. Implements ICloneable
  13.  
  14. Private TCP_Client As TcpClient
  15. Private colCookies As Dictionary(Of String, String) = New Dictionary(Of String, String)
  16. Public strCookies As String = String.Empty
  17. Public LastPage As String = String.Empty
  18.  
  19. Private pUseProxy As Boolean = False
  20. Private pProxyAddress As String = String.Empty
  21. Private pProxyPort As Integer = 80
  22.  
  23. Public Const constHeaderUserAgent As String = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"
  24.  
  25. Public headerAccept As String = "text/html,application/xhtml+xml,application/xml,0.9,*/*;q=0.8"
  26. Public headerUserAgent As String = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"
  27. Public headerAcceptLanguage As String = "en-us,en;q=0.5"
  28. Public headerAcceptCharset As String = "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
  29. Public alternativePostdataSeparator As String = "ยต"
  30. Public intMaxSendTime As Integer = 3000
  31. Public intMaxReceiveTime As Integer = 3000
  32. Public ExceptionCatcher As ExceptionCatcherSub = Nothing
  33.  
  34. Public Delegate Sub ExceptionCatcherSub(ByVal sender As Object, ByVal Ex As Exception)
  35.  
  36. Public Sub New()
  37. 'Do nothing :)
  38. End Sub
  39.  
  40. Public Sub New(ByVal ExCatcher As ExceptionCatcherSub)
  41. ExceptionCatcher = ExCatcher
  42. End Sub
  43.  
  44. Public Function Request(ByVal Method As String, ByVal URL As String, Optional ByVal Referer As String = "") As String
  45. Try
  46. Dim Host As String = String.Empty
  47. Dim ReqHeaders As String = String.Empty
  48.  
  49. Call PrepRequest(Method, URL, Referer, strCookies, headerUserAgent, Host, ReqHeaders)
  50.  
  51. TCP_Client = New TcpClient
  52. TCP_Client.SendTimeout = intMaxSendTime
  53. TCP_Client.ReceiveTimeout = intMaxReceiveTime
  54. If pUseProxy Then
  55. TCP_Client.Connect(Host, pProxyPort)
  56. Else
  57. TCP_Client.Connect(Host, 80)
  58. End If
  59. Dim headers As Byte() = System.Text.Encoding.GetEncoding(1252).GetBytes(ReqHeaders)
  60. Dim ns As NetworkStream = TCP_Client.GetStream()
  61.  
  62. ns.Write(headers, 0, headers.Length)
  63. Dim sr As StreamReader = New StreamReader(ns, Encoding.Default)
  64. Dim strReadHTML As String = sr.ReadToEnd()
  65. sr.Close()
  66. ns.Close()
  67. TCP_Client.Close()
  68.  
  69. If strReadHTML.IndexOf(vbCrLf & vbCrLf) > 0 Then
  70. Dim strParts() As String = Split(strReadHTML, vbCrLf & vbCrLf, 2)
  71. strCookies = ParseCookies(strParts(0))
  72. If strParts(0).Contains("Content-Encoding") And Not Method = "HEAD" Then
  73. strParts(1) = DecompressGzip(strParts(1))
  74. End If
  75. Dim theret As String
  76. For i = 0 To strParts.Count - 1
  77. theret = theret & strParts(i).ToString
  78.  
  79. Next i
  80.  
  81. Return theret
  82. Else
  83.  
  84. Return strReadHTML
  85. End If
  86. Catch ex As Exception
  87. ExceptionHandler(ex)
  88. Return "-1" 'Error just return -1
  89. End Try
  90. End Function
  91.  
  92. Private Function PrepRequest(ByVal Method As String, ByVal URL As String, ByVal Referer As String, ByVal strCookiesToUse As String, ByVal strUserAgentToUse As String, ByRef strHost As String, ByRef strReqHeaders As String)
  93. Dim Host As String = String.Empty
  94. Dim strFile As String = String.Empty
  95. Dim strPost As String = String.Empty
  96. Dim intPos As Integer = 0
  97. Dim strReqContent As String = String.Empty
  98.  
  99. If Referer = String.Empty Then
  100. Referer = LastPage
  101. ElseIf Referer = "" Then
  102. Referer = String.Empty
  103. End If
  104.  
  105. If URL.ToLower().Contains("http://") Then
  106. Host = URL.Substring(7)
  107. Else
  108. Host = URL
  109. End If
  110.  
  111. If Host.Contains("/") Then
  112. intPos = Host.IndexOf("/", 0)
  113. strFile = Host.Substring(intPos)
  114. Host = Host.Substring(0, intPos)
  115. Else
  116. strFile = "/"
  117. End If
  118.  
  119. If Method = "POST" Then
  120. intPos = strFile.IndexOf(alternativePostdataSeparator)
  121. If intPos <> -1 Then
  122. strPost = strFile.Substring(intPos + 1)
  123. strFile = strFile.Substring(0, intPos)
  124. Else
  125. intPos = strFile.IndexOf("?")
  126. If intPos <> -1 Then
  127. strPost = strFile.Substring(intPos + 1)
  128. strFile = strFile.Substring(0, intPos)
  129. Else
  130. strPost = ""
  131. End If
  132. End If
  133.  
  134. ElseIf Method = "AMF" Then
  135. intPos = strFile.IndexOf(alternativePostdataSeparator)
  136. If intPos <> -1 Then
  137. strPost = strFile.Substring(intPos + 1)
  138. strFile = strFile.Substring(0, intPos)
  139. Else
  140. intPos = strFile.IndexOf("?")
  141. If intPos <> -1 Then
  142. strPost = strFile.Substring(intPos + 1)
  143. strFile = strFile.Substring(0, intPos)
  144. Else
  145. strPost = ""
  146. End If
  147. End If
  148.  
  149. ElseIf Not Method = "HEAD" Then
  150. Method = "GET"
  151. End If
  152.  
  153. If pUseProxy Then
  154. strFile = "http://" & Host & strFile
  155. Host = pProxyAddress
  156. End If
  157.  
  158. LastPage = URL
  159.  
  160. If Method = "POST" Then
  161. strReqContent = "Content-type: application/x-www-form-urlencoded" & vbCrLf & "Content-length: " & strPost.Length.ToString() & vbCrLf & vbCrLf & strPost
  162. ElseIf Method = "AMF" Then
  163. strReqContent = "Content-Type: application/x-amf" & vbCrLf & "Content-length: " & strPost.Length.ToString() & vbCrLf & vbCrLf & strPost
  164. Else
  165. strReqContent = vbCrLf
  166. End If
  167.  
  168. If strUserAgentToUse = String.Empty Then
  169. strUserAgentToUse = constHeaderUserAgent
  170. End If
  171. If Method = "AMF" Then
  172. strReqHeaders = "POST" & " " & strFile & " HTTP/1.1" & vbCrLf & _
  173. "Host: " & Host & vbCrLf & _
  174. "User-Agent: " & headerUserAgent & vbCrLf & _
  175. "Accept: " & headerAccept & vbCrLf & _
  176. "Accept-Language: " & headerAcceptLanguage & vbCrLf & _
  177. "Accept-Encoding: gzip, deflate" & vbCrLf & _
  178. "Accept-Charset: " & headerAcceptCharset & vbCrLf & _
  179. "Connection: close" & vbCrLf & _
  180. If(Not Referer = String.Empty, "Referer: " & Referer & vbCrLf, "") & _
  181. "Cookie: " & strCookies & vbCrLf & _
  182. strReqContent
  183. Else
  184. strReqHeaders = Method & " " & strFile & " HTTP/1.1" & vbCrLf & _
  185. "Host: " & Host & vbCrLf & _
  186. "User-Agent: " & headerUserAgent & vbCrLf & _
  187. "Accept: " & headerAccept & vbCrLf & _
  188. "Accept-Language: " & headerAcceptLanguage & vbCrLf & _
  189. "Accept-Encoding: gzip, deflate" & vbCrLf & _
  190. "Accept-Charset: " & headerAcceptCharset & vbCrLf & _
  191. "Connection: close" & vbCrLf & _
  192. If(Not Referer = String.Empty, "Referer: " & Referer & vbCrLf, "") & _
  193. "Cookie: " & strCookies & vbCrLf & _
  194. strReqContent
  195. End If
  196.  
  197.  
  198. strHost = Host
  199. Return Nothing
  200. End Function
  201. Private Function DecompressGzip(ByVal Compressed As String) As String
  202. Dim memStream As MemoryStream = New MemoryStream(Encoding.Default.GetBytes(Compressed))
  203. Dim decompressStream As GZipStream = New GZipStream(memStream, CompressionMode.Decompress)
  204.  
  205. Dim endBytes As Byte()
  206. ReDim endBytes(4)
  207. Dim intPosition As Integer = memStream.Length - 4
  208. memStream.Position = intPosition
  209. memStream.Read(endBytes, 0, 4)
  210. memStream.Position = 0
  211. Dim buffer As Byte()
  212. ReDim buffer(BitConverter.ToInt32(endBytes, 0) + 100)
  213. Dim intOffset As Integer = 0
  214. Dim intTotal As Integer = 0
  215. While (True)
  216. Dim intO As Integer = decompressStream.Read(buffer, intOffset, 100)
  217. If intO = 0 Then
  218. Exit While
  219. End If
  220. intOffset += intO
  221. intTotal += intO
  222. End While
  223. Return Encoding.Default.GetString(buffer)
  224. End Function
  225.  
  226. Public Property UseProxy() As Boolean
  227. Get
  228. Return pUseProxy
  229. End Get
  230. Set(ByVal value As Boolean)
  231. pUseProxy = value
  232. End Set
  233. End Property
  234.  
  235. Public Property ProxyAddress() As String
  236. Get
  237. Return pProxyAddress & ":" & pProxyPort
  238. End Get
  239. Set(ByVal value As String)
  240. Dim strParts() As String = Split(value, ":", 2)
  241. If UBound(strParts) = 1 Then
  242. pProxyAddress = strParts(0)
  243. pProxyPort = Val(strParts(1))
  244. End If
  245. End Set
  246. End Property
  247.  
  248. Public Function StripHeaders(ByVal strSource As String) As String
  249. Return Split(strSource, vbCrLf & vbCrLf, 2)(1)
  250. End Function
  251.  
  252. Public Function getimage(ByVal strURL As String, Optional ByVal strReferer As String = "") As Bitmap
  253. Try
  254. Dim strSource As String = String.Empty
  255. strSource = Request("GET", strURL, strReferer)
  256. Dim memStream As MemoryStream = New MemoryStream(Encoding.Default.GetBytes(StripHeaders(strSource)))
  257. Dim bMap As Bitmap = New Bitmap(memStream)
  258. Return bMap
  259. Catch ex As Exception
  260. 'Return an empty image.. An empty image is better than no image, right :P ?
  261. ExceptionHandler(ex)
  262. Return New Bitmap(0, 0)
  263. End Try
  264.  
  265. End Function
  266.  
  267. Public Sub ClearCookies()
  268. colCookies.Clear()
  269. strCookies = String.Empty
  270. End Sub
  271.  
  272. Public Sub AppendCookies(ByVal strCooksToAppend As String)
  273. Dim strCooks() As String = Split(strCooksToAppend, ";")
  274. For Each strCook As String In strCooks
  275. If strCook.IndexOf("=") > -1 Then
  276. strCookies = ParseCookies("set-cookie: " & strCook & ";")
  277. End If
  278. Next
  279. End Sub
  280.  
  281. Public Sub SetCookies(ByVal strCooksToSet As String)
  282. ClearCookies()
  283. AppendCookies(strCooksToSet)
  284. End Sub
  285.  
  286. Public Function GetCookies() As String
  287. Dim strCooks As String = String.Empty
  288. Try
  289. For Each colItem As KeyValuePair(Of String, String) In colCookies
  290. strCooks &= colItem.Value.ToString() & "; "
  291. Next
  292. Return strCooks.Substring(0, strCookies.Length - 1)
  293. Catch ex As Exception
  294. 'Return the cookie string already saved if paring the collection fails. Some cookies are better than none, right :D ?
  295. ExceptionHandler(ex)
  296. Return strCookies
  297. End Try
  298. End Function
  299.  
  300. Public Function ParseCookies(ByVal strHeaders As String) As String
  301. Try
  302. Dim ParseCooks As String = String.Empty
  303. Dim regMatches As MatchCollection
  304. Dim regX As Regex = New Regex("set-cookie:\s*([^=]+)=([^;]+);", RegexOptions.IgnoreCase)
  305. regMatches = regX.Matches(strHeaders)
  306. If regMatches.Count > 0 Then
  307. For Each regM As Match In regMatches
  308. If colCookies.ContainsKey(regM.Groups(1).ToString()) Then
  309. colCookies.Remove(regM.Groups(1).ToString())
  310. End If
  311. colCookies.Add(regM.Groups(1).ToString(), regM.Groups(1).ToString() & "=" & regM.Groups(2).ToString())
  312. Next
  313. End If
  314. For Each colItem As KeyValuePair(Of String, String) In colCookies
  315. ParseCooks &= colItem.Value.ToString() & "; "
  316. Next
  317. Return ParseCooks
  318. Catch ex As Exception
  319. 'Return the cookie string already saved if paring the collection fails. Some cookies are better than none, right :D ?
  320. ExceptionHandler(ex)
  321. Return strCookies
  322. End Try
  323. End Function
  324.  
  325. Private Sub ExceptionHandler(ByVal Ex As Exception)
  326. If Not ExceptionCatcher Is Nothing Then
  327. ExceptionCatcher.Invoke(Me, Ex)
  328. End If
  329. End Sub
  330.  
  331. Public Function Clone() As Object Implements System.ICloneable.Clone
  332. Return DirectCast(MemberwiseClone(), httpwrapper)
  333. End Function
  334. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement