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

GetResponse() public method

public GetResponse ( ) : WebResponse
return WebResponse
        public override WebResponse GetResponse()
        {
            if (NetEventSource.IsEnabled)
            {
                if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
                if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"Method: {_methodInfo.Method}");
            }

            try
            {
                CheckError();

                if (_ftpWebResponse != null)
                {
                    return _ftpWebResponse;
                }

                if (_getResponseStarted)
                {
                    throw new InvalidOperationException(SR.net_repcall);
                }

                _getResponseStarted = true;

                _startTime = DateTime.UtcNow;
                _remainingTimeout = Timeout;

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

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

                RequestStage prev = FinishRequestStage(RequestStage.RequestStarted);
                if (prev >= RequestStage.RequestStarted)
                {
                    if (prev < RequestStage.ReadReady)
                    {
                        lock (_syncObject)
                        {
                            if (_requestStage < RequestStage.ReadReady)
                                _readAsyncResult = new LazyAsyncResult(null, null, null);
                        }

                        // GetRequeststream or BeginGetRequestStream has not finished yet
                        if (_readAsyncResult != null)
                            _readAsyncResult.InternalWaitForCompletion();

                        CheckError();
                    }
                }
                else
                {
                    SubmitRequest(false);
                    if (_methodInfo.IsUpload)
                        FinishRequestStage(RequestStage.WriteReady);
                    else
                        FinishRequestStage(RequestStage.ReadReady);
                    CheckError();

                    EnsureFtpWebResponse(null);
                }
            }
            catch (Exception exception)
            {
                if (NetEventSource.IsEnabled) NetEventSource.Error(this, exception);

                // if _exception == null, we are about to throw an exception to the user
                // and we haven't saved the exception, which also means we haven't dealt
                // with it. So just release the connection and log this for investigation.
                if (_exception == null)
                {
                    if (NetEventSource.IsEnabled) NetEventSource.Error(this, exception);
                    SetException(exception);
                    FinishRequestStage(RequestStage.CheckForError);
                }
                throw;
            }
            finally
            {
                if (NetEventSource.IsEnabled) NetEventSource.Exit(this, _ftpWebResponse);
            }
            return _ftpWebResponse;
        }

Same methods

FtpWebRequest::GetResponse ( ) : System.Net.WebResponse

Usage Example

Example #1
1
        /// <summary>
        /// 连接FTP的方法
        /// </summary>
        /// <param name="ftpuri">ftp服务器地址,端口</param>
        /// <param name="ftpUserID">用户名</param>
        /// <param name="ftpPassword">密码</param>
        public DownloadFtp(string ftpuri, string ftpUserID, string ftpPassword)
        {
            // 根据uri创建FtpWebRequest对象
              ft = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpuri));
              // ftp用户名和密码
              ft.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
              ft.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
             fr = (FtpWebResponse)ft.GetResponse();
              stream = fr.GetResponseStream();
              ////二进制文件读入
              //if (!fr.ContentType.ToLower().StartsWith("text/"))
              //{
              //    SaveBinaryFile(fr);
              //}
              ////文本文件
              //else
              //{
              string buffer = "", line;
              StreamReader reader = new StreamReader(stream);
              while ((line = reader.ReadLine()) != null)
              {
                  buffer += line + "\r\n";
              }

              //装入整个文件之后,接着就要把它保存为文本文件。
              SaveTextFile(buffer);
              //}
        }
All Usage Examples Of System.Net.FtpWebRequest::GetResponse