开发者

Access text file from web with VB .Net

开发者 https://www.devze.com 2022-12-16 11:08 出处:网络
开发者_开发知识库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

开发者_开发知识库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


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?)


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")
0

精彩评论

暂无评论...
验证码 换一张
取 消