コード例 #1
0
        private void RiseFtpСredentialsNeed(FtpСredentialsNeedEventArgs e)
        {
            EventHandler <FtpСredentialsNeedEventArgs> handler = FtpСredentialsNeed;

            if (handler != null)
            {
                handler(this, e);
            }
        }
コード例 #2
0
        /// <summary>
        /// Начинает закачку данных с опеределеного ресураса.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="filePath">Путь к закачиваемому файлу. </param>
        public void Start(string url, string filePath)
        {
            _filePath = filePath;
            var fileUri = new Uri(url);

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            } //if

            if (fileUri.Scheme == Uri.UriSchemeHttp)
            {
                _state         = new HttpWebRequestState(BUFFER_SIZE);
                _state.request = WebRequest.Create(fileUri);
            }
            else if (fileUri.Scheme == Uri.UriSchemeFtp)
            {
                var args = new FtpСredentialsNeedEventArgs();

                RiseFtpСredentialsNeed(args);
                _state                     = new FtpWebRequestState(BUFFER_SIZE);
                _state.request             = FtpWebRequest.Create(fileUri);
                _state.request.Credentials = new NetworkCredential(args.Login, args.Password);

                // Set FTP-specific stuff
                ((FtpWebRequest)_state.request).KeepAlive = false;

                //Проверка на существование системного proxy
                var systemProxy = WebRequest.GetSystemWebProxy();

                if (systemProxy.IsBypassed(new Uri(url)))
                {
                    // First thing we do is get file size.  2nd step, done later,
                    // will be to download actual file.
                    _state.request.Method = WebRequestMethods.Ftp.GetFileSize;
                    _state.FtpMethod      = WebRequestMethods.Ftp.GetFileSize;
                }
                else
                {
                    _state.request.Method = WebRequestMethods.Ftp.DownloadFile;
                    _state.FtpMethod      = WebRequestMethods.Ftp.DownloadFile;
                } //else
            }
            else
            {
                throw new Exception("Ошибочная схема: " + fileUri.Scheme);
            } //else

            _state.FileUri = fileUri;

            _state.TransferStart = DateTime.Now;

            // Start the asynchronous request.
            _state.request.BeginGetResponse(RespCallback, null);
        }