Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I download a webpage into a stream in .NET

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
like image 685
Robin Vessey Avatar asked Jan 24 '23 13:01

Robin Vessey


2 Answers

The short answer, in C#, looks like

using(System.Net.WebClient client = new System.Net.WebClient())
{
  string html = client.DownloadString("http://www.google.com");
}
like image 56
Henk Holterman Avatar answered Jan 29 '23 20:01

Henk Holterman


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()
like image 33
M4N Avatar answered Jan 29 '23 22:01

M4N