requeststate.cs

来自「JAVA的精彩实例」· CS 代码 · 共 110 行

CS
110
字号
using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using feiyun0112.cnblogs.com.CSDNReader.Functions;
using System.Collections.Generic;


namespace feiyun0112.cnblogs.com.CSDNReader.Model
{
    public class RequestState:IDisposable 
    {
        
        public const int BUFFER_SIZE = 0x1000;
        public byte[] BufferRead = new byte[0x1000];
        public string ContentEncoding;
        public HttpWebRequest Request ;
        public MemoryStream RequestData = new MemoryStream();
        public Stream ResponseStream;
        public StartDownloadCallback StartDownload;
        public RequestCompleteCallback WebRequestComplete;
        public ExceptionCallback WebRequestException;
        public RequestStatePriority Priority;


        public void FireRequestComplete()
        {
            if ((this.WebRequestComplete != null) && (this.RequestData != null))
            {
                byte[] dataBuffer = this.RequestData.GetBuffer();
                     
                try
                {
                    this.WebRequestComplete(dataBuffer);
                }
                catch (Exception e)
                {
                    FireRequestException(e);
                }
            }


        }

        public void FireRequestException(Exception e)
        {
            if (this.ResponseStream != null)
            {
                this.ResponseStream.Close();
            }
            if (this.WebRequestException != null)
            {
                this.WebRequestException(e);
            }
        }

        public bool  FireStartDownload()
        {
            if (this.StartDownload != null)
            {
                return this.StartDownload();
            }
            return true;
        }



        public int CompareTo(RequestState x)
        {
            return this.Priority - x.Priority;
        }

        #region IDisposable 成员

        public void Dispose()
        {
            BufferRead = null;
            if (this.Request != null)
            {
                this.Request.Abort();
                this.Request = null;
            }
            if (this.RequestData != null)
            {
                this.RequestData.Close();
                this.RequestData = null;
            }            
            if (this.ResponseStream != null)
            {
                this.ResponseStream.Close();
                this.ResponseStream = null;
            }
            
        }

        #endregion
    }

    public class RequestStateComparer : IComparer<RequestState>
    {
        public int Compare(RequestState x, RequestState y)
        {
              return y.CompareTo(x);
            
        }
    }


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?