コード例 #1
0
        /// <summary>
        /// Starts a data download and conversion operation.
        /// </summary>
        /// <param name="query">The query that is used for creating the request.</param>
        /// <exception cref="System.ArgumentException">Thrown when the passed query is invalid.</exception>
        /// <returns>The response object containing the generic result data and additional information.</returns>
        protected StreamResponse Download(Uri url)
        {
            DateTime startTime = DateTime.UtcNow;
            ConnectionState state = ConnectionState.Success;
            Exception dlException = null;
            int size = -1;
            System.IO.Stream result = null;

            HttpWebRequest wr = this.CreateWebRequest(url);

            try
            {
                this.AddDownload(wr, startTime, null);

                using (HttpWebResponse resp = (HttpWebResponse)wr.GetResponse())
                {
                    using (System.IO.Stream stream = resp.GetResponseStream())
                    {
                        result = MaasOne.MyHelper.CopyStream(stream);
                        resp.Close();
                        if (result != null && result.CanSeek) int.TryParse(result.Length.ToString(), out size);
                    }
                }
            }
            catch (WebException ex)
            {
                state = ConnectionState.ErrorOccured;
                if (ex.Status == WebExceptionStatus.RequestCanceled)
                { state = ConnectionState.Canceled; }
                else if (ex.Status == WebExceptionStatus.Timeout)
                { state = ConnectionState.Timeout; }
                dlException = ex;
            }
            catch (Exception ex)
            {
                state = ConnectionState.ErrorOccured;
                dlException = this.GetOrCreateWebException(ex);
            }

            this.RemoveDownload(wr);

            ConnectionInfo conn = new ConnectionInfo(state, dlException, wr.Timeout, size, startTime, DateTime.UtcNow);

            return new StreamResponse(conn, result);
        }
コード例 #2
0
        private void ResponseDownloadCompleted(IAsyncResult result)
        {
            AsyncDownloadArgs asyncArgs = (AsyncDownloadArgs)result.AsyncState;
            ConnectionState state = ConnectionState.Success;
            Exception dlException = null;
            int size = 0;
            System.IO.Stream res = null;

            if (!asyncArgs.TimedOut)
            {
                try
                {
                    using (HttpWebResponse resp = (HttpWebResponse)asyncArgs.WR.EndGetResponse(result))
                    {
                        using (System.IO.Stream stream = resp.GetResponseStream())
                        {
                            res = MyHelper.CopyStream(stream);
#if !(NETFX_CORE)
                            resp.Close();
#else
                            resp.Dispose();
#endif
                            if (res != null && res.CanSeek) int.TryParse(res.Length.ToString(), out size);
                        }
                    }
                }
                catch (WebException ex)
                {
                    state = ConnectionState.ErrorOccured;
                    if (ex.Status == WebExceptionStatus.RequestCanceled)
                        state = ConnectionState.Canceled;
                    dlException = ex;
                }
                catch (Exception ex)
                {
                    state = ConnectionState.ErrorOccured;
                    dlException = this.GetOrCreateWebException(ex);
                }
            }
            else
            {
                state = ConnectionState.Timeout;
            }


            this.RemoveDownload(asyncArgs.WR);

            ConnectionInfo conn = new ConnectionInfo(state, dlException, asyncArgs.Timeout, size, asyncArgs.StartTime, DateTime.UtcNow);

            StreamResponse strResp = new StreamResponse(conn, res);
#if !(NETFX_CORE)
            System.ComponentModel.AsyncOperationManager.CreateOperation(this).Post(new System.Threading.SendOrPostCallback(delegate(object obj)
            {
                object[] arr = (object[])obj;
                RaiseAsyncDownloadCompleted((StreamResponse)arr[0], arr[1]);

            }), new object[] {
                    strResp,
                    asyncArgs.UserArgs
                });
#else
            RaiseAsyncDownloadCompleted(strResp, asyncArgs.UserArgs);
#endif
        }
