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..::.GetResponse Method

Returns the FTP server response.

Namespace:  System.Net
Assembly:  System (in System.dll)
Visual Basic
Public Overrides Function GetResponse As WebResponse
C#
public override WebResponse GetResponse()
Visual C++
public:
virtual WebResponse^ GetResponse() override
F#
abstract GetResponse : unit -> WebResponse 
override GetResponse : unit -> WebResponse 

Return Value

Type: System.Net..::.WebResponse
A WebResponse reference that contains an FtpWebResponse instance. This object contains the FTP server's response to the request.
ExceptionCondition
InvalidOperationException

GetResponse or BeginGetResponse has already been called for this instance.

- or -

An HTTP proxy is enabled, and you attempted to use an FTP command other than WebRequestMethods..::.Ftp..::.DownloadFile, WebRequestMethods..::.Ftp..::.ListDirectory, or WebRequestMethods..::.Ftp..::.ListDirectoryDetails.

WebException

EnableSsl is set to true, but the server does not support this feature.

- or -

A Timeout was specified and the timeout has expired.

To access the FTP-specific properties, you must cast the WebResponse object returned by this method to FtpWebResponse.

GetResponse causes a control connection to be established, and might also create a data connection. GetResponse blocks until the response is received. To prevent this, you can perform this operation asynchronously by calling the BeginGetResponse and EndGetResponse methods in place of GetResponse.

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

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

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

NoteNote

Multiple calls to GetResponse return the same response object; the request is not reissued.

Notes to Callers

This method generates network traffic.

The following code example demonstrates copying a file to a request's data stream and sending a request to append data to a file to the server. The example calls GetResponse to send the request and block until the response is returned by the server.

Visual Basic
Public Shared Function AppendFileOnServer(ByVal fileName As String, ByVal serverUri As Uri) As Boolean
    ' The URI described by serverUri should use the ftp:// scheme.
    ' It contains the name of the file on the server.
    ' Example: ftp://contoso.com/someFile.txt. 
    ' The fileName parameter identifies the file containing 
    ' the data to be appended to the file on the server.

    If serverUri.Scheme <> Uri.UriSchemeFtp Then
        Return False
    End If
    ' Get the object used to communicate with the server.
    Dim request As FtpWebRequest = CType(WebRequest.Create(serverUri), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.AppendFile

    Dim sourceStream As New StreamReader(fileName)
    Dim fileContents() As Byte = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
    sourceStream.Close()
    request.ContentLength = fileContents.Length

    ' This example assumes the FTP site uses anonymous logon.
    request.Credentials = New NetworkCredential("anonymous", "janeDoe@contoso.com")
    Dim requestStream As Stream = request.GetRequestStream()
    requestStream.Write(fileContents, 0, fileContents.Length)
    requestStream.Close()
    Dim response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)

    Console.WriteLine("Append status: {0}", response.StatusDescription)

    response.Close()
    Return True
End Function
C#
public static bool AppendFileOnServer(string fileName, Uri serverUri)
{
    // The URI described by serverUri should use the ftp:// scheme.
    // It contains the name of the file on the server.
    // Example: ftp://contoso.com/someFile.txt. 
    // The fileName parameter identifies the file containing 
    // the data to be appended to the file on the server.

    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.Method = WebRequestMethods.Ftp.AppendFile;

    StreamReader sourceStream = new StreamReader(fileName);
    byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    request.ContentLength = fileContents.Length;

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);
    requestStream.Close();
    FtpWebResponse response = (FtpWebResponse) request.GetResponse();

    Console.WriteLine("Append status: {0}",response.StatusDescription);

    response.Close();  
    return true;
}
Visual C++
static bool AppendFileOnServer( String^ fileName, Uri^ serverUri )
{
   // The URI described by serverUri should use the ftp:// scheme.
   // It contains the name of the file on the server.
   // Example: ftp://contoso.com/someFile.txt. 
   // The fileName parameter identifies the file containing 
   // the data to be appended to the file on the server.
   if ( serverUri->Scheme != Uri::UriSchemeFtp )
   {
      return false;
   }

   // Get the object used to communicate with the server.
   FtpWebRequest^ request = dynamic_cast<FtpWebRequest^>(WebRequest::Create( serverUri ));
   request->Method = WebRequestMethods::Ftp::AppendFile;
   StreamReader^ sourceStream = gcnew StreamReader( fileName );
   array<Byte>^fileContents = Encoding::UTF8->GetBytes( sourceStream->ReadToEnd() );
   sourceStream->Close();
   request->ContentLength = fileContents->Length;

   // This example assumes the FTP site uses anonymous logon.
   request->Credentials = gcnew NetworkCredential( "anonymous","janeDoe@contoso.com" );
   Stream^ requestStream = request->GetRequestStream();
   requestStream->Write( fileContents, 0, fileContents->Length );
   requestStream->Close();
   FtpWebResponse^ response = dynamic_cast<FtpWebResponse^>(request->GetResponse());
   Console::WriteLine( "Append status: {0}", response->StatusDescription );
   response->Close();
   return true;
}

.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
Ctype Superfluo ?      Mario Formosa   |   Edit   |   Show History

Ho notato che pur sostituendo la riga

Dim request As Net.FtpWebRequest = CType(Net.WebRequest.Create(serverUri), Net.FtpWebRequest)
con
Dim request As Net.FtpWebRequest = Net.FtpWebRequest.Create(UriString)

e la riga
Dim response As Net.FtpWebResponse = CType(request.GetResponse(), Net.FtpWebResponse)
con
Dim response As Net.FtpWebResponse = request.GetResponse()

l'esempio va a buon fine comunque.

Tags What's this?: Add a tag
Flag as ContentBug
ContentLength      bhuber7   |   Edit   |   Show History
I wonder why this example sets the request.ContentLength property when it's ignored by the FtpWebRequest class anyway.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker