Meka][Meka Unstopable Posts: 700
| note: you may use this tutorial on other sites as long as u post the author note. -------------------------- Obtaining Files From Web -------------------------- Author: Meka][Meka -------------------------- http://www.meka-meka.com/ -------------------------- Level: Beginner -------------------------- Ok start a new project, ok we need to create a function that will request the file, open a stream and obtain the incomining data
Code: | Private Function GetFile(ByVal Url As String, ByVal File As String) As String Dim URLReq As System.Net.HttpWebRequest 'requesting Dim URLRes As System.Net.HttpWebResponse 'responces Dim iBytesRead As Integer 'bytes being read Dim bBuffer(999) As Byte 'our store buffer Try URLReq = System.Net.HttpWebRequest.Create(Url & "/" & File) 'request the file URLRes = URLReq.GetResponse 'get the responce Dim sData As String 'store the file data Dim IncomingData As System.IO.Stream = URLReq.GetResponse.GetResponseStream 'get incoming stream Do iBytesRead = IncomingData.Read(bBuffer, 0, 1000) 'read the data sData = sData & System.Text.Encoding.Default.GetString(bBuffer, 0, bBuffer.Length) 'append the data Loop Until iBytesRead = 0 'loop until thers no responces IncomingData.Close() 'close the stream Return sData 'return the data Catch ex As Exception 'if error occurs MsgBox(ex.Message) 'show an error End Try End Function | now all we need todo is call the function with the appropriate paremeters
Code: | Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim sData As String = GetFile("http://websiteaddress/", "test.txt") MsgBox(sData) End Sub | i will update it, maybe add threading, and few other things ltr njoy,
|
Meka][Meka Unstopable Posts: 700
| updated, using its own thread, and added stringbuilder as i used old slow string appending on the tut
Code: | Private URL As String Private File As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load URL = "http://www.gameplanetuk.com/" File = "test.txt" Dim Thread As New Threading.Thread(AddressOf GetFile) 'create a new thread for getting the file Thread.IsBackground = True 'let it jus run as a background process Thread.Start() 'run the function with the new thread End Sub Private Sub ReceivedFile(ByVal sData As String) MsgBox(sData) End Sub Private Sub GetFile() Dim URLReq As System.Net.HttpWebRequest 'requesting Dim URLRes As System.Net.HttpWebResponse 'responces Dim iBytesRead As Integer 'bytes being read Dim bBuffer(999) As Byte 'our store buffer Try URLReq = System.Net.HttpWebRequest.Create(URL & "/" & File) 'request the file URLRes = URLReq.GetResponse 'get the responce Dim sData As New System.Text.StringBuilder 'store the file data Dim IncomingData As System.IO.Stream = URLReq.GetResponse.GetResponseStream 'get incoming stream Do iBytesRead = IncomingData.Read(bBuffer, 0, 1000) 'read the data sData.Append(System.Text.Encoding.Default.GetString(bBuffer, 0, bBuffer.Length)) 'append the data Loop Until iBytesRead = 0 'loop until thers no responces IncomingData.Close() 'close the stream ReceivedFile(sData.ToString) 'run function coz we have the file Catch ex As Exception 'if error occurs MsgBox(ex.Message) 'show an error End Try Threading.Thread.CurrentThread.Abort() 'abort the thread End Sub | -/Meka][Meka
|