コード例 #3
0
        /// <summary>
        /// Starts an async data download and conversion operation.
        /// </summary>
        /// <param name="query">The query that is used for creating the request.</param>
        /// <exception cref="System.ArgumentException">Thrown when the <see cref="DefaultQuery"/> is invalid.</exception>
        /// <returns>The task object representing the async download operation.</returns>
        protected async System.Threading.Tasks.Task<StreamResponse> DownloadTaskAsync(Uri url)
        {
            DateTime startTime = DateTime.UtcNow;

            System.Net.Http.HttpClient hc = new System.Net.Http.HttpClient();
            hc.Timeout = TimeSpan.FromMilliseconds(this.Timeout);


            ConnectionState state = ConnectionState.Success;
            Exception dlException = null;
            int size = -1;
            System.IO.Stream result = null;

            this.AddDownload(hc, startTime, null);

            var cts = new System.Threading.CancellationTokenSource();
            try
            {
                startTime = DateTime.Now;
                using (System.Net.Http.HttpResponseMessage resp = await hc.GetAsync(url, cts.Token))
                {
                    using (System.IO.Stream stream = await resp.Content.ReadAsStreamAsync())
                    {
                        result = MaasOne.MyHelper.CopyStream(stream);

                        resp.Dispose();
                        if (result != null && result.CanSeek) int.TryParse(result.Length.ToString(), out size);
                    }
                }
            }
            catch (WebException ex)
            {
                state = ConnectionState.ErrorOccured;
                if (ex.Status == WebExceptionStatus.RequestCanceled)
                { state = ConnectionState.Canceled; }
                dlException = ex;
            }
            catch (System.Threading.Tasks.TaskCanceledException ex)
            {
                if (ex.CancellationToken == cts.Token)
                { state = ConnectionState.Canceled; }
                else
                { state = ConnectionState.Timeout; }
                dlException = this.GetOrCreateWebException(ex);
            }
            catch (Exception ex)
            {
                state = ConnectionState.ErrorOccured;
                dlException = this.GetOrCreateWebException(ex);
            }

            this.RemoveDownload(hc);

            ConnectionInfo conn = new ConnectionInfo(state, dlException, (int)hc.Timeout.TotalMilliseconds, size, startTime, DateTime.UtcNow);

            return new StreamResponse(conn, result);
        }
コード例 #4
0
        /// <summary>
        /// Starts an asynchronous data download and conversion operation.
        /// </summary>
        /// <exception cref="System.ArgumentException">Thrown when the <see cref="DefaultQuery"/> is invalid.</exception>
        /// <param name="query">The query that is used for creating the request.</param>
        /// <param name="userArgs">Individual user arguments that will be passed to the <see cref="AsyncDownloadCompletedEventHandler{T}"/> when <see cref="AsyncDownloadCompleted"/> event is fired.</param>
        /// <remarks><see cref="AsyncDownloadCompleted"/> event is fired when download and conversion operation is completed.</remarks>
        protected void DownloadAsync(Uri url, object userArgs)
        {
            DateTime startTime = DateTime.UtcNow;

            HttpWebRequest wr = this.CreateWebRequest(url);

            try
            {
                this.AddDownload(wr, startTime, userArgs);

                AsyncDownloadArgs asyncArgs = new AsyncDownloadArgs(wr, userArgs, startTime, this.Timeout);

                IAsyncResult res = wr.BeginGetResponse(new AsyncCallback(this.ResponseDownloadCompleted), asyncArgs);
#if !(NETFX_CORE)
                System.Threading.ThreadPool.RegisterWaitForSingleObject(res.AsyncWaitHandle,
                                                                        new System.Threading.WaitOrTimerCallback((object state, bool timedOut) =>
                                                                        { if (timedOut) this.TimeoutAsync(state); }),
                                                                        asyncArgs,
                                                                        this.Timeout,
                                                                        true);
#else
                System.Threading.Tasks.Task.Factory.StartNew(() =>
                {
                    if (res.AsyncWaitHandle.WaitOne(asyncArgs.Timeout) == false)
                        this.TimeoutAsync(asyncArgs);
                    return;
                });
#endif
            }
            catch (Exception ex)
            {
                System.Net.WebException dlException = this.GetOrCreateWebException(ex);
                ConnectionInfo conn = new ConnectionInfo(ConnectionState.ErrorOccured, dlException, this.Timeout, -1, startTime, DateTime.UtcNow);
                this.RaiseAsyncDownloadCompleted(new StreamResponse(conn, null), userArgs);
            }
        }
コード例 #5
0
ファイル: StreamResponse.cs プロジェクト: jrwren/yahoofinance
 internal StreamResponse(ConnectionInfo connInfo, System.IO.Stream result) : base(connInfo, result) { }
コード例 #6
0
ファイル: ResponseBase.cs プロジェクト: jrwren/yahoofinance
 internal ResponseBase(ConnectionInfo connInfo, object result)
 {
     this.Connection = connInfo;
     this.ResultBase = result;
 }