FtpWebRequest.BeginGetRequestStream(AsyncCallback, Object) 方法

定义

开始异步打开请求的内容流进行写入。

public:
 override IAsyncResult ^ BeginGetRequestStream(AsyncCallback ^ callback, System::Object ^ state);
public override IAsyncResult BeginGetRequestStream(AsyncCallback? callback, object? state);
public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state);
override this.BeginGetRequestStream : AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginGetRequestStream (callback As AsyncCallback, state As Object) As IAsyncResult

参数

callback
AsyncCallback

一个 AsyncCallback 委托,该委托引用在作完成时要调用的方法。

state
Object

包含有关操作的信息的用户定义对象。 此操作完成后,此对象将 callback 传递给委托。

返回

指示 IAsyncResult 操作状态的实例。

例外

以前对此方法的调用或 GetRequestStream() 尚未完成。

无法建立与 FTP 服务器的连接。

Method 属性未设置为 UploadFile.

示例

下面的代码示例演示如何开始异步操作以获取请求的流。 此代码示例是为类概述提供的大型示例的 FtpWebRequest 一部分。

// Command line arguments are two strings:
// 1. The url that is the name of the file being uploaded to the server.
// 2. The name of the file on the local machine.
//
public static void Main(string[] args)
{
    // Create a Uri instance with the specified URI string.
    // If the URI is not correctly formed, the Uri constructor
    // will throw an exception.
    ManualResetEvent waitObject;

    Uri target = new Uri (args[0]);
    string fileName = args[1];
    FtpState state = new FtpState();
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
    request.Method = WebRequestMethods.Ftp.UploadFile;

    // This example uses anonymous logon.
    // The request is anonymous by default; the credential does not have to be specified.
    // The example specifies the credential only to
    // control how actions are logged on the server.

    request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

    // Store the request in the object that we pass into the
    // asynchronous operations.
    state.Request = request;
    state.FileName = fileName;

    // Get the event to wait on.
    waitObject = state.OperationComplete;

    // Asynchronously get the stream for the file contents.
    request.BeginGetRequestStream(
        new AsyncCallback (EndGetStreamCallback),
        state
    );

    // Block the current thread until all operations are complete.
    waitObject.WaitOne();

    // The operations either completed or threw an exception.
    if (state.OperationException != null)
    {
        throw state.OperationException;
    }
    else
    {
        Console.WriteLine("The operation completed - {0}", state.StatusDescription);
    }
}

注解

必须通过调用 EndGetRequestStream 该方法来完成异步操作。 通常,由由 <a0/> 引用的方法调用。 若要确定操作的状态,请检查此方法返回的对象中的 IAsyncResult 属性。

此方法在等待流时不会阻止。 若要阻止,请调用 GetRequestStream 代替此方法。

有关使用异步编程模型的详细信息,请参阅 异步调用同步方法

注释

在应用程序中启用网络跟踪时,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的 Network Tracing

调用方说明

此方法生成网络流量。

适用于

另请参阅