jmasterx
jmasterx

Reputation: 54113

Access text file from web with VB .Net

I'm trying to load a text file with VB .Net so that I can use it with a streamreader object. for example: www.fakesite.com/text.txt

Thanks

It is of a particular URL and is already present on the server

Upvotes: 0

Views: 5282

Answers (2)

Shiggsy
Shiggsy

Reputation: 31

Public Function GetPage(ByVal PageURL As String) As String
    Dim S As String = ""
    Try
        Dim Request As HttpWebRequest = WebRequest.Create(PageURL)
        Dim Response As HttpWebResponse = Request.GetResponse()
        Using Reader As StreamReader = New StreamReader(Response.GetResponseStream())
            S = Reader.ReadToEnd
        End Using
    Catch ex As Exception
        Debug.WriteLine("FAIL: " + ex.Message)
    End Try
    Return S
End Function    

You can call it with:

Dim Page As String = GetPage("http://randomurl.xxx/file.txt")

Upvotes: 3

Thomas
Thomas

Reputation: 181745

You can do this with HttpWebRequest and its GetResponse method, which gives you a WebResponse on which you can call GetResponseStream to get a Stream object to pass to your StreamReader.

(Dizzy yet?)

Upvotes: 2

Related Questions