开发者_开发知识库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")
精彩评论