Export (0) Print
Expand All

FtpWebRequest.GetRequestStream Method

Retrieves the stream used to upload data to an FTP server.

Namespace:  System.Net
Assembly:  System (in System.dll)

public override Stream GetRequestStream()

Return Value

Type: System.IO.Stream
A writable Stream instance used to store data to be sent to the server by the current request.

ExceptionCondition
InvalidOperationException

BeginGetRequestStream has been called and has not completed.

- 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

A connection to the FTP server could not be established.

ProtocolViolationException

The Method property is not set to WebRequestMethods.Ftp.UploadFile or WebRequestMethods.Ftp.AppendFile.

Set the request properties before calling the GetRequestStream method. After writing the data to the stream, you must close the stream prior to sending the request.

If you have not set the Method property to UploadFile or AppendFile, you cannot get the stream.

GetRequestStream blocks while waiting for the stream. To prevent this, call the BeginGetRequestStream method in place of GetRequestStream.

NoteNote:

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

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 the server to upload the data and append it to a file.

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;
}
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 = dynamic_cast<FtpWebRequest*>(WebRequest::Create(serverUri));
        request->Method = FtpMethods::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 (S"anonymous",S"janeDoe@contoso.com");
        Stream* requestStream = request->GetRequestStream();
        requestStream->Write(fileContents, 0, fileContents->Length);
        requestStream->Close();
        FtpWebResponse* response = dynamic_cast<FtpWebResponse*> (request->GetResponse());

        Console::WriteLine(S"Append status: {0}",response->StatusDescription);

        response->Close();  
        return true;
    }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft