Home   Cover Cover Cover Cover
 

Network resources

Chap4_Ex14.cs
using System;
using System.Net;
using System.IO;
using System.Text;

public class NetResources {
  static int MAXLEN = 100000;

  public static void Main(string[] args) {
    // Initialize the WebRequest.
    WebRequest myRequest = WebRequest.Create("http://dotnet.jku.at/index.html");

    // Return the response. 
    WebResponse myResponse = myRequest.GetResponse();

    Stream s = myResponse.GetResponseStream();
    byte[] data = new byte[MAXLEN];
    int pos = 0;
    int b = s.ReadByte();
    while(b>0 && pos < MAXLEN) {
      data[pos] = (byte)b;
      pos++;
      b = s.ReadByte();
    }
    
    s.Close();
    Console.Write(Encoding.ASCII.GetString(data));
    
    // Close the response to free resources.
    myResponse.Close();

  }
}