5

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)

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
Lars Werkman
  • 2,528
  • 2
  • 20
  • 20

2 Answers2

28

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
}
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
4

If you want to use the StreamReader, here is the code I am using:

    const int Buffer_Size = 100 * 1024;


        WebRequest request = CreateWebRequest(uri);
        WebResponse response = request.GetResponse();
        result = GetPageHtml(response);

...

    private string GetPageHtml(WebResponse response) {
        char[] buffer = new char[Buffer_Size];
        Stream responseStream = response.GetResponseStream();
        using(StreamReader reader = new StreamReader(responseStream)) {
          int index = 0;
          int readByte = 0;
          do {
              readByte = reader.Read(buffer, index, 256);
              index += readByte;
          }
          while (readByte != 0);
          response.Close();
        }
        string result = new string(buffer);
        result = result.TrimEnd(new char[] {'\0'});
        return result;
    }
platon
  • 5,310
  • 1
  • 22
  • 24
  • What if an exception is thrown in the middle of all this code? Will you leave all those streams leaking memory into the desert? Also are you really writing all this code to download a remote web page? – Darin Dimitrov Jul 11 '11 at 22:16
  • As far as I see, the exception can be thrown in the reader.Read method. So, it can be surrounded by either try ... finally brackets or using. Also, I really wrote this code approximately 1 year ago to download an adv site content to analyze it home. I've changed the code. – platon Jul 11 '11 at 22:23
  • 1
    an exception could be thrown anytime on any single line of code we write. That's the precise reason why should always ensure to properly dispose IDisposable objects such as streams: by using `using` blocks. But in this particular case why reinventing wheels and writing all this code when the .NET framework already provides us with it: WebClient, as shown in my answer. – Darin Dimitrov Jul 11 '11 at 22:25
  • 2
    Your solution is better since it is shorter. Just a single line of code. I wrote it here, because sometimes it becomes important to be able to change the part of a method provided by MS. Here the example is easy. You can pass the required encoding to the StreamReader constructor and do not rely on the WebClient's DownloadString method which tries to guess the encoding itself. – platon Jul 11 '11 at 22:38