System.Net.FtpWebRequest.GetRequestStream C# (CSharp) Method

GetRequestStream() public method

Used to query for the Request stream of an FTP Request

public GetRequestStream ( ) : Stream
return System.IO.Stream
        public override Stream GetRequestStream()
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(this);
                NetEventSource.Info(this, $"Method: {_methodInfo.Method}");
            }

            try
            {
                if (_getRequestStreamStarted)
                {
                    throw new InvalidOperationException(SR.net_repcall);
                }
                _getRequestStreamStarted = true;
                if (!_methodInfo.IsUpload)
                {
                    throw new ProtocolViolationException(SR.net_nouploadonget);
                }
                CheckError();

                _startTime = DateTime.UtcNow;
                _remainingTimeout = Timeout;

                if (Timeout != System.Threading.Timeout.Infinite)
                {
                    _remainingTimeout = Timeout - (int)((DateTime.UtcNow - _startTime).TotalMilliseconds);

                    if (_remainingTimeout <= 0)
                    {
                        throw ExceptionHelper.TimeoutException;
                    }
                }

                FinishRequestStage(RequestStage.RequestStarted);
                SubmitRequest(false);
                FinishRequestStage(RequestStage.WriteReady);
                CheckError();

                if (_stream.CanTimeout)
                {
                    _stream.WriteTimeout = ReadWriteTimeout;
                    _stream.ReadTimeout = ReadWriteTimeout;
                }
            }
            catch (Exception exception)
            {
                if (NetEventSource.IsEnabled) NetEventSource.Error(this, exception);
                throw;
            }
            finally
            {
                if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
            }
            return _stream;
        }

Same methods

FtpWebRequest::GetRequestStream ( ) : System.IO.Stream

Usage Example

        public static bool WriteFileToFTP(string HostName, int Port, string UserName, string Password, string targetPath, byte[] fileBytes)
        {
            bool fileCopied = false;

            try
            {
                using (FtpClient ftp = new FtpClient(HostName, Port, UserName, Password))
                {
                    ftp.Connect();
                    System.Net.FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create("ftp://" + HostName + targetPath);
                    clsRequest.Credentials = new System.Net.NetworkCredential(UserName, Password);
                    clsRequest.Method      = System.Net.WebRequestMethods.Ftp.UploadFile;
                    System.IO.Stream clsStream = clsRequest.GetRequestStream();
                    clsStream.Write(fileBytes, 0, fileBytes.Length);
                    clsStream.Close();
                    clsStream.Dispose();
                    fileCopied = true;
                }
            }
            catch (Exception)
            {
                return(fileCopied);
            }
            return(fileCopied);
        }
All Usage Examples Of System.Net.FtpWebRequest::GetRequestStream