WebClient.OpenReadCompleted Event

Definition

Occurs when an asynchronous operation to open a stream containing a resource completes.

public event System.Net.OpenReadCompletedEventHandler? OpenReadCompleted;
public event System.Net.OpenReadCompletedEventHandler OpenReadCompleted;

Event Type

Examples

The following code example demonstrates setting an event handler for this event.

public static void OpenResourceForReading2(string address)
{
    WebClient client = new WebClient();
    Uri uri = new Uri(address);

    client.OpenReadCompleted += new OpenReadCompletedEventHandler(OpenReadCallback2);
    client.OpenReadAsync(uri);
}

The following code example shows an implementation of a handler for this event.

private static void OpenReadCallback2(Object sender, OpenReadCompletedEventArgs e)
{
    Stream reply = null;
    StreamReader s = null;

    try
    {
        reply = (Stream)e.Result;
        s = new StreamReader(reply);
        Console.WriteLine(s.ReadToEnd());
    }
    finally
    {
        if (s != null)
        {
            s.Close();
        }

        if (reply != null)
        {
            reply.Close();
        }
    }
}

Remarks

Caution

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

This event is raised each time an asynchronous operation to open a stream containing a resource completes. These operations are started by calling the OpenReadAsync methods.

The OpenReadCompletedEventHandler is the delegate for this event. The OpenReadCompletedEventArgs class provides the event handler with event data.

For more information about how to handle events, see Handling and Raising Events.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1