| 
 using System; 
using System.IO; 
using System.Net; 
using System.Text; 
namespace Van.Base 
{ 
    public class HttpHelper 
    { 
        #region 委托 事件 
        public delegate void dgtProgValueChanged(long Value); 
        ///  
        /// 进度改变事件 
        ///  
        public event dgtProgValueChanged OnProgValueChanged; 
        #endregion 
        #region 属性 
        ///  
        /// 代理 
        ///  
        public WebProxy Proxy { get; set; } 
        ///  
        /// Cookie 
        ///  
        public CookieContainer UserCookie { get; set; } 
        ///  
        /// 重试次数 
        ///  
        public int IAfreshTime { get; set; } 
        ///  
        /// 错误次数 
        ///  
        public int IErrorTime { get; private set; } 
        long m_ProgValue = 0; 
        ///  
        /// 当前读取字节 
        ///  
        public long ProgValue 
        { 
            get { return m_ProgValue; } 
            private set 
            { 
                m_ProgValue = value; 
                if (OnProgValueChanged != null) 
                { 
                    OnProgValueChanged(value); 
                } 
            } 
        } 
        ///  
        /// 待读取最大字节 
        ///  
        public long ProgMaximum { get; private set; } 
        #endregion 
        #region 方法 
        #region Get 
        ///  
        /// 获取HTML 
        ///  
        /// 地址 
        /// Accept请求头 
        /// Html代码 
        public string GetHTML(string URL, string Accept) 
        { 
            return GetHTML(URL, Accept, System.Text.Encoding.UTF8); 
        } 
        ///  
        /// 获取HTML 
        ///  
        /// 地址 
        /// Accept请求头 
        /// 字符编码 
        /// Html代码 
        public string GetHTML(string URL, string Accept, Encoding encoding) 
        { 
            return GetHTML(URL, Accept, encoding, 1024); 
        } 
        ///  
        /// 获取HTML 
        ///  
        /// 地址 
        /// Accept请求头 
        /// 字符编码 
        /// 数据包大小 
        /// Html代码 
        public string GetHTML(string URL, string Accept, Encoding encoding, int bufflen) 
        { 
            IErrorTime = 0; 
            return _GetHTML(URL, Accept, encoding, bufflen); 
        } 
        ///  
        /// 获取HTML 
        ///  
        /// 地址 
        /// Accept请求头 
        /// 字符编码 
        /// 数据包大小 
        /// Html代码 
        private string _GetHTML(string URL, string Accept, Encoding encoding,int bufflen) 
        { 
            try 
            { 
                HttpWebRequest MyRequest = (HttpWebRequest)HttpWebRequest.Create(URL); 
                MyRequest.Proxy = Proxy; 
                MyRequest.Accept = Accept; 
                if (UserCookie == null) 
                { 
                    UserCookie = new CookieContainer(); 
                } 
                MyRequest.CookieContainer = UserCookie; 
                HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse(); 
                return _GetHTML(MyResponse, encoding, bufflen); 
            } 
            catch (Exception erro) 
            { 
                if (erro.Message.Contains("连接") && IAfreshTime - IErrorTime > 0) 
                { 
                    IErrorTime++; 
                    return _GetHTML(URL, Accept, encoding, bufflen); 
                } 
                throw; 
            } 
        } 
        ///  
        /// 获取HTML 
        ///  
        ///  
        /// 字符编码 
        /// 数据包大小 
        ///  
        private string _GetHTML(HttpWebResponse MyResponse, Encoding encoding, int bufflen) 
        { 
            using (Stream MyStream = MyResponse.GetResponseStream()) 
            { 
                using (StreamReader reader = new StreamReader(MyStream, encoding)) 
                { 
                    ProgMaximum = MyResponse.ContentLength; 
                    string result = null; 
                    long totalDownloadedByte = 0; 
                    byte[] by = new byte[bufflen]; 
                    int osize = MyStream.Read(by, 0, by.Length); 
                    while (osize > 0) 
                    { 
                        totalDownloadedByte = osize + totalDownloadedByte; 
                        result += encoding.GetString(by, 0, osize); 
                        ProgValue = totalDownloadedByte; 
                        osize = MyStream.Read(by, 0, by.Length); 
                    } 
                    reader.Close(); 
                    return result; 
                } 
            } 
        } 
        #endregion 
 
        #region GetImg 
        public System.Drawing.Bitmap Getimg(string URL, string Accept) 
        { 
            return _GetBit(URL, Accept); 
        } 
        ///  
        /// 获取HTML 
        ///  
        /// 地址 
        /// Accept请求头 
        /// Html代码 
        private System.Drawing.Bitmap _GetBit(string URL, string Accept) 
        { 
            HttpWebRequest MyRequest = (HttpWebRequest)HttpWebRequest.Create(URL); 
            MyRequest.Proxy = Proxy; 
            MyRequest.Accept = Accept; 
            if (UserCookie == null) 
            { 
                UserCookie = new CookieContainer(); 
            } 
            MyRequest.CookieContainer = UserCookie; 
            HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse(); 
            return _GetBit(MyResponse); 
        } 
        ///  
        /// 获取图像 
        ///  
        ///  
        ///  
        private System.Drawing.Bitmap _GetBit(HttpWebResponse MyResponse) 
        { 
            using (Stream MyStream = MyResponse.GetResponseStream()) 
            { 
                return new System.Drawing.Bitmap(MyStream); 
            } 
        } 
        #endregion 
        #region Post 
        ///  
        /// 回发(字符编码默认UTF-8) 
        ///  
        /// 回发地址 
        /// 参数 
        /// Html代码 
        public string PostPage(string URL, string PostData) 
        { 
            return PostPage(URL, PostData, System.Text.Encoding.UTF8); 
        } 
        ///  
        /// 回发 
        ///  
        /// 回发地址 
        /// 参数 
        /// 字符编码 
        /// Html代码 
        public string PostPage(string URL, string PostData, Encoding encoding) 
        { 
            return PostPage(URL, PostData, encoding, null); 
        } 
        ///  
        /// 回发 
        ///  
        /// 回发地址 
        /// 参数 
        /// 字符编码 
        /// Html代码 
        public string PostPage(string URL, string PostData, Encoding encoding, string ContentType) 
        { 
            IErrorTime = 0; 
            return _PostPage(URL, PostData, encoding, ContentType); 
        } 
        ///  
        /// 回发 
        ///  
        /// 回发地址 
        /// 参数 
        /// 字符编码 
        /// Html代码 
        private string _PostPage(string URL, string PostData, Encoding encoding,string ContentType) 
        { 
            try 
            { 
                if (ContentType==null) 
                { 
                    ContentType = "application/x-www-form-urlencoded"; 
                } 
                HttpWebRequest MyRequest = (HttpWebRequest)HttpWebRequest.Create(URL); 
                MyRequest.Proxy = Proxy; 
                if (UserCookie == null) 
                { 
                    UserCookie = new CookieContainer(); 
                } 
                MyRequest.CookieContainer = UserCookie; 
                MyRequest.Method = "POST"; 
                MyRequest.ContentType = ContentType; 
                byte[] b = encoding.GetBytes(PostData); 
                MyRequest.ContentLength = b.Length; 
                using (System.IO.Stream sw = MyRequest.GetRequestStream()) 
                { 
                    try 
                    { 
                        sw.Write(b, 0, b.Length); 
                    } 
                    catch 
                    { 
                    } 
                } 
                HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse(); 
                return _GetHTML(MyResponse, encoding, 1024); 
            } 
            catch (Exception erro) 
            { 
                if (erro.Message.Contains("连接") && IAfreshTime - IErrorTime > 0) 
                { 
                    IErrorTime++; 
                    return _PostPage(URL, PostData, encoding, ContentType); 
                } 
                throw; 
            } 
        } 
        #endregion 
        #endregion 
    } 
} 
  
 |