Click to Rate and Give Feedback
TechNet
TechNet Library
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
FtpWebRequest..::.BeginGetResponse Method

Begins sending a request and receiving a response from an FTP server asynchronously.

Namespace:  System.Net
Assembly:  System (in System.dll)
Visual Basic
<HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading := True)> _
Public Overrides Function BeginGetResponse ( _
    callback As AsyncCallback, _
    state As Object _
) As IAsyncResult
C#
[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
public override IAsyncResult BeginGetResponse(
    AsyncCallback callback,
    Object state
)
Visual C++
[HostProtectionAttribute(SecurityAction::LinkDemand, ExternalThreading = true)]
public:
virtual IAsyncResult^ BeginGetResponse(
    AsyncCallback^ callback, 
    Object^ state
) override
F#
[<HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)>]
abstract BeginGetResponse : 
        callback:AsyncCallback * 
        state:Object -> IAsyncResult 
[<HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)>]
override BeginGetResponse : 
        callback:AsyncCallback * 
        state:Object -> IAsyncResult 

Parameters

callback
Type: System..::.AsyncCallback
An AsyncCallback delegate that references the method to invoke when the operation is complete.
state
Type: System..::.Object
A user-defined object that contains information about the operation. This object is passed to the callback delegate when the operation completes.

Return Value

Type: System..::.IAsyncResult
An IAsyncResult instance that indicates the status of the operation.
ExceptionCondition
InvalidOperationException

GetResponse or BeginGetResponse has already been called for this instance.

You must complete the asynchronous operation by calling the EndGetResponse method. Typically, EndGetResponse is called by the method referenced by callback. To determine the state of the operation, check the properties in the IAsyncResult object returned by the BeginGetResponse method.

If the Proxy property is set, either directly or in a configuration file, communications with the FTP server are made through the specified proxy.

BeginGetResponse does not block while waiting for the response from the server. To block, call the GetResponse method in place of BeginGetResponse.

For more information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.

NoteNote

If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Notes to Callers

This method generates network traffic.

The following code example demonstrates ending an asynchronous operation to get a request's stream, and then starting a request to get the response. This code example is part of a larger example provided for the FtpWebRequest class overview.

Visual Basic
Private Shared Sub EndGetStreamCallback(ByVal ar As IAsyncResult)
    Dim state As FtpState = CType(ar.AsyncState, FtpState)

    Dim requestStream As Stream = Nothing
    ' End the asynchronous call to get the request stream.
    Try
        requestStream = state.Request.EndGetRequestStream(ar)
        ' Copy the file contents to the request stream.
        Const bufferLength As Integer = 2048
        Dim buffer(bufferLength - 1) As Byte
        Dim count As Integer = 0
        Dim readBytes As Integer = 0
        Dim stream As FileStream = File.OpenRead(state.FileName)
        Do
            readBytes = stream.Read(buffer, 0, bufferLength)
            requestStream.Write(buffer, 0, readBytes)
            count += readBytes
        Loop 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(AddressOf EndGetResponseCallback), state)
        ' Return exceptions to the main application thread.
    Catch e As Exception
        Console.WriteLine("Could not get the request stream.")
        state.OperationException = e
        state.OperationComplete.Set()
        Return
    End Try

End Sub
C#
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;
    }

}
Visual C++
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;
      }
   }

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker