WebClient.OpenWriteCompleted 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在打开流以将数据写入资源完成的异步操作时发生。
public:
event System::Net::OpenWriteCompletedEventHandler ^ OpenWriteCompleted;
public event System.Net.OpenWriteCompletedEventHandler? OpenWriteCompleted;
public event System.Net.OpenWriteCompletedEventHandler OpenWriteCompleted;
member this.OpenWriteCompleted : System.Net.OpenWriteCompletedEventHandler
Public Custom Event OpenWriteCompleted As OpenWriteCompletedEventHandler
Public Event OpenWriteCompleted As OpenWriteCompletedEventHandler
事件类型
示例
下面的代码示例演示如何为此事件设置事件处理程序。
void OpenResourceForWriting2( String^ address )
{
WebClient^ client = gcnew WebClient;
Uri ^uri = gcnew Uri(address);
// Specify that the OpenWriteCallback method gets called
// when the writeable stream is available.
client->OpenWriteCompleted += gcnew OpenWriteCompletedEventHandler( OpenWriteCallback2 );
client->OpenWriteAsync( uri, "POST" );
// Applications can perform other tasks
// while waiting for the upload to complete.
}
public static void OpenResourceForWriting2(string address)
{
WebClient client = new WebClient();
Uri uri = new Uri(address);
// Specify that the OpenWriteCallback method gets called
// when the writeable stream is available.
client.OpenWriteCompleted += new OpenWriteCompletedEventHandler(OpenWriteCallback2);
client.OpenWriteAsync(uri, "POST");
// Applications can perform other tasks
// while waiting for the upload to complete.
}
Public Shared Sub OpenResourceForWriting2(ByVal address As String)
Dim client As WebClient = New WebClient()
' Specify that the OpenWriteCallback method gets called
' when the writeable stream is available.
AddHandler client.OpenWriteCompleted, AddressOf OpenWriteCallback2
Dim uri as Uri = New Uri(address)
client.OpenWriteAsync(uri, "POST")
' Applications can perform other tasks
' while waiting for the upload to complete.
End Sub
下面的代码示例演示此事件的处理程序的实现。
void OpenWriteCallback2( Object^ /*sender*/, OpenWriteCompletedEventArgs^ e )
{
Stream^ body = nullptr;
StreamWriter^ s = nullptr;
try
{
body = dynamic_cast<Stream^>(e->Result);
s = gcnew StreamWriter( body );
s->AutoFlush = true;
s->Write( "This is content data to be sent to the server." );
}
finally
{
if ( s != nullptr )
{
s->Close();
}
if ( body != nullptr )
{
body->Close();
}
}
}
private static void OpenWriteCallback2(Object sender, OpenWriteCompletedEventArgs e)
{
Stream body = null;
StreamWriter s = null;
try
{
body = (Stream)e.Result;
s = new StreamWriter(body);
s.AutoFlush = true;
s.Write("This is content data to be sent to the server.");
}
finally
{
if (s != null)
{
s.Close();
}
if (body != null)
{
body.Close();
}
}
}
Private Shared Sub OpenWriteCallback2(ByVal sender As Object, ByVal e As OpenWriteCompletedEventArgs)
Dim body As Stream = Nothing
Dim s As StreamWriter = Nothing
Try
body = CType(e.Result, Stream)
s = New StreamWriter(body)
s.AutoFlush = True
s.Write("This is content data to be sent to the server.")
Finally
If Not s Is Nothing Then
s.Close()
End If
If Not body Is Nothing Then
body.Close()
End If
End Try
End Sub
注解
谨慎
WebRequest
、HttpWebRequest
、ServicePoint
和 WebClient
已过时,不应将其用于新开发。 请改用 HttpClient。
每次异步操作打开用于将数据发送到资源完成的流时都会引发此事件。 这些操作通过调用 OpenWriteAsync 方法启动。
OpenWriteCompletedEventHandler 是此事件的委托。 OpenWriteCompletedEventArgs 类为事件处理程序提供事件数据。
有关如何处理事件的详细信息,请参阅 处理和引发事件。