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.
//
static void Main()
{
   array<String^>^args = Environment::GetCommandLineArgs();

   // 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 = gcnew Uri( args[ 1 ] );
   String^ fileName = args[ 2 ];
   FtpState^ state = gcnew FtpState;
   FtpWebRequest ^ request = dynamic_cast<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 = gcnew 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( gcnew 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 != nullptr )
   {
      throw state->OperationException;
   }
   else
   {
      Console::WriteLine( "The operation completed - {0}", state->StatusDescription );
   }
}
// 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 합니다.

비동기 프로그래밍 모델 사용에 대한 자세한 내용은 동기 메서드 비동기 호출을 참조하세요.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

호출자 참고

이 메서드는 네트워크 트래픽을 생성합니다.

적용 대상

추가 정보