Пример #1
0
        /// <summary>
        /// Async HTTP Read Callback
        /// IAsyncResult asyncResult (input) -> data necesary to access the webdonwload instance, because
        /// This is a static method and has not the Webdownload instance
        /// </summary>
        private static void AsyncReadCallback(IAsyncResult asyncResult)
        {
            WebDownload webDL = (WebDownload)asyncResult.AsyncState;

            Stream responseStream = webDL.responseStream;

            try
            {
                int read = responseStream.EndRead(asyncResult);
                if (read > 0)
                {
                    webDL.ContentStream.Write(webDL.readBuffer, 0, read);
                    webDL.BytesProcessed += read;
                    webDL.OnProgressCallback(webDL.BytesProcessed, webDL.ContentLength);

                    IAsyncResult asynchronousResult = responseStream.BeginRead(webDL.readBuffer, 0, webDL.readBuffer.Length, new AsyncCallback(AsyncReadCallback), webDL);

                    return;
                }
                else
                {
                    if (webDL.BytesProcessed <= 0)
                    {
                        Exception myException = new Exception("400 No tile");

                        webDL.SaveException(myException);

                        webDL.AsyncFinishDownload();
                        return;
                    }

                    webDL.SaveException(null);
                    webDL.AsyncFinishDownload();
                }
            }
            catch (IOException ex)
            {
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncReadCallback(): IOException: " + ex.Message.ToString() + webDL.BytesProcessed);
                if (webDL.num_retry > 5)
                {
                    webDL.SaveException(new Exception("Unable to connect to the remote server several Async Read tries have been broke"));
                    webDL.AsyncFinishDownload();
                    return;
                }
                webDL.num_retry++;

                webDL.AsyncFinishPrepareRetry();

                webDL.DownloadAsync();
            }
            catch (WebException e)
            {
                if (webDL.timedOut == false)
                {
                    // request cancelled.
                    Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncReadCallback(): WebException: " + e.Status.ToString());
                    webDL.SaveException(e);
                    webDL.AsyncFinishDownload();
                    //webDL.Cancel();
                }
            }
            catch (NullReferenceException e)
            {
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncReadCallback(): NullReferenceException: " + e.Message);
                webDL.SaveException(e);
                webDL.AsyncFinishDownload();
            }
            catch (Exception e)
            {
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncReadCallback(): Exception: " + e.Message);
                webDL.SaveException(e);
                //Cancel();
                webDL.AsyncFinishDownload();
            }
        }
Пример #2
0
        /// <summary>
        /// Async HTTP response Callback
        /// IAsyncResult asyncResult (input) -> data necesary to access the webdonwload instance, because
        /// This is a static method and has not the Webdownload instance
        /// </summary>
        private static void AsyncResponseCallback(IAsyncResult asyncResult)
        {
            WebDownload webDL = (WebDownload)asyncResult.AsyncState;



            try
            {
                webDL.response = webDL.request.EndGetResponse(asyncResult) as HttpWebResponse;

                // only if server responds 200 OK
                if (webDL.response.StatusCode == HttpStatusCode.OK)
                {
                    webDL.ContentType     = webDL.response.ContentType;
                    webDL.ContentEncoding = webDL.response.ContentEncoding;

                    // Find the data size from the headers.
                    string strContentLength = webDL.response.Headers["Content-Length"];
                    if (strContentLength != null)
                    {
                        webDL.ContentLength = int.Parse(strContentLength, CultureInfo.InvariantCulture);
                    }
                    // If if (webDL.ContentLength <= 0) ?????

                    webDL.responseStream = webDL.response.GetResponseStream();

                    IAsyncResult result = webDL.responseStream.BeginRead(webDL.readBuffer, 0, webDL.readBuffer.Length, new AsyncCallback(AsyncReadCallback), webDL);
                    return;
                }
                else
                {
                    Exception myException = new Exception(webDL.response.StatusCode.ToString());

                    webDL.SaveException(myException);

                    webDL.AsyncFinishDownload();
                }
            }
            catch (WebException e)
            {
                if (webDL.timedOut == false)
                {
                    // request cancelled.
                    Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncResponseCallback(): WebException: " + e.Status.ToString());
                    webDL.SaveException(e);
                    webDL.AsyncFinishDownload();
                    //webDL.Cancel();
                }
            }
            catch (ArgumentNullException e)
            {
                // request cancelled.
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncResponseCallback(): ArgumentNullException: " + e.Message);
                webDL.SaveException(e);
                webDL.AsyncFinishDownload();
            }
            catch (InvalidOperationException e)
            {
                // request cancelled.
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncResponseCallback(): InvalidOperationException: " + e.Message);
                webDL.SaveException(e);
                webDL.AsyncFinishDownload();
            }
            catch (ArgumentException e)
            {
                // request cancelled.
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncResponseCallback(): ArgumentException: " + e.Message);
                webDL.SaveException(e);
                webDL.AsyncFinishDownload();
            }
            catch (NullReferenceException e)
            {
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncResponseCallback(): NullReferenceException: " + e.Message);
                webDL.SaveException(e);
                webDL.AsyncFinishDownload();
            }
            catch (Exception e)
            {
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncResponseCallback(): Exception: " + e.Message);
                webDL.SaveException(e);
                //Cancel();
                webDL.AsyncFinishDownload();
            }
        }