.net cookie跨域和逗号bug的修复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace BlowFish
{
    public class Cookie
    {
        #region 属性
        public string Name { get; set; }
        public string Value { get; set; }
        public string Domain { get; set; }
        #endregion

        #region 方法


        #endregion
    }
    public class CookieContainer
    {
        private Hashtable _htcookie = new Hashtable();
        public void AddCookie(Cookie c)
        {
            if (c.Value.IndexOf(";") < 0 && c.Name.IndexOf(";") < 0)
            {
                _htcookie.Remove(c.Name);
                _htcookie.Add(c.Name, c);
            }
            else
            {
                throw new Exception("cookiez值或名称不合规范");
            }
        }
        public Cookie GetCookie(string Name)
        {
            if (_htcookie.Contains(Name))
            {
                return _htcookie[Name] as Cookie;
            }
            else
            {
                return null;
            }
        }
        public void RemoveCookie(string Name)
        {
            _htcookie.Remove(Name);
        }
        public List<Cookie> GetCookies(string url)
        {
            List<Cookie> l = new List<Cookie>();

            string s = new Regex("https*://[^/]+").Match(url).Value.Replace("http://", "").Replace("https://", "").Replace("www.", "").Replace("/", "");
            if (s != "")
            {
                foreach (DictionaryEntry de in _htcookie)
                {
                    string demain = (de.Value as Cookie).Domain;

                    if (s.IndexOf(demain) > -1)
                    {
                        l.Add(de.Value as Cookie);
                    }
                }
            }
            return l;
        }

    }
    public class HTTPRequest
    {

        public HTTPRequest(string Url)
        {
            _url = Url;
        }
        #region 字段
        private string _url = string.Empty;
        private int _timeout = 10000;
        private string _contenttype = "application/x-www-form-urlencoded";
        private string _useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
        private string _accept = "application/x-javascript";
        private bool _allowwritestreambuffering = false;
        private bool _expect100continue = false;
        #endregion

        #region 属性
        public string RequestUrl
        {
            get
            {

                return _url;
            }
            set
            {
                _url = value;
            }
        }
        public int Timeout
        {
            get
            {
                return _timeout;
            }
            set
            {
                _timeout = value;
            }
        }
        public string Refer { get; set; }
        public string ContentType
        {
            get
            {
                return _contenttype;
            }
            set
            {
                _contenttype = value;
            }
        }
        public string UserAgent
        {
            get
            {
                return _useragent;
            }
            set
            {
                _useragent = value;
            }
        }
        public string Accept
        {
            get
            {
                return _accept;
            }
            set
            {
                _accept = value;
            }
        }
        public bool AllowWriteStreamBuffering
        {
            get
            {
                return _allowwritestreambuffering;
            }
            set
            {
                _allowwritestreambuffering = value;
            }
        }
        public bool Expect100Continue
        {
            get
            {
                return _expect100continue;
            }
            set
            {
                _expect100continue = value;
            }
        }
        #endregion

        #region 公共方法
        public byte[] GetResponse(byte[] postData, ref CookieContainer cookies)
        {
            byte[] b = null;
            string myUrl = "-1";
            while (true)
            {
                if (myUrl == null)
                {
                    break;
                }
                else
                {
                    if (myUrl == "-1")
                    {
                        b = _GetResponse(_url, Refer, "POST", postData, ref cookies, ref myUrl);
                    }
                    else
                    {
                        b = _GetResponse(myUrl, Refer, "GET", null, ref cookies, ref myUrl);

                        if (myUrl != null)
                        {

                            _url = myUrl;
                        }
                    }
                }
                if (b == null)
                {
                    break;
                }
            }
            return b;
        }
        public byte[] GetResponse(ref CookieContainer cookies)
        {

            byte[] b = null;
            string myUrl = "-1";
            while (true)
            {
                if (myUrl == null)
                {
                    break;
                }
                else
                {
                    if (myUrl == "-1")
                    {
                        b = _GetResponse(_url, Refer, "GET", null, ref cookies, ref myUrl);
                    }
                    else
                    {

                        b = _GetResponse(myUrl, Refer, "GET", null, ref cookies, ref myUrl);
                        if (myUrl != null)
                        {

                            _url = myUrl;
                        }
                    }
                }
                if (b == null)
                {
                    break;
                }
            }
            return b;
        }
        #endregion

        #region 私有方法
        private byte[] _GetResponse(string _u, string refer, string _m, byte[] postData, ref CookieContainer _cookies, ref string Location)
        {
            if (_u.IndexOf("https") > -1)
            {
                try
                {
                    string strHtmlContent = string.Empty;
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckVolidationResult);
                    HttpWebRequest webRequest = WebRequest.Create(_u) as HttpWebRequest;
                    webRequest.ProtocolVersion = HttpVersion.Version11;
                    webRequest.Timeout = Timeout;
                    webRequest.ContentType = _contenttype;
                    webRequest.Referer = refer;
                    webRequest.UserAgent = _useragent;
                    webRequest.AllowAutoRedirect = false;
                    webRequest.Method = _m;
                    webRequest.Accept = _accept;
                    webRequest.AllowWriteStreamBuffering = _allowwritestreambuffering;
                    webRequest.ServicePoint.Expect100Continue = _expect100continue;
                    //添加cookie
                    string strcookie = string.Empty;
                    if (_cookies != null)
                    {
                        foreach (Cookie c in _cookies.GetCookies(_u))
                        {
                            strcookie += c.Name + "=" + c.Value + ";";
                        }
                        webRequest.Headers.Set("cookie", strcookie);
                    }
                    if (_m.ToLower() == "post")
                    {
                        webRequest.ContentLength = postData.Length;
                        Stream requestStream = webRequest.GetRequestStream();
                        requestStream.Write(postData, 0, postData.Length);
                        requestStream.Close();
                    }
                    HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();//获得服务器响应对象 

                    //处理返回cookie
                    if (_cookies != null)
                    {
                        if (response != null)
                        {
                            string s = response.Headers["Set-Cookie"];
                            if (!string.IsNullOrEmpty(s))
                            {
                                while (true)
                                {
                                    string mytemp = new Regex("[^,;]+=").Match(s).Value;
                                    if (!string.IsNullOrEmpty(mytemp))
                                    {
                                        string myIs = new Regex("([Ee][Xx][Pp][Ii][Rr][Ee][Ss])|([Dd][Oo][Mm][Aa][Ii][Nn])|([Pp][Aa][Tt][Hh])").Match(mytemp).Value;
                                        if (string.IsNullOrEmpty(myIs))
                                        {
                                            s = s.Substring(s.IndexOf(mytemp));
                                            string mysp = s.Split(';')[0];
                                            string myd = new Regex("[Dd][Oo][Mm][Aa][Ii][Nn]=[a-zA-Z0-9_%$#@!.*-]+").Match(s).Value;

                                            Cookie c = new Cookie();
                                            c.Name = mysp.Split('=')[0];
                                            c.Value = mysp.Split('=')[1];
                                            c.Domain = myd.Substring(myd.IndexOf('=') + 1); ;
                                            _cookies.AddCookie(c);
                                            s = s.Substring(s.IndexOf(mysp) + mysp.Length);
                                        }
                                        else
                                        {

                                            s = s.Substring(s.IndexOf(mytemp) + mytemp.Length);
                                        }
                                    }
                                    else
                                    {

                                        break;
                                    }
                                }
                            }

                        }
                    }

                    Location = response.Headers["Location"];
                    Stream resStream = response.GetResponseStream();//转成流对象  
                    MemoryStream memoryStream = new MemoryStream();
                    const int bufferLength = 1024;
                    int actual; byte[]
                        buffer = new byte[bufferLength];
                    while ((actual = resStream.Read(buffer, 0, bufferLength)) > 0)
                    {
                        memoryStream.Write(buffer, 0, actual);
                    }

                    // 设置当前流的位置为流的开始 
                    //resStream.Seek(0, SeekOrigin.Begin);
                    //MemoryStream ms=new MemoryStream(
                    response.Close();
                    webRequest.Abort();
                    return memoryStream.ToArray();
                }
                catch (Exception ex)
                {
                    //throw ex; 
                    string slog = "执行请求操作失败,错误信息:" + ex.Message;
                    if (slog.IndexOf("404", 0) < -1)
                        iFIASSoft.Manager.WriteLog(slog);
                    return null;
                }
            }
            else
            {
                try
                {
                    string strHtmlContent = string.Empty;
                    HttpWebRequest webRequest = WebRequest.Create(_u) as HttpWebRequest;
                    webRequest.Timeout = Timeout;
                    webRequest.ContentType = _contenttype;
                    webRequest.Referer = refer;
                    webRequest.UserAgent = _useragent;
                    webRequest.AllowAutoRedirect = false;
                    webRequest.Method = _m;
                    webRequest.Accept = _accept;
                    webRequest.AllowWriteStreamBuffering = _allowwritestreambuffering;
                    webRequest.ServicePoint.Expect100Continue = _expect100continue;
                    //添加cookie
                    string strcookie = string.Empty;
                    if (_cookies != null)
                    {
                        foreach (Cookie c in _cookies.GetCookies(_u))
                        {
                            strcookie += c.Name + "=" + c.Value + ";";
                        }
                        webRequest.Headers.Set("cookie", strcookie);
                    }
                    if (_m.ToLower() == "post")
                    {
                        webRequest.ContentLength = postData.Length;
                        Stream requestStream = webRequest.GetRequestStream();
                        requestStream.Write(postData, 0, postData.Length);
                        requestStream.Close();
                    }
                    HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();//获得服务器响应对象 

                    //处理返回cookie
                    if (_cookies != null)
                    {
                        if (response != null)
                        {
                            string s = response.Headers["Set-Cookie"];
                            if (!string.IsNullOrEmpty(s))
                            {
                                while (true)
                                {
                                    string mytemp = new Regex("[^,;]+=").Match(s).Value;
                                    if (!string.IsNullOrEmpty(mytemp))
                                    {
                                        string myIs = new Regex("([Ee][Xx][Pp][Ii][Rr][Ee][Ss])|([Dd][Oo][Mm][Aa][Ii][Nn])|([Pp][Aa][Tt][Hh])").Match(mytemp).Value;
                                        if (string.IsNullOrEmpty(myIs))
                                        {
                                            s = s.Substring(s.IndexOf(mytemp));
                                            string mysp = s.Split(';')[0];
                                            string myd = new Regex("[Dd][Oo][Mm][Aa][Ii][Nn]=[a-zA-Z0-9_%$#@!.*-]+").Match(s).Value;

                                            Cookie c = new Cookie();
                                            c.Name = mysp.Split('=')[0];
                                            c.Value = mysp.Split('=')[1];
                                            c.Domain = myd.Substring(myd.IndexOf('=') + 1); ;
                                            _cookies.AddCookie(c);
                                            s = s.Substring(s.IndexOf(mysp) + mysp.Length);
                                        }
                                        else
                                        {

                                            s = s.Substring(s.IndexOf(mytemp) + mytemp.Length);
                                        }
                                    }
                                    else
                                    {

                                        break;
                                    }
                                }
                            }

                        }
                    }
                    Location = response.Headers["Location"];
                    Stream resStream = response.GetResponseStream();//转成流对象  
                    //StreamReader sr = new StreamReader(resStream, Encoding.UTF8);
                    //strHtmlContent = sr.ReadToEnd();
                    MemoryStream memoryStream = new MemoryStream();
                    const int bufferLength = 1024;
                    int actual; byte[]
                        buffer = new byte[bufferLength];
                    while ((actual = resStream.Read(buffer, 0, bufferLength)) > 0)
                    {
                        memoryStream.Write(buffer, 0, actual);
                    }

                    // 设置当前流的位置为流的开始 
                    //resStream.Seek(0, SeekOrigin.Begin);
                    //MemoryStream ms=new MemoryStream(
                    response.Close();
                    webRequest.Abort();
                    return memoryStream.ToArray();
                }
                catch (Exception ex)
                {
                    // throw ex;
                    string slog = "执行请求操作失败,错误信息:" + ex.Message;
                    if (slog.IndexOf("404", 0) < -1)
                        iFIASSoft.Manager.WriteLog(slog);
                    return null;
                }
            }
        }
        private bool CheckVolidationResult(object o, X509Certificate cetificate, X509Chain chain, SslPolicyErrors err)
        {
            return true;
        }
        #endregion
    }
}

 

////////////////////////上面为子封装的类 下面用法事例

public BlowFish.CookieContainer cookies = new BlowFish.CookieContainer();

tring strHtmlContent = "";

BlowFish.HTTPRequest wep = new BlowFish.HTTPRequest("网址");wep.Refer = "网址";
byte[] b = wep.GetResponse(ref cookies);
strHtmlContent = System.Text.Encoding.UTF8.GetString(b)

 

posted @ 2013-03-25 09:18  吹鱼算法  阅读(1465)  评论(9编辑  收藏  举报