' ******************************************************************************
' **
' ** Yahoo Finance Managed
' ** Written by Marius Häusler 2010
' ** It would be pleasant, if you contact me when you are using this code.
' ** Contact: YahooFinanceManaged@gmail.com
' ** Project Home: http://code.google.com/p/yahoo-finance-managed/
' **
' ******************************************************************************
' **
' ** Copyright 2010 Marius Häusler
' **
' ** Licensed under the Apache License, Version 2.0 (the "License");
' ** you may not use this file except in compliance with the License.
' ** You may obtain a copy of the License at
' **
' ** http://www.apache.org/licenses/LICENSE-2.0
' **
' ** Unless required by applicable law or agreed to in writing, software
' ** distributed under the License is distributed on an "AS IS" BASIS,
' ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' ** See the License for the specific language governing permissions and
' ** limitations under the License.
' **
' ******************************************************************************
Namespace Base
''' <summary>
''' Base class for downloading String data
''' </summary>
''' <remarks></remarks>
Public MustInherit Class DataDownload
Inherits Download
Friend Event AsyncDataBaseDownloadCompleted(ByVal sender As Download, ByVal ba As DataDownloadCompletedEventArgs)
Friend mHelper As New MyHelper
Private mTextEncoding As System.Text.Encoding = System.Text.Encoding.Default
''' <summary>
''' The used encoding informations
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property TextEncoding() As System.Text.Encoding
Get
Return mTextEncoding
End Get
Set(ByVal value As System.Text.Encoding)
If value IsNot Nothing Then : mTextEncoding = value
Else : mTextEncoding = System.Text.Encoding.Default
End If
End Set
End Property
Friend Overloads Function Download(ByVal url As String) As DataResponse
Dim text As String = String.Empty
Dim resp As TimeoutWebClientResponse = MyBase.Download(url)
If resp.Connection.State = ConnectionState.Success Then
text = Me.StreamToStringAndClose(resp.Result, Me.TextEncoding)
End If
Return New DataResponse(resp.Connection, text)
End Function
Private Sub BaseAsyncDownload_Completed(ByVal sender As Download, ByVal e As BaseDownloadCompletedEventArgs) Handles MyBase.AsyncBaseDownloadCompleted
Dim text As String = String.Empty
If e.Response.Connection.State = ConnectionState.Success Then
text = Me.StreamToStringAndClose(e.Response.Result, Me.TextEncoding)
End If
Dim args As New DataDownloadCompletedEventArgs(e.UserArgs, New DataResponse(e.Response.Connection, text))
RaiseEvent AsyncDataBaseDownloadCompleted(Me, args)
End Sub
Private Function StreamToStringAndClose(ByVal stream As IO.Stream, ByVal encoding As System.Text.Encoding) As String
Dim res As String = String.Empty
If stream IsNot Nothing Then
Dim enc As System.Text.Encoding = encoding
If enc Is Nothing Then enc = System.Text.Encoding.Default
Using sr As New IO.StreamReader(stream, enc)
res = sr.ReadToEnd
End Using
mHelper.CloseStream(stream)
End If
Return res
End Function
Friend Sub New()
End Sub
End Class
Friend Class DataDownloadCompletedEventArgs
Inherits DownloadCompletedEventArgs
Public Overloads ReadOnly Property Response() As DataResponse
Get
Return TryCast(MyBase.Response, DataResponse)
End Get
End Property
Public Sub New(ByVal userArgs As Object, ByVal response As DataResponse)
MyBase.New(userArgs, response)
End Sub
End Class
Friend Class DataResponse
Inherits Response
Public Overloads ReadOnly Property Result() As String
Get
Return MyBase.Result.ToString
End Get
End Property
Public Sub New(ByVal info As ConnectionInfo, ByVal result As String)
MyBase.New(info, result)
End Sub
End Class
End Namespace