WebClient.DownloadStringCompleted Événement
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Se produit lorsqu’une opération de téléchargement de ressources asynchrone se termine.
public:
event System::Net::DownloadStringCompletedEventHandler ^ DownloadStringCompleted;
public event System.Net.DownloadStringCompletedEventHandler? DownloadStringCompleted;
public event System.Net.DownloadStringCompletedEventHandler DownloadStringCompleted;
member this.DownloadStringCompleted : System.Net.DownloadStringCompletedEventHandler
Public Custom Event DownloadStringCompleted As DownloadStringCompletedEventHandler
Public Event DownloadStringCompleted As DownloadStringCompletedEventHandler
Type d'événement
Exemples
L’exemple de code suivant illustre la définition d’un gestionnaire d’événements pour cet événement.
// Sample call : DownloadStringInBackground2 ("http://www.contoso.com/GameScores.html");
void DownloadStringInBackground2( String^ address )
{
WebClient^ client = gcnew WebClient;
Uri ^uri = gcnew Uri(address);
// Specify that the DownloadStringCallback2 method gets called
// when the download completes.
client->DownloadStringCompleted += gcnew DownloadStringCompletedEventHandler( DownloadStringCallback2 );
client->DownloadStringAsync( uri );
}
// Sample call : DownloadStringInBackground2 ("http://www.contoso.com/GameScores.html");
public static void DownloadStringInBackground2(string address)
{
WebClient client = new WebClient();
Uri uri = new Uri(address);
// Specify that the DownloadStringCallback2 method gets called
// when the download completes.
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback2);
client.DownloadStringAsync(uri);
}
' Sample call : DownloadStringInBackground2 ("http:' www.contoso.com/GameScores.html")
Public Shared Sub DownloadStringInBackground2(ByVal address As String)
Dim client As WebClient = New WebClient()
' Specify that the DownloadStringCallback2 method gets called
' when the download completes.
AddHandler client.DownloadStringCompleted, AddressOf DownloadStringCallback2
Dim uri as Uri = New Uri(address)
client.DownloadStringAsync(uri)
End Sub
L’exemple de code suivant montre une implémentation d’un gestionnaire pour cet événement.
void DownloadStringCallback2( Object^ /*sender*/, DownloadStringCompletedEventArgs^ e )
{
// If the request was not canceled and did not throw
// an exception, display the resource.
if ( !e->Cancelled && e->Error == nullptr )
{
String^ textString = dynamic_cast<String^>(e->Result);
Console::WriteLine( textString );
}
}
private static void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e)
{
// If the request was not canceled and did not throw
// an exception, display the resource.
if (!e.Cancelled && e.Error == null)
{
string textString = (string)e.Result;
Console.WriteLine(textString);
}
}
Private Shared Sub DownloadStringCallback2(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
' If the request was not canceled and did not throw
' an exception, display the resource.
If e.Cancelled = False AndAlso e.Error Is Nothing Then
Dim textString As String = CStr(e.Result)
Console.WriteLine(textString)
End If
End Sub
Remarques
Prudence
WebRequest
, HttpWebRequest
, ServicePoint
et WebClient
sont obsolètes et vous ne devez pas les utiliser pour le nouveau développement. Utilisez HttpClient à la place.
Cet événement est déclenché chaque fois qu’une opération asynchrone pour télécharger une ressource en tant que chaîne se termine. Ces opérations sont démarrées en appelant les méthodes DownloadStringAsync.
Le DownloadStringCompletedEventHandler est le délégué de cet événement. La classe DownloadStringCompletedEventArgs fournit au gestionnaire d’événements des données d’événement.
Pour plus d’informations sur la gestion des événements, consultez Gestion et déclenchement d’événements.