Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Region " Usage Examples "
- 'Public Class Form1
- ' Public GameWallpaper As New GameWallpapersAPI(username:="e@mail.com", password:="password")
- ' Dim wallIds As IEnumerable(Of Integer) = Enumerable.Range(0, 9999)
- ' Dim wallRes As GameWallpapersAPI.WallpaperResolution = GameWallpapersAPI.WallpaperResolution.Res1920x1080
- ' Dim wallInfo As GameWallpapersAPI.GameWallpaperInfo
- ' Private Sub Button1_Click() Handles Button1.Click
- ' MessageBox.Show(CStr(GameWallpaper.CheckLogin), "Account Login success?",
- ' MessageBoxButtons.OK, MessageBoxIcon.Information)
- ' Dim outputFolder As String = "C:\GameWallpapers"
- ' Dim fileSuccess As String = "C:\GameWallpaper Success.txt"
- ' Dim fileError As String = "C:\GameWallpaper Error.txt"
- ' IO.File.WriteAllText(fileSuccess, "")
- ' IO.File.WriteAllText(fileError, "")
- ' If Not IO.Directory.Exists(outputFolder) Then
- ' IO.Directory.CreateDirectory(outputFolder)
- ' End If
- 'For Each id As Integer In wallIds
- ' Debug.WriteLine(CStr(id))
- ' ' Download wallpaper.
- ' wallInfo = GameWallpaper.DownloadWallpaper(id, wallRes)
- ' If wallInfo.ImageStream IsNot Nothing Then
- ' ' Save image stream to disk.
- ' If wallInfo.ImageStream.Length <> 0 Then ' Image is ok.
- ' Using img As Image = Image.FromStream(wallInfo.ImageStream)
- ' img.Save(IO.Path.Combine(outputFolder, wallInfo.Filename), wallInfo.ImageFormat)
- ' End Using
- ' ' Record wallpaper success.
- ' IO.File.AppendAllText(fileSuccess, wallInfo.Url & Environment.NewLine)
- ' Else ' Image contains errors.
- ' ' Record wallpaper error.
- ' IO.File.AppendAllText(fileError, wallInfo.Url & Environment.NewLine)
- ' End If
- ' Else
- ' ' Record wallpaper error.
- ' IO.File.AppendAllText(fileError, wallInfo.Url & Environment.NewLine)
- ' End If
- ' Threading.Thread.Sleep(3000)
- 'Next id
- ' End Sub
- 'End Class
- #End Region
- #Region " Imports "
- Imports System.Net
- Imports System.Web
- Imports System.IO
- #End Region
- #Region " GameWallpapersApi "
- ''' <summary>
- '''
- ''' </summary>
- Public NotInheritable Class GameWallpapersAPI
- #Region " Properties "
- ''' <summary>
- ''' Gets the GameWallpapers account username.
- ''' </summary>
- ''' <value>The GameWallpapers account username.</value>
- Public ReadOnly Property Username As String
- Get
- Return Me.username1
- End Get
- End Property
- ''' <summary>
- ''' The GameWallpapers account username.
- ''' </summary>
- Private ReadOnly username1 As String
- ''' <summary>
- ''' Gets the GameWallpapers account password.
- ''' </summary>
- ''' <value>The GameWallpapers account password.</value>
- Public ReadOnly Property Password As String
- Get
- Return Me.password1
- End Get
- End Property
- ''' <summary>
- ''' The GameWallpapers account password.
- ''' </summary>
- Private ReadOnly password1 As String
- ''' <summary>
- ''' Determines whether the user is logged in GameWallpapers.
- ''' </summary>
- Private isLogged As Boolean
- ''' <summary>
- ''' Gets the main GameWallpapers url.
- ''' </summary>
- ''' <value>The main GameWallpapers url.</value>
- Public ReadOnly Property UrlMain As String
- Get
- Return "http://www.gamewallpapers.com/"
- End Get
- End Property
- ''' <summary>
- ''' Gets the GameWallpapers login url.
- ''' </summary>
- ''' <value>The GameWallpapers login url.</value>
- Public ReadOnly Property UrlLogin As String
- Get
- Return "http://www.gamewallpapers.com/login.php"
- End Get
- End Property
- ''' <summary>
- ''' Gets the GameWallpapers get-wallpaper query string format.
- ''' </summary>
- ''' <value>The GameWallpapers get-wallpaper query string format.</value>
- Public ReadOnly Property UrlLoginQueryFormat As String
- Get
- Return "action={0}&email={1}&wachtwoord={2}"
- End Get
- End Property
- ''' <summary>
- ''' Gets the GameWallpapers get-wallpaper.
- ''' </summary>
- ''' <value>The GameWallpapers get-wallpaper.</value>
- Public ReadOnly Property UrlGetWallpaper As String
- Get
- Return "http://www.gamewallpapers.com/members/getwallpaper.php"
- End Get
- End Property
- ''' <summary>
- ''' Gets the GameWallpapers get-wallpaper query string format.
- ''' </summary>
- ''' <value>The GameWallpapers get-wallpaper query string format.</value>
- Public ReadOnly Property UrlGetWallpaperQueryFormat As String
- Get
- Return "id={0}&res={1}"
- End Get
- End Property
- ''' <summary>
- ''' Gets the GameWallpaper cookie container.
- ''' </summary>
- ''' <value>The cookie container.</value>
- Public ReadOnly Property CookieCollection As CookieCollection
- Get
- Return Me.cookieCollection1
- End Get
- End Property
- ''' <summary>
- ''' The GameWallpapers cookie container.
- ''' </summary>
- Private cookieCollection1 As CookieCollection
- ''' <summary>
- ''' Gets or sets the http user agent.
- ''' </summary>
- ''' <value>The http user agent.</value>
- Public Property UserAgent As String = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0"
- ''' <summary>
- ''' Gets the GameWallpapers headers for a Login POST request.
- ''' </summary>
- ''' <value>The GameWallpapers headers for a Login POST request.</value>
- Public ReadOnly Property RequestHeadersPostLogin As WebHeaderCollection
- Get
- Dim headers As New WebHeaderCollection
- With headers
- .Add("Accept-Language", "en-us,en;q=0.5")
- .Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
- .Add("Keep-Alive", "300")
- End With
- Return headers
- End Get
- End Property
- ''' <summary>
- ''' Gets the GameWallpapers headers for a GET request.
- ''' </summary>
- ''' <value>The GameWallpapers headers for a GET request.</value>
- Public ReadOnly Property RequestHeadersGet As WebHeaderCollection
- Get
- Return Me.RequestHeadersPostLogin
- End Get
- End Property
- ''' <summary>
- ''' Gets the GameWallpapers headers for a GetWallpaper POST request.
- ''' </summary>
- ''' <value>The GameWallpapers headers for a GetWallpaper POST request.</value>
- Public ReadOnly Property RequestHeadersGetWallpaper As WebHeaderCollection
- Get
- Dim headers As New WebHeaderCollection
- With headers
- .Add("Accept-Language", "en-US,en;q=0.5")
- .Add("Accept-Encoding", "gzip, deflate")
- .Add("Keep-Alive", "300")
- End With
- Return headers
- End Get
- End Property
- #End Region
- #Region " Enumerations "
- ''' <summary>
- ''' Specifies a GameWallpapers avaliable resolution.
- ''' </summary>
- <Serializable>
- Public Enum WallpaperResolution
- ''' <summary>
- ''' 1280X720, 720p resolution.
- ''' </summary>
- Res1280x720
- ''' <summary>
- ''' 1280x768, 15:9 resolution.
- ''' </summary>
- Res1280x768
- ''' <summary>
- ''' 1280x800, 16:10 resolution.
- ''' </summary>
- Res1280x800
- ''' <summary>
- ''' _1360x768, 16:9 resolution.
- ''' </summary>
- Res1360x768
- ''' <summary>
- ''' 1366x768, 16:9 resolution.
- ''' </summary>
- Res1366x768
- ''' <summary>
- ''' 1440x900, 16:10 resolution.
- ''' </summary>
- Res1440x900
- ''' <summary>
- ''' 1600x900, 16:9 resolution.
- ''' </summary>
- Res1600x900
- ''' <summary>
- ''' 1680x945, 16:10 resolution.
- ''' </summary>
- Res1680x945
- ''' <summary>
- ''' 1680x1050, 16:10 resolution.
- ''' </summary>
- Res1680x1050
- ''' <summary>
- ''' 1920x1080, 1080p resolution.
- ''' </summary>
- Res1920x1080
- ''' <summary>
- ''' 1920X1200, 16:10 resolution.
- ''' </summary>
- Res1920x1200
- ''' <summary>
- ''' 2560x1440, 16:9 resolution.
- ''' </summary>
- Res2560x1440
- ''' <summary>
- ''' 2560x1600, 16:10 resolution.
- ''' </summary>
- Res2560x1600
- ''' <summary>
- ''' 3840x2160, 4K resolution.
- ''' </summary>
- Res3840x2160
- End Enum
- #End Region
- #Region " Types "
- ''' <summary>
- ''' Defines a GameWallpaper.
- ''' </summary>
- <Serializable>
- Public NotInheritable Class GameWallpaperInfo : Implements IDisposable
- #Region " Properties "
- ''' <summary>
- ''' Gets the wallpaper imageformat.
- ''' </summary>
- ''' <value>The wallpaper imageformat.</value>
- Public ReadOnly Property ImageFormat As Imaging.ImageFormat
- Get
- Return Imaging.ImageFormat.Jpeg
- End Get
- End Property
- ''' <summary>
- ''' Gets the wallpaper Identifier.
- ''' </summary>
- ''' <value>The wallpaper Identifier.</value>
- Public ReadOnly Property Id As Integer
- Get
- Return Me.id1
- End Get
- End Property
- ''' <summary>
- ''' The wallpaper Identifier.
- ''' </summary>
- Private ReadOnly id1 As Integer
- ''' <summary>
- ''' Gets the wallpaper resolution.
- ''' </summary>
- ''' <value>The wallpaper resolution.</value>
- Public ReadOnly Property Resolution As WallpaperResolution
- Get
- Return Me.resolution1
- End Get
- End Property
- ''' <summary>
- ''' The wallpaper resolution.
- ''' </summary>
- Private ReadOnly resolution1 As WallpaperResolution
- ''' <summary>
- ''' Gets the wallpaper filename.
- ''' </summary>
- ''' <value>The wallpaper filename.</value>
- Public ReadOnly Property Filename As String
- Get
- Return Me.filename1
- End Get
- End Property
- ''' <summary>
- ''' The wallpaper filename.
- ''' </summary>
- Private ReadOnly filename1 As String
- ''' <summary>
- ''' Gets the Stream that contains the image.
- ''' </summary>
- ''' <value>The wallpaper filename.</value>
- Public ReadOnly Property ImageStream As Stream
- Get
- Return Me.imageStream1
- End Get
- End Property
- ''' <summary>
- ''' The Stream that contains the image.
- ''' </summary>
- Private ReadOnly imageStream1 As MemoryStream
- ''' <summary>
- ''' Gets the wallpaper url.
- ''' </summary>
- ''' <value>The wallpaper url.</value>
- Public ReadOnly Property Url As String
- Get
- Return Me.url1
- End Get
- End Property
- ''' <summary>
- ''' The wallpaper url.
- ''' </summary>
- Private ReadOnly url1 As String
- #End Region
- #Region " Constructors "
- ''' <summary>
- ''' Prevents a default instance of the <see cref="GameWallpaperInfo"/> class from being created.
- ''' </summary>
- Private Sub New()
- End Sub
- ''' <summary>
- ''' Initializes a new instance of the <see cref="GameWallpaperInfo"/> class.
- ''' </summary>
- ''' <param name="url">The wallpaper url.</param>
- ''' <param name="id">The wallpaper identifier.</param>
- ''' <param name="resolution">The wallpaper resolution.</param>
- ''' <param name="filename">The wallpaper filename.</param>
- ''' <param name="imageStream">The Stream that contains the image.</param>
- Public Sub New(ByVal url As String,
- ByVal id As Integer,
- ByVal resolution As WallpaperResolution,
- Optional ByVal filename As String = Nothing,
- Optional ByVal imageStream As MemoryStream = Nothing)
- Me.url1 = url
- Me.id1 = id
- Me.resolution1 = resolution
- Me.filename1 = filename
- Me.imageStream1 = imageStream
- End Sub
- #End Region
- #Region " IDisposable "
- ''' <summary>
- ''' To detect redundant calls when disposing.
- ''' </summary>
- Private isDisposed As Boolean = False
- ''' <summary>
- ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- ''' </summary>
- Public Sub Dispose() Implements IDisposable.Dispose
- Me.Dispose(isDisposing:=True)
- GC.SuppressFinalize(obj:=Me)
- End Sub
- ''' <summary>
- ''' Releases unmanaged and - optionally - managed resources.
- ''' </summary>
- ''' <param name="isDisposing">
- ''' <c>true</c> to release both managed and unmanaged resources;
- ''' <c>false</c> to release only unmanaged resources.
- ''' </param>
- Protected Sub Dispose(ByVal isDisposing As Boolean)
- If Not Me.isDisposed Then
- If isDisposing Then
- If Me.imageStream1 IsNot Nothing Then
- Me.imageStream1.Flush()
- Me.imageStream1.Close()
- End If
- End If
- End If
- Me.isDisposed = True
- End Sub
- #End Region
- End Class
- ''' <summary>
- ''' Defines the query data for a LoginPost request.
- ''' </summary>
- Private NotInheritable Class LoginQueryData
- ''' <summary>
- ''' Gets the action field.
- ''' </summary>
- ''' <value>The action field.</value>
- Public ReadOnly Property Action As String
- Get
- Return "go"
- End Get
- End Property
- ''' <summary>
- ''' Gets or sets the email field.
- ''' </summary>
- ''' <value>The email field.</value>
- Public Property Email As String
- ''' <summary>
- ''' Gets or sets the wachtwoord field.
- ''' </summary>
- ''' <value>The wachtwoord field.</value>
- Public Property Wachtwoord As String
- End Class
- ''' <summary>
- ''' Defines the query data for a GetWallpaper request.
- ''' </summary>
- Private NotInheritable Class WallpaperQueryData
- ''' <summary>
- ''' Gets or sets the id field.
- ''' </summary>
- ''' <value>The id field.</value>
- Public Property Id As String
- ''' <summary>
- ''' Gets or sets res wachtwoord field.
- ''' </summary>
- ''' <value>The res field.</value>
- Public Property Res As String
- End Class
- #End Region
- #Region " Exceptions "
- ''' <summary>
- ''' Exception that is thrown when GameWallpapers server does not sends a response.
- ''' </summary>
- <Serializable>
- Public NotInheritable Class NoResponseReceivedException : Inherits Exception
- ''' <summary>
- ''' Initializes a new instance of the <see cref="NoResponseReceivedException"/> class.
- ''' </summary>
- Public Sub New()
- MyBase.New("No response received from server")
- End Sub
- ''' <summary>
- ''' Initializes a new instance of the <see cref="NoResponseReceivedException"/> class.
- ''' </summary>
- ''' <param name="message">The message that describes the error.</param>
- Public Sub New(message As String)
- MyBase.New(message)
- End Sub
- ''' <summary>
- ''' Initializes a new instance of the <see cref="NoResponseReceivedException"/> class.
- ''' </summary>
- ''' <param name="message">The message that describes the error.</param>
- ''' <param name="inner">The inner exception.</param>
- Public Sub New(message As String, inner As Exception)
- MyBase.New(message, inner)
- End Sub
- End Class
- ''' <summary>
- ''' Exception that is thrown when cannot login to GameWallpapers.
- ''' </summary>
- <Serializable>
- Public NotInheritable Class LoginRejectedException : Inherits Exception
- ''' <summary>
- ''' Initializes a new instance of the <see cref="LoginRejectedException"/> class.
- ''' </summary>
- Public Sub New()
- MyBase.New("Cannot login to server.")
- End Sub
- ''' <summary>
- ''' Initializes a new instance of the <see cref="LoginRejectedException"/> class.
- ''' </summary>
- ''' <param name="message">The message that describes the error.</param>
- Public Sub New(message As String)
- MyBase.New(message)
- End Sub
- ''' <summary>
- ''' Initializes a new instance of the <see cref="LoginRejectedException"/> class.
- ''' </summary>
- ''' <param name="message">The message that describes the error.</param>
- ''' <param name="inner">The inner exception.</param>
- Public Sub New(message As String, inner As Exception)
- MyBase.New(message, inner)
- End Sub
- End Class
- ''' <summary>
- ''' Exception that is thrown when a GET request is rejected.
- ''' </summary>
- <Serializable>
- Public NotInheritable Class GetRequestRejectedException : Inherits Exception
- ''' <summary>
- ''' Initializes a new instance of the <see cref="GetRequestRejectedException"/> class.
- ''' </summary>
- Public Sub New()
- MyBase.New("Cannot login to server.")
- End Sub
- ''' <summary>
- ''' Initializes a new instance of the <see cref="GetRequestRejectedException"/> class.
- ''' </summary>
- ''' <param name="message">The message that describes the error.</param>
- Public Sub New(message As String)
- MyBase.New(message)
- End Sub
- ''' <summary>
- ''' Initializes a new instance of the <see cref="GetRequestRejectedException"/> class.
- ''' </summary>
- ''' <param name="message">The message that describes the error.</param>
- ''' <param name="inner">The inner exception.</param>
- Public Sub New(message As String, inner As Exception)
- MyBase.New(message, inner)
- End Sub
- End Class
- #End Region
- #Region " Constructors "
- ''' <summary>
- ''' Prevents a default instance of the <see cref="GameWallpapersApi"/> class from being created.
- ''' </summary>
- Private Sub New()
- End Sub
- ''' <summary>
- ''' Initializes a new instance of the <see cref="GameWallpapersApi"/> class.
- ''' </summary>
- ''' <param name="username">The GameWallpapers account username.</param>
- ''' <param name="password">The GameWallpapers account password.</param>
- Public Sub New(ByVal username As String,
- ByVal password As String)
- Me.username1 = username
- Me.password1 = password
- Me.cookieCollection1 = New CookieCollection
- End Sub
- #End Region
- #Region " Public Methods "
- ''' <summary>
- ''' Determines whether the account can log in GameWallpapers.
- ''' </summary>
- ''' <returns><c>true</c> if the account can log in GameWallpapers, <c>false</c> otherwise.</returns>
- Public Function CheckLogin() As Boolean
- If Me.GetMethod(Me.UrlMain, Me.cookieCollection1) Then
- Return Me.PostLoginMethod(Me.GetLoginQueryData, Me.cookieCollection1)
- Else
- Return False
- End If ' Me.GetMethod
- End Function
- ''' <summary>
- ''' Downloads the specified wallpaper.
- ''' </summary>
- ''' <param name="id">The wallpaper identifier.</param>
- ''' <param name="res">The desired wallpaper resource (maybe could not be avaliable for the specified wallpaper).</param>
- Public Function DownloadWallpaper(ByVal id As Integer,
- ByVal res As WallpaperResolution) As GameWallpaperInfo
- ' Set the query data.
- Dim wallpaperData As New WallpaperQueryData With
- {
- .Id = CStr(id),
- .Res = res.ToString.Remove(0, "res".Length)
- }
- If Me.isLogged Then
- Return Me.GetWallpaperMethod(wallpaperData, Me.cookieCollection1)
- Else
- If Me.GetMethod(Me.UrlMain, Me.cookieCollection1) Then
- If Me.PostLoginMethod(Me.GetLoginQueryData, Me.cookieCollection1) Then
- Return Me.GetWallpaperMethod(wallpaperData, Me.cookieCollection1)
- Else ' Cannot login the account.
- Throw New LoginRejectedException
- Return Nothing
- End If ' Me.PostLoginMethod
- Else
- Throw New GetRequestRejectedException
- Return Nothing
- End If ' Me.GetMethod
- End If
- End Function
- #End Region
- #Region " Private Methods "
- ''' <summary>
- ''' Converts cookie string to global cookie collection object.
- ''' </summary>
- ''' <param name="cookie">The cookie string.</param>
- ''' <param name="cookieCollection">The cookie collection.</param>
- Private Sub SaveCookies(ByVal cookie As String,
- ByRef cookieCollection As CookieCollection)
- Dim cookieStrings() As String = cookie.Trim.
- Replace("path=/,", String.Empty).
- Replace("path=/", String.Empty).
- Split({";"c}, StringSplitOptions.RemoveEmptyEntries)
- cookieCollection = New CookieCollection
- For Each cookieString As String In cookieStrings
- If Not String.IsNullOrEmpty(cookieString.Trim) Then
- cookieCollection.Add(New Cookie(name:=cookieString.Trim.Split("="c)(0),
- value:=cookieString.Trim.Split("="c)(1)))
- End If
- Next cookieString
- End Sub
- ''' <summary>
- ''' Convert cookie container object to global cookie collection object.
- ''' </summary>
- ''' <param name="cookieContainer">The cookie container.</param>
- ''' <param name="cookieCollection">The cookie collection.</param>
- ''' <param name="url">The url.</param>
- Private Sub SaveCookies(ByVal cookieContainer As CookieContainer,
- ByRef cookieCollection As CookieCollection,
- ByVal url As String)
- cookieCollection = New CookieCollection
- For Each cookie As Cookie In cookieContainer.GetCookies(New Uri(url))
- cookieCollection.Add(cookie)
- Next cookie
- End Sub
- ''' <summary>
- ''' Sets the cookie container.
- ''' </summary>
- ''' <param name="url">The url.</param>
- ''' <param name="cookieCollection">The cookie collection.</param>
- ''' <returns>CookieContainer.</returns>
- Private Function SetCookieContainer(ByVal url As String,
- ByVal cookieCollection As CookieCollection) As CookieContainer
- Dim cookieContainer As New CookieContainer
- Dim refDate As Date
- For Each oldCookie As Cookie In cookieCollection
- If Not DateTime.TryParse(oldCookie.Value, refDate) Then
- Dim newCookie As New Cookie
- With newCookie
- .Name = oldCookie.Name
- .Value = oldCookie.Value
- .Domain = New Uri(url).Host
- .Secure = False
- End With
- cookieContainer.Add(newCookie)
- End If
- Next oldCookie
- Return cookieContainer
- End Function
- ''' <summary>
- ''' Gets a new <see cref="LoginQueryData"/> instance using the current account credentials.
- ''' </summary>
- ''' <returns>A new <see cref="LoginQueryData"/> instance using the current account credentials.</returns>
- Private Function GetLoginQueryData() As LoginQueryData
- Return New LoginQueryData With
- {
- .Email = HttpUtility.UrlEncode(Me.username1),
- .Wachtwoord = HttpUtility.UrlEncode(Me.password1)
- }
- End Function
- ''' <summary>
- ''' Gets a formatted <see cref="String"/> representation of a <see cref="LoginQueryData"/> object.
- ''' </summary>
- ''' <param name="loginQueryData">The <see cref="LoginQueryData"/> object that contains the login query fields.</param>
- ''' <returns>A formatted <see cref="String"/> representation of a <see cref="LoginQueryData"/> object.</returns>
- Private Function GetLoginQueryString(ByVal loginQueryData As LoginQueryData) As String
- Return String.Format(Me.UrlLoginQueryFormat,
- loginQueryData.Action, loginQueryData.Email, loginQueryData.Wachtwoord)
- End Function
- ''' <summary>
- ''' Gets a formatted <see cref="String"/> representation of a <see cref="WallpaperQueryData"/> object.
- ''' </summary>
- ''' <param name="wallpaperQueryData">The <see cref="WallpaperQueryData"/> object that contains the 'GetWallpaper' query fields.</param>
- ''' <returns>A formatted <see cref="String"/> representation of a <see cref="LoginQueryData"/> object.</returns>
- Private Function GetWallpaperQueryString(ByVal wallpaperQueryData As WallpaperQueryData) As String
- Return String.Format(Me.UrlGetWallpaperQueryFormat,
- wallpaperQueryData.Id, wallpaperQueryData.Res)
- End Function
- ''' <summary>
- '''
- ''' </summary>
- ''' <param name="url">The url.</param>
- ''' <param name="cookieCollection">The cookie collection.</param>
- ''' <returns><c>true</c> if successfull, <c>false</c> otherwise.</returns>
- Private Function GetMethod(ByVal url As String,
- ByRef cookieCollection As CookieCollection) As Boolean
- Dim request As HttpWebRequest = Nothing
- Dim response As HttpWebResponse = Nothing
- Dim sr As StreamReader = Nothing
- Dim result As Boolean = False
- Try
- #If DEBUG Then
- Debug.WriteLine("[+] GetMethod method started.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Attempting to perform a request with:")
- Debug.WriteLine(String.Format("Method: {0}", "GET"))
- Debug.WriteLine(String.Format("UserAgent: {0}", Me.UserAgent))
- Debug.WriteLine(String.Format("Accept: {0}", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"))
- Debug.WriteLine(String.Format("Headers: {0}", String.Join(Environment.NewLine, Me.RequestHeadersGet)))
- #End If
- request = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
- With request
- .Method = "GET"
- .Headers = Me.RequestHeadersGet
- .Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
- .UserAgent = Me.UserAgent
- .AllowAutoRedirect = False
- .KeepAlive = True
- End With
- #If DEBUG Then
- Debug.WriteLine("[-] Request done.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Getting server response...")
- #End If
- ' Get the server response.
- response = DirectCast(request.GetResponse, HttpWebResponse)
- #If DEBUG Then
- Debug.WriteLine("[-] Getting server response done.")
- #End If
- If request.HaveResponse Then
- #If DEBUG Then
- Debug.WriteLine("[+] Saving cookies...")
- #End If
- ' Save the cookie info.
- Me.SaveCookies(response.Headers("Set-Cookie"), cookieCollection)
- #If DEBUG Then
- Debug.WriteLine("[-] Saving cookies done.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Getting server response...")
- #End If
- ' Get the server response.
- response = DirectCast(request.GetResponse, HttpWebResponse)
- #If DEBUG Then
- Debug.WriteLine("[-] Getting server response done.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Reading server response...")
- #End If
- sr = New StreamReader(response.GetResponseStream)
- Using sr
- ' Read the response from the server, but we do not save it.
- sr.ReadToEnd()
- End Using
- result = True
- #If DEBUG Then
- Debug.WriteLine("[-] Reading server response done.")
- #End If
- Else ' No response received from server.
- Throw New NoResponseReceivedException(String.Format("No response received from server with url: {0}", url))
- result = False
- End If
- Catch ex As Exception
- Throw
- result = False
- Finally
- If sr IsNot Nothing Then
- sr.Dispose()
- End If
- If response IsNot Nothing Then
- response.Close()
- End If
- End Try
- #If DEBUG Then
- Debug.WriteLine("[-] GetMethod method finished.")
- Debug.WriteLine("[i] Returning result value...")
- #End If
- Return result
- End Function
- ''' <summary>
- '''
- ''' </summary>
- ''' <param name="loginData">The login post data.</param>
- ''' <param name="cookieCollection">The cookie collection.</param>
- ''' <returns><c>true</c> if successfull, <c>false</c> otherwise.</returns>
- Private Function PostLoginMethod(ByVal loginData As LoginQueryData,
- ByRef cookieCollection As CookieCollection) As Boolean
- Dim request As HttpWebRequest = Nothing
- Dim response As HttpWebResponse = Nothing
- Dim sw As StreamWriter = Nothing
- Dim initialCookieCount As Integer = 0
- Dim postData As String
- Dim result As Boolean = False
- Try
- #If DEBUG Then
- Debug.WriteLine("[+] PostLoginMethod method started.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Attempting to perform a login request with:")
- Debug.WriteLine(String.Format("Method: {0}", "POST"))
- Debug.WriteLine(String.Format("UserAgent: {0}", Me.UserAgent))
- Debug.WriteLine(String.Format("Referer: {0}", Me.UrlMain))
- Debug.WriteLine(String.Format("Accept: {0}", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"))
- Debug.WriteLine(String.Format("ContentType: {0}", "application/x-www-form-urlencoded"))
- Debug.WriteLine(String.Format("Headers: {0}", String.Join(Environment.NewLine, Me.RequestHeadersPostLogin)))
- #End If
- request = DirectCast(HttpWebRequest.Create(Me.UrlLogin), HttpWebRequest)
- With request
- .Method = "POST"
- .Headers = Me.RequestHeadersPostLogin
- .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
- .ContentType = "application/x-www-form-urlencoded"
- .UserAgent = Me.UserAgent
- .Referer = Me.UrlMain
- End With
- #If DEBUG Then
- Debug.WriteLine("[-] Request done.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Passing request cookie info...")
- #End If
- If cookieCollection IsNot Nothing Then ' Pass cookie info from the login page.
- request.CookieContainer = Me.SetCookieContainer(Me.UrlLogin, cookieCollection)
- End If
- #If DEBUG Then
- Debug.WriteLine("[-] Passing request cookie info done.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Setting post data with:")
- Debug.WriteLine(String.Format("Action: {0}", loginData.Action))
- Debug.WriteLine(String.Format("Email: {0}", loginData.Email))
- Debug.WriteLine(String.Format("Wachtwoord: {0}", loginData.Wachtwoord))
- #End If
- ' Set the post data.
- postData = Me.GetLoginQueryString(loginData)
- sw = New StreamWriter(request.GetRequestStream)
- Using sw
- sw.Write(postData) ' Post the data to the server.
- End Using
- #If DEBUG Then
- Debug.WriteLine("[-] Setting post data done.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Getting server response...")
- #End If
- ' Get the server response.
- initialCookieCount = request.CookieContainer.Count
- response = DirectCast(request.GetResponse, HttpWebResponse)
- #If DEBUG Then
- Debug.WriteLine("[-] Getting server response done.")
- #End If
- If request.CookieContainer.Count > initialCookieCount Then ' Login successful.
- result = True
- Else ' Login unsuccessful.
- result = False
- End If
- #If DEBUG Then
- Debug.WriteLine(String.Format("[i] Login response result is: {0}",
- If(result, "Successful (True)",
- "Unsuccessful (False)")))
- #End If
- If result Then ' Save new login cookies.
- #If DEBUG Then
- Debug.WriteLine("[+] Saving new login cookies...")
- #End If
- Me.SaveCookies(request.CookieContainer, cookieCollection, Me.UrlMain)
- #If DEBUG Then
- Debug.WriteLine("[-] Saving new login cookies done.")
- #End If
- End If
- Catch ex As Exception
- Throw
- Finally
- If sw IsNot Nothing Then
- sw.Dispose()
- End If
- If response IsNot Nothing Then
- response.Close()
- End If
- End Try
- #If DEBUG Then
- Debug.WriteLine("[-] PostLoginMethod method finished.")
- Debug.WriteLine("[i] Returning result value...")
- #End If
- Me.isLogged = result
- Return result
- End Function
- ''' <summary>
- ''' Tries to download the specified wallpaper from GameWallpapers server.
- ''' </summary>
- ''' <param name="wallpaperQueryData">The wallpaper query data.</param>
- ''' <param name="cookieCollection">The cookie collection.</param>
- ''' <returns>A <see cref="GameWallpaperInfo"/> containing the wallpaper stream and it's info.</returns>
- Private Function GetWallpaperMethod(ByVal wallpaperQueryData As WallpaperQueryData,
- ByRef cookieCollection As CookieCollection) As GameWallpaperInfo
- Dim request As HttpWebRequest
- Dim url As String = Me.UrlGetWallpaper & "?" & Me.GetWallpaperQueryString(wallpaperQueryData)
- Dim contentDisposition As String
- Dim webResponse As WebResponse = Nothing
- Dim responseStream As Stream = Nothing
- Dim imageStream As MemoryStream = Nothing
- Dim wallInfo As GameWallpaperInfo = Nothing
- #If DEBUG Then
- Debug.WriteLine("[+] GetWallpaperMethod method started.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Attempting to perform a GetWallpaper request with:")
- Debug.WriteLine(String.Format("Method: {0}", "GET"))
- Debug.WriteLine(String.Format("UserAgent: {0}", Me.UserAgent))
- Debug.WriteLine(String.Format("Accept: {0}", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"))
- Debug.WriteLine(String.Format("ContentType: {0}", "application/x-www-form-urlencoded"))
- Debug.WriteLine(String.Format("Headers: {0}", String.Join(Environment.NewLine, Me.RequestHeadersGetWallpaper)))
- #End If
- Try
- request = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
- With request
- .Method = "GET"
- .Headers = Me.RequestHeadersGetWallpaper
- .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
- .AllowAutoRedirect = False
- .UserAgent = Me.UserAgent
- .KeepAlive = True
- End With
- #If DEBUG Then
- Debug.WriteLine("[-] Request done.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Passing request cookie info...")
- #End If
- If cookieCollection IsNot Nothing Then
- ' Pass cookie info so that we remain logged in.
- request.CookieContainer = Me.SetCookieContainer(url, cookieCollection)
- End If
- #If DEBUG Then
- Debug.WriteLine("[-] Passing request cookie done.")
- #End If
- #If DEBUG Then
- Debug.WriteLine("[+] Getting response...")
- #End If
- webResponse = request.GetResponse
- #If DEBUG Then
- Debug.WriteLine("[-] Getting response done.")
- #End If
- Using webResponse
- contentDisposition = CType(webResponse, HttpWebResponse).Headers("Content-Disposition")
- If Not String.IsNullOrEmpty(contentDisposition) Then ' There is an image to download.
- Dim filename As String = contentDisposition.Substring(contentDisposition.IndexOf("=") + "=".Length).
- TrimStart(" "c).TrimEnd({" "c, ";"c})
- Try
- imageStream = New MemoryStream
- responseStream = webResponse.GetResponseStream
- #If DEBUG Then
- Debug.WriteLine("[+] Reading image stream...")
- #End If
- Using responseStream
- Dim buffer(2047) As Byte
- Dim read As Integer
- Do
- read = responseStream.Read(buffer, 0, buffer.Length)
- imageStream.Write(buffer, 0, read)
- Loop Until read = 0
- responseStream.Close()
- End Using
- #If DEBUG Then
- Debug.WriteLine("[-] Reading image stream done.")
- #End If
- Catch ex As Exception
- Throw
- End Try
- #If DEBUG Then
- Debug.WriteLine("[+] Setting wallpaper info object...")
- #End If
- wallInfo = New GameWallpaperInfo(url:=url,
- id:=CInt(wallpaperQueryData.Id),
- resolution:=DirectCast([Enum].Parse(GetType(WallpaperResolution),
- "Res" & wallpaperQueryData.Res,
- ignoreCase:=True), WallpaperResolution),
- filename:=filename,
- imageStream:=imageStream)
- #If DEBUG Then
- Debug.WriteLine("[-] Setting wallpaper info object done.")
- #End If
- End If ' String.IsNullOrEmpty(contentDisposition)
- End Using ' webResponse
- Catch ex As Exception
- Throw
- Finally
- If webResponse IsNot Nothing Then
- webResponse.Close()
- End If
- If responseStream IsNot Nothing Then
- responseStream.Close()
- End If
- End Try
- #If DEBUG Then
- Debug.WriteLine("[-] GetWallpaperMethod method finished.")
- Debug.WriteLine("[i] Returning wallInfo object...")
- #End If
- If wallInfo Is Nothing Then
- wallInfo = New GameWallpaperInfo(url:=url,
- id:=CInt(wallpaperQueryData.Id),
- resolution:=DirectCast([Enum].Parse(GetType(WallpaperResolution),
- "Res" & wallpaperQueryData.Res,
- ignoreCase:=True), WallpaperResolution))
- End If
- Return wallInfo
- End Function
- #End Region
- End Class
- #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement