Retrieving HTTP content in .NET

Firing events

Other versions of the GetUrl() method return a StreamReader object and yet another version GetUrlEvents() that fires an OnReceiveData event whenever data arrives in the buffer. The event provides a current byte and total byte count (if available) as well as two flags Done and Cancel. Done lets you know when the request is finished, while the Cancel flag lets your code stop downloading data.

To run with the event enabled you just hook up an event handler to the event:

HttpWebRequest loHttp = new wwHttp();

loHttp.OnReceiveData += new
wwHttp.OnReceiveDataHandler(this.loHttp_OnReceiveData);

string lcHtml = loHttp.GetUrlEvents(this.txtUrl.Text.TrimEnd(),4096);

this.txtHTML.Text = lcHtml;

loHttp.OnReceiveData -=
  new wwHttp.OnReceiveDataHandler(
          this.loHttp_OnReceiveData);

Make sure to disconnect the handler at the end of your request, or set it up in a static location that runs only one time. The event handler method can then do some work with the data in the OnReceiveDataArgs object returned to you (Listing 3):

Listing 4: Implementing the wwHttp::OnReceiveData event

private void loHttp_OnReceiveData(object sender,
         wwHttp.OnReceiveDataEventArgs e) {
  if (e.Done)
   MessageBox.Show("Download Complete!");
  else if (e.NumberOfReads == 20) {
   MessageBox.Show("Cancelling... too large.");
   e.Cancel = true;
}
  else
   this.oStatus.Panels[0].Text =
     e.CurrentByteCount.ToString() + " of " +
     e.TotalBytes.ToString() + " bytes read";
}

Using the event is easy. Creating the event on the wwHttp class is a bit more involved and requires three steps:

First the actual event needs to be defined on the class:

public event OnReceiveDataHandler OnReceiveData;

Next the event's arguments need to be wrapped up into a class contains the arguments as properties:

public class OnReceiveDataEventArgs
{
  public long CurrentByteCount=0;
  public long TotalBytes = 0;
  public int NumberOfReads = 0;
  public char [] CurrentChunk;
  public bool Done = false;
  public bool Cancel = false;
}

Creating a public Delegate which acts as the method signature for the Event to be called:

public delegate void
 OnReceiveDataHandler(object sender,
           OnReceiveDataEventArgs e);

You only need to define this delegate if you want to pass custom parameters. If no parameters are required you can just define your event use the standard System.EventHandler delegate. These three pieces make up the event interface.

To actually fire the event you can simply run your code and call the call the function pointer that the user assigned to the event. Here's the relevant code from wwHttp that demonstrates how the Response loop is read and how the event is fired on each update cycle.

Listing 5: Reading the Response Stream and firing events

StreamReader oHttpResponse = this.GetUrlStream(Url);
if (oHttpResponse == null)
  return "";

long lnSize = BufferSize;

// *** Use StringBuilder to create the result string
StringBuilder loWriter = new StringBuilder((int) lnSize);
// *** Create the argument structure used as event parm
OnReceiveDataEventArgs oArgs = new OnReceiveDataEventArgs();
oArgs.TotalBytes = lnSize;

while (lnSize > 0) {
  lnSize = oHttpResponse.Read(lcTemp,0,(int) BufferSize);
  if (lnSize > 0)
  {
   loWriter.Append(lcTemp, 0,(int) lnSize);
   lnCount++;
   lnTotalBytes += lnSize;

   // *** Raise an event if hooked up
   if (this.OnReceiveData != null)
   {
     /// *** Update the event handler
     oArgs.CurrentByteCount = lnTotalBytes;
     oArgs.NumberOfReads = lnCount;
     oArgs.CurrentChunk = lcTemp;

    // *** Call the event method
     this.OnReceiveData(this,oArgs);

     // *** Check for cancelled flag
     if (oArgs.Cancel)
      goto CloseDown;
   }
  }
} // while

The key to this code is the delegate OnReceiveData. It acts a function pointer which points the assigned method on the form in the example above. From within the stream reading loop this method is called every time a new buffer is retrieved.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer Science is no more about computers than astronomy is about telescopes.” - E. W. Dijkstra