C# Read webpage content Streamreader

Lars Werkman picture Lars Werkman · Jul 11, 2011 · Viewed 32.3k times · Source

I need to read the content of a webpage in streamreader like

www.example.com

<test>
<sample></sample>
</test>

i got this:

System.IO.StreamReader StreamReader1 =
new System.IO.StreamReader("www.example.com");
string test = StreamReader1.ReadToEnd();

but i then i get this error code

Attempt to access the method failed: System.IO.StreamReader..ctor(System.String)

Answer

Darin Dimitrov picture Darin Dimitrov · Jul 11, 2011

Try a WebClient, it's easier and you don't have to worry about streams and rivers:

using (var client = new WebClient())
{
    string result = client.DownloadString("http://www.example.com");
    // TODO: do something with the downloaded result from the remote
    // web site
}