コード例 #1
0
ファイル: HttpTask.cs プロジェクト: safino9/priv10
        private static void ReadCallBack(IAsyncResult asyncResult)
        {
            int       Success = 0;
            int       ErrCode = 0;
            Exception Error   = null;
            HttpTask  task    = (HttpTask)asyncResult.AsyncState;

            try
            {
                int read = task.streamResponse.EndRead(asyncResult);
                // Read the HTML page and then print it to the console.
                if (read > 0)
                {
                    task.streamWriter.Write(task.BufferRead, 0, read);
                    task.mOffset += read;

                    int Percent = task.mLength > 0 ? (int)((Int64)100 * task.mOffset / task.mLength) : -1;
                    if (Percent != task.mOldPercent)
                    {
                        task.mOldPercent = Percent;
                        task.mDispatcher.Invoke(new Action(() =>
                        {
                            task.Progress?.Invoke(task, new ProgressEventArgs(Percent));
                        }));
                    }

                    // setup next read
                    task.streamResponse.BeginRead(task.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), task);
                    return;
                }
                else
                {
                    // this is done on finisch
                    //task.streamWriter.Close();
                    //task.streamResponse.Close();
                    Success = 1;
                }
            }
            catch (Exception e)
            {
                ErrCode = -3;
                Error   = e;
                Console.WriteLine("\nReadCallBack Exception raised!");
                Console.WriteLine("\nMessage:{0}", e.Message);
            }
            task.mDispatcher.Invoke(new Action(() =>
            {
                task.Finish(Success, ErrCode, Error);
            }));
        }
コード例 #2
0
ファイル: HttpTask.cs プロジェクト: safino9/priv10
        private static void RespCallback(IAsyncResult asynchronousResult)
        {
            int       Success = 0;
            int       ErrCode = 0;
            Exception Error   = null;
            HttpTask  task    = (HttpTask)asynchronousResult.AsyncState;

            try
            {
                // State of request is asynchronous.
                task.response = (HttpWebResponse)task.request.EndGetResponse(asynchronousResult);

                ErrCode = (int)task.response.StatusCode;

                Console.WriteLine("The server at {0} returned {1}", task.response.ResponseUri, task.response.StatusCode);

                string fileName = Path.GetFileName(task.response.ResponseUri.ToString());
                task.lastTime = DateTime.Now;

                Console.WriteLine("With headers:");
                foreach (string key in task.response.Headers.AllKeys)
                {
                    Console.WriteLine("\t{0}:{1}", key, task.response.Headers[key]);

                    if (key.Equals("Content-Length", StringComparison.CurrentCultureIgnoreCase))
                    {
                        task.mLength = int.Parse(task.response.Headers[key]);
                    }
                    else if (key.Equals("Content-Disposition", StringComparison.CurrentCultureIgnoreCase))
                    {
                        string cd = task.response.Headers[key];
                        fileName = cd.Substring(cd.IndexOf("filename=") + 9).Replace("\"", "");
                    }
                    else if (key.Equals("Last-Modified", StringComparison.CurrentCultureIgnoreCase))
                    {
                        task.lastTime = DateTime.Parse(task.response.Headers[key]);
                    }
                }

                //Console.WriteLine(task.lastTime);

                if (task.mDlName == null)
                {
                    task.mDlName = fileName;
                }

                FileInfo testInfo = new FileInfo(task.mDlPath + @"\" + task.mDlName);
                if (testInfo.Exists && testInfo.LastWriteTime == task.lastTime && testInfo.Length == task.mLength)
                {
                    task.request.Abort();
                    Success = 2;
                }
                else
                {
                    // prepare download filename
                    if (!Directory.Exists(task.mDlPath))
                    {
                        Directory.CreateDirectory(task.mDlPath);
                    }
                    if (task.mDlName.Length == 0 || task.mDlName[0] == '?')
                    {
                        task.mDlName = GetNextTempFile(task.mDlPath, "Download");
                    }

                    FileInfo info = new FileInfo(task.mDlPath + @"\" + task.mDlName + ".tmp");
                    if (info.Exists)
                    {
                        info.Delete();
                    }

                    // Read the response into a Stream object.
                    task.streamResponse = task.response.GetResponseStream();

                    task.streamWriter = info.OpenWrite();

                    // Begin the Reading of the contents of the HTML page and print it to the console.
                    task.streamResponse.BeginRead(task.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), task);
                    return;
                }
            }
            catch (WebException e)
            {
                if (e.Response != null)
                {
                    string fileName = Path.GetFileName(e.Response.ResponseUri.AbsolutePath.ToString());

                    if (task.mDlName == null)
                    {
                        task.mDlName = fileName;
                    }

                    FileInfo testInfo = new FileInfo(task.mDlPath + @"\" + task.mDlName);
                    if (testInfo.Exists)
                    {
                        Success = 2;
                    }
                }

                if (Success == 0)
                {
                    ErrCode = -2;
                    Error   = e;
                    Console.WriteLine("\nRespCallback Exception raised!");
                    Console.WriteLine("\nMessage:{0}", e.Message);
                    Console.WriteLine("\nStatus:{0}", e.Status);
                }
            }
            catch (Exception e)
            {
                ErrCode = -2;
                Error   = e;
                Console.WriteLine("\nRespCallback Exception raised!");
                Console.WriteLine("\nMessage:{0}", e.Message);
            }
            task.mDispatcher.Invoke(new Action(() =>
            {
                task.Finish(Success, ErrCode, Error);
            }));
        }