» Site Navigation | | | » Advertisement | | | » Recent Threads | | | | | | | | | | | |  |  | [VB.NET] HTTPWrapper |  |
11-09-2008, 11:39 PM
|
#1 (permalink)
| Banned
Join Date: Apr 2008 Posts: 178
GPoints: 693 Rep Power: 0 | [VB.NET] HTTPWrapper Heres the wrapper used in the Omnipresent Autobuyer. Y'all might have some use for it...
Use: Code: Public vbWrapper(0) As Wrapper
Dim intWrapperID As Integer = lstAccounts.Items.Count
ReDim Preserve vbWrapper(intWrapperID)
Me.vbWrapper(intWrapperID) = New Wrapper() Get: Code: strHTML = Me.vbWrapper(intWrapperID).Request("GET", "http://www.neopets.com/", "") Post: Code: strHTML = Me.vbWrapper(intWrapperID).Request("POST", "http://www.neopets.com/hi.phtml?destination=%2Fpetcentral.phtml&username=" & strUsername, "http://www.neopets.com/loginpage.phtml") Code: 'TcpClient Wrapper by Robawtic edited by leptoon
Imports System.Text.RegularExpressions
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.IO.Compression
Imports System.Runtime.Serialization.Formatters.Binary
Public Class Wrapper
Inherits System.Windows.Forms.UserControl
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SuspendLayout()
'
'Wrapper
'
Me.Name = "Wrapper"
Me.ResumeLayout(False)
End Sub
#End Region
Private colCookies As New Collection
Dim strCookies As String = Nothing
Public LastPage As String
Public ProxyHost As String
Public ProxyPort As String
Public strPost As String = Nothing
Dim strHTML As String = Nothing
Public Function Request(ByVal Method As String, ByVal URL As String, ByVal Referer As String) As String
Dim Host As String = Nothing
Dim strFile As String = Nothing
Dim pos As Integer = 0
If Referer Is Nothing Then
Referer = LastPage
End If
If URL.Contains("http://") Then
Host = URL.Substring(7)
Else
Host = URL
End If
If Host.Contains("/") Then
pos = Host.IndexOf("/", 0)
strFile = Host.Substring(pos)
Host = Host.Substring(0, pos)
Else
strFile = "/"
End If
If Method = "POST" Then
pos = strFile.IndexOf("?")
If Not (pos = -1) Then
strPost = strFile.Substring(pos + 1)
strFile = strFile.Substring(0, pos)
Else
strPost = Nothing
End If
End If
LastPage = URL
Dim ReqHeaders As String
If Method = "GET" OrElse Method = "PIC" Then
ReqHeaders = "GET" + " " + strFile + " HTTP/1.1" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Host: " + Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Language: en-us,en;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Encoding: gzip, deflate" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Keep-Alive: 300" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: keep-alive" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Referer: " + Referer + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Cookie: " + strCookies + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & ""
Else
ReqHeaders = "POST" + " " + strFile + " HTTP/1.1" & Microsoft.VisualBasic.Chr(13) & Microsoft.VisualBasic.Chr(10) & "" + "Host: " + Host + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Language: en-us,en;q=0.5" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Encoding: gzip, deflate" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Keep-Alive: 300" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Connection: keep-alive" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Referer: " + Referer + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Cookie: " + strCookies + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Content-Type: application/x-www-form-urlencoded" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "Content-Length: 0" & Microsoft.VisualBasic.Chr(13) & Microsoft.VisualBasic.Chr(10) & "" + "Connection: close" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + strPost
End If
If Method = "PIC" Then
ReqHeaders.Replace("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept: image/png,*/*;q=0.5")
End If
Dim tcp As New System.Net.Sockets.TcpClient
Dim netstream As System.Net.Sockets.NetworkStream
Dim TN(1) As Long
If Referer = "" Then Referer = LastPage
If InStr(1, URL, "http://") <> 0 Then Host = Mid$(URL, 8)
If InStr(1, Host, "/") <> 0 Then Host = Mid$(Host, 1, InStr(1, Host, "/") - 1)
If InStr(1, Host, "?") <> 0 Then Host = Mid$(Host, 1, InStr(1, Host, "?") - 1)
'If Mid$(Host, 1, 4) = "www." Then Host = Mid$(Host, 5)
LastPage = URL
Try
If ProxyPort = "" And ProxyHost = "" Then
tcp.Connect(Host, 80) 'edited for proxy support
Else
tcp.Connect(ProxyHost, ProxyPort)
End If
Catch ex As Exception
Return ex.Message
End Try
Dim sendbytes As Byte()
sendbytes = System.Text.Encoding.ASCII.GetBytes(ReqHeaders)
netstream = tcp.GetStream()
netstream.Write(sendbytes, 0, sendbytes.Length)
Dim sr As StreamReader = New StreamReader(netstream, Encoding.Default)
Dim strHTML As String = sr.ReadToEnd
Dim strParts As String() = Regex.Split(strHTML, Environment.NewLine + Environment.NewLine)
strCookies = ParseCookies(strParts(0))
If strParts(0).Contains("Content-Encoding") Then
strParts(1) = DecompressGzip(strParts(1))
End If
Return strParts(0) + Environment.NewLine + Environment.NewLine + strParts(1)
End Function
Public Function DecompressGzip(ByVal compressed As String) As String
Dim memStream As MemoryStream = New MemoryStream(System.Text.Encoding.Default.GetBytes(compressed))
Dim decompressStream As GZipStream = New GZipStream(memStream, CompressionMode.Decompress)
Dim endBytes(4) As Byte
Dim position As Integer = CType(memStream.Length, Integer) - 4
memStream.Position = position
memStream.Read(endBytes, 0, 4)
memStream.Position = 0
Dim buffer(BitConverter.ToInt32(endBytes, 0) + 100) As Byte
Dim offset As Integer = 0
Dim total As Integer = 0
While True
Dim bytesRead As Integer = decompressStream.Read(buffer, offset, 100)
If bytesRead = 0 Then
Exit While
End If
offset += bytesRead
total += bytesRead
End While
Return Encoding.ASCII.GetString(buffer)
End Function
Public Function ParseCookies(ByVal Headers As String) As String
ParseCookies = ""
Dim reg As Regex
Dim matches As MatchCollection
Dim match As Match
reg = New Regex("set-cookie:\s*([^=]+)=([^;]+);", RegexOptions.IgnoreCase)
If reg.IsMatch(Headers) Then
matches = reg.Matches(Headers)
For Each match In matches
Try
colCookies.Add(match.Groups(1).ToString & "=" & match.Groups(2).ToString, match.Groups(1).ToString)
Catch ex As Exception
colCookies.Remove(match.Groups(1).ToString)
colCookies.Add(match.Groups(1).ToString & "=" & match.Groups(2).ToString, match.Groups(1).ToString)
End Try
Next
End If
Dim i As Long
For i = 1 To colCookies.Count Step 1
ParseCookies = ParseCookies & colCookies.Item(i).ToString & ";"
Next
End Function
Public Function StripHeaders(ByVal strSource As String) As String
Dim strParts() As String = Regex.Split(strSource, Environment.NewLine + Environment.NewLine + "([a-zA-Z0-9]*)" + Environment.NewLine)
Return strParts(2)
End Function
Private Shared Sub Pause(ByVal seconds As Double)
Dim num As Double = seconds * 1000
Dim t1 As DateTime = DateTime.Now
Dim t2 As DateTime = DateTime.Now
Dim tmDiff As TimeSpan = t2 - t1
While Convert.ToDouble(tmDiff.TotalMilliseconds.ToString) < num
t2 = DateTime.Now
tmDiff = t2 - t1
Application.DoEvents()
End While
End Sub
Public Function GrabPic(ByVal strURL As String) As System.Drawing.Image
Dim memStream As New MemoryStream(System.Text.Encoding.Default.GetBytes(StripHeaders(Request("GET", strURL, LastPage))))
GrabPic = Image.FromStream(memStream)
Return GrabPic
End Function
Public Sub ClearCookies()
colCookies.Clear()
strCookies = Nothing
End Sub
End Class +Rep if it helped, please.
Last edited by leptoon; 11-09-2008 at 11:56 PM..
| |
| | | The Following 3 Users Say Thank You to leptoon For This Useful Post: | |
09-23-2009, 10:05 AM
|
#2 (permalink)
| Full Member
Join Date: Apr 2009 Location: Costa Rica Posts: 144
GPoints: 2,005 Rep Power: 2 | can you send me this but for gaiaonline in a pm please thanks
__________________ HTML Code: http://ozzy12323.elbruto.es/ | |
| |  | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | |