Ejemplo n.º 1
0
        /// <summary>
        /// Returns a value indicating if a connection can be made to the resource.
        /// </summary>
        public void CheckConnection()
        {
            if (State != WebOperationState.Idle)
            {
                return;
            }

            State              = WebOperationState.InProgress;
            Result             = WebOperationResult.None;
            _webRequest.Method = GetWebCheckConnectionMethod();

            IWebResponse response = null;

            try
            {
                OnWebOperationProgress(new WebOperationProgressChangedEventArgs(0, 0, State, Result));
                response = _webRequest.GetResponse();
                Result   = WebOperationResult.Completed;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }

                State = WebOperationState.Idle;
                OnWebOperationProgress(new WebOperationProgressChangedEventArgs(0, 0, State, Result));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Downloads the resource to a stream.
        /// </summary>
        /// <param name="stream">The stream used to receive the data.</param>
        public void Download(Stream stream)
        {
            if (State != WebOperationState.Idle)
            {
                return;
            }

            State              = WebOperationState.InProgress;
            Result             = WebOperationResult.None;
            _webRequest.Method = GetWebDownloadMethod();

            long totalBytesRead = 0;
            long totalLength;

            IWebResponse response = _webRequest.GetResponse();

            using (Stream responseStream = response.GetResponseStream())
            {
                totalLength = response.ContentLength;
                if (AutoSizeBuffer)
                {
                    Buffer = CalculateBufferSize(totalLength);
                }

                var buffer    = new byte[Buffer];
                int bytesRead = 0;

                do
                {
                    if (_cancel)
                    {
                        Result = WebOperationResult.Canceled;
                        break;
                    }

                    if (responseStream != null)
                    {
                        bytesRead = responseStream.Read(buffer, 0, buffer.Length);
                    }
                    stream.Write(buffer, 0, bytesRead);

                    totalBytesRead += bytesRead;
                    OnWebOperationProgress(new WebOperationProgressChangedEventArgs(totalBytesRead, totalLength, State, Result));
                }while (bytesRead > 0);
            }

            if (Result != WebOperationResult.Canceled && (totalLength < 0 || totalBytesRead == totalLength))
            {
                Result = WebOperationResult.Completed;
            }

            // Close the Response Stream
            response.Close();

            State = WebOperationState.Idle;
            OnWebOperationProgress(new WebOperationProgressChangedEventArgs(totalBytesRead, totalLength, State, Result));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Uploads the specified stream to the resource.
        /// </summary>
        /// <param name="stream">The stream to send to the resource.</param>
        public void Upload(Stream stream)
        {
            if (State != WebOperationState.Idle)
            {
                return;
            }

            State              = WebOperationState.InProgress;
            Result             = WebOperationResult.None;
            _webRequest.Method = GetWebUploadMethod();

            long totalBytesRead = 0;
            long totalLength    = stream.Length - stream.Position;

            if (AutoSizeBuffer)
            {
                Buffer = CalculateBufferSize(totalLength);
            }

            using (Stream requestStream = _webRequest.GetRequestStream())
            {
                var buffer = new byte[Buffer];
                int bytesRead;

                do
                {
                    if (_cancel)
                    {
                        Result = WebOperationResult.Canceled;
                        break;
                    }

                    bytesRead = stream.Read(buffer, 0, buffer.Length);
                    requestStream.Write(buffer, 0, bytesRead);

                    totalBytesRead += bytesRead;
                    OnWebOperationProgress(new WebOperationProgressChangedEventArgs(totalBytesRead, totalLength, State, Result));
                } while (bytesRead > 0);
            }

            if (Result != WebOperationResult.Canceled && totalBytesRead == totalLength)
            {
                Result = WebOperationResult.Completed;
            }

            IWebResponse response = _webRequest.GetResponse();

            response.Close();

            State = WebOperationState.Idle;
            OnWebOperationProgress(new WebOperationProgressChangedEventArgs(totalBytesRead, totalLength, State, Result));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the length of the data being received.
        /// </summary>
        /// <returns>The length of the data being received in bytes.</returns>
        public long GetDownloadLength()
        {
            if (State != WebOperationState.Idle)
            {
                return(0);
            }

            State              = WebOperationState.InProgress;
            Result             = WebOperationResult.None;
            _webRequest.Method = GetWebDownloadMethod();

            OnWebOperationProgress(new WebOperationProgressChangedEventArgs(0, 0, State, Result));
            IWebResponse response = _webRequest.GetResponse();
            long         length   = response.ContentLength;

            response.Close();

            State  = WebOperationState.Idle;
            Result = WebOperationResult.Completed;
            OnWebOperationProgress(new WebOperationProgressChangedEventArgs(0, length, State, Result));

            return(length);
        }