I know this should be a basic question but I am hitting a brick wall. I am looking to go to a URL/URI download the resulting string as if I had opened a file and then get it out into a String variable.
I have been stuffing about with IO.Stream and Net.httpxxx but haven't managed to get the elements to line up in the right way.
I get "the given path's format is not supported" from opening the page in the standard stream, because it's not in the local file system ... that bit i understand, the bit I don't get is ... how do I achieve the equivelent of:
Public Function GetWebPageAsString(pURL As String) As String
Dim lStream As IO.StreamReader = New System.IO.StreamReader(pURL)
Return lStream.ReadToEnd
End Function
The short answer, in C#, looks like
using(System.Net.WebClient client = new System.Net.WebClient())
{
string html = client.DownloadString("http://www.google.com");
}
WebClient.OpenRead() might be what you're looking for.
Sample from the MSDN page linked above:
Dim uriString as String
uriString = "http://www.google.com"
Dim myWebClient As New WebClient()
Console.WriteLine("Accessing {0} ...", uriString)
Dim myStream As Stream = myWebClient.OpenRead(uriString)
Console.WriteLine(ControlChars.Cr + "Displaying Data :" + ControlChars.Cr)
Dim sr As New StreamReader(myStream)
Console.WriteLine(sr.ReadToEnd())
myStream.Close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With