FtpWebRequest.BeginGetRequestStream(AsyncCallback, Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
開始非同步開啟請求的內容流以供撰寫。
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 該方法來完成非同步操作。 通常, EndGetRequestStream 是由所 callback參考的方法所呼叫。 要判斷操作狀態,請檢查此方法回傳物件 IAsyncResult 中的屬性。
此方法在等待串流時不會阻擋。 要封鎖,請用電話 GetRequestStream 代替這個方法。
關於使用非同步程式設計模型的詳細資訊,請參見 「非同步呼叫同步方法」。
Note
當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 欲了解更多資訊,請參閱.NET框架中的網絡追蹤。
給呼叫者的注意事項
此方法產生網路流量。