FtpWebRequest.BeginGetResponse(AsyncCallback, Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
开始以异步方式向 FTP 服务器发送请求并从 FTP 服务器接收响应。
public:
override IAsyncResult ^ BeginGetResponse(AsyncCallback ^ callback, System::Object ^ state);
public override IAsyncResult BeginGetResponse (AsyncCallback? callback, object? state);
public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state);
override this.BeginGetResponse : AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginGetResponse (callback As AsyncCallback, state As Object) As IAsyncResult
参数
- callback
- AsyncCallback
一个 AsyncCallback 委托,它引用操作完成时要调用的方法。
- state
- Object
一个用户定义对象,其中包含操作的相关信息。 操作完成时,此对象传递给 callback
委托。
返回
IAsyncResult 实例,指示操作的状态。
例外
示例
下面的代码示例演示如何结束异步操作以获取请求的流,然后启动请求以获取响应。 此代码示例是为类概述提供的更大示例的 FtpWebRequest 一部分。
private:
static void EndGetStreamCallback( IAsyncResult^ ar )
{
FtpState^ state = dynamic_cast<FtpState^>(ar->AsyncState);
Stream^ requestStream = nullptr;
// End the asynchronous call to get the request stream.
try
{
requestStream = state->Request->EndGetRequestStream( ar );
// Copy the file contents to the request stream.
const int bufferLength = 2048;
array<Byte>^buffer = gcnew array<Byte>(bufferLength);
int count = 0;
int readBytes = 0;
FileStream^ stream = File::OpenRead( state->FileName );
do
{
readBytes = stream->Read( buffer, 0, bufferLength );
requestStream->Write( buffer, 0, bufferLength );
count += readBytes;
}
while ( readBytes != 0 );
Console::WriteLine( "Writing {0} bytes to the stream.", count );
// IMPORTANT: Close the request stream before sending the request.
requestStream->Close();
// Asynchronously get the response to the upload request.
state->Request->BeginGetResponse( gcnew AsyncCallback( EndGetResponseCallback ), state );
}
// Return exceptions to the main application thread.
catch ( Exception^ e )
{
Console::WriteLine( "Could not get the request stream." );
state->OperationException = e;
state->OperationComplete->Set();
return;
}
}
private static void EndGetStreamCallback(IAsyncResult ar)
{
FtpState state = (FtpState) ar.AsyncState;
Stream requestStream = null;
// End the asynchronous call to get the request stream.
try
{
requestStream = state.Request.EndGetRequestStream(ar);
// Copy the file contents to the request stream.
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream stream = File.OpenRead(state.FileName);
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
}
while (readBytes != 0);
Console.WriteLine ("Writing {0} bytes to the stream.", count);
// IMPORTANT: Close the request stream before sending the request.
requestStream.Close();
// Asynchronously get the response to the upload request.
state.Request.BeginGetResponse(
new AsyncCallback (EndGetResponseCallback),
state
);
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine("Could not get the request stream.");
state.OperationException = e;
state.OperationComplete.Set();
return;
}
}
注解
必须通过调用 EndGetResponse 方法来完成异步操作。 通常, EndGetResponse 由 引用的方法调用 callback
。 若要确定操作的状态,请检查 方法返回BeginGetResponse的 IAsyncResult 对象中的属性。
Proxy如果直接或在配置文件中设置了 属性,则通过指定的代理与 FTP 服务器的通信。
BeginGetResponse 在等待服务器响应时不会阻止。 若要阻止,请 GetResponse 调用 方法代替 BeginGetResponse。
有关使用异步编程模型的详细信息,请参阅 异步调用同步方法。
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪。
注意
WebException如果引发 ,请使用Response异常的 和 Status 属性来确定来自服务器的响应。
调用方说明
此方法生成网络流量。