当前位置: 代码迷 >> ASP.NET >> 关于c# post登陆的有关问题,郁闷
  详细解决方案

关于c# post登陆的有关问题,郁闷

热度:908   发布时间:2013-02-25 00:00:00.0
关于c# post登陆的问题,郁闷啊
先贴出 我的代码,可以得到ASP.NET_SessionId的值,可是为什么 就是登陆不陈宫呢?
代码也是从网上找的,大家看看。
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;using System.Net;using System.IO;namespace PostData{    public class Post    {        public string CookieHeader { get; set; }        public string EncodeName { get; set; }        public string PageContent { get; set; }        public Dictionary<string, string> Params { get; set; }        public CookieContainer Container { get; set; }            public string PostData(string strUrl, string strArgs, string strRefer, string encodename, string method)        {            return this.PostData(strUrl, strArgs, strRefer, encodename, method, string.Empty);        }        public string PostData(string strUrl, Dictionary<string, string> dics, string strRefer, string encodename, string method)        {            string strArgs = string.Empty;            StringBuilder objEncodedPostDatas = new StringBuilder();            if (dics != null && dics.Count > 0)            {                foreach (KeyValuePair<string, string> kv in dics)                {                    objEncodedPostDatas.Append(HttpUtility.UrlEncode(kv.Key));                    objEncodedPostDatas.Append("=");                    objEncodedPostDatas.Append(HttpUtility.UrlEncode(kv.Value));                    objEncodedPostDatas.Append("&");                }            }            strArgs = objEncodedPostDatas.ToString();            return this.PostData(strUrl, strArgs, strRefer, encodename, method, string.Empty);        }        /// <summary>        /// 模拟登陆页面,提交登录数据进行登录,并记录header中的cookie        /// </summary>        /// <param name="strURL">提交的页面地址</param>        /// <param name="strArgs">用户登录数据</param>        /// <param name="strRefer">引用地址</param>        /// <param name="code">网站编码名称</param>        /// <param name="method"></param>        /// <param name="contentType"></param>        /// <returns></returns>        public string PostData(string strURL, string strArgs, string strRefer, string code, string method, string contentType)        {            if (this.Container == null) this.Container = new CookieContainer();            strRefer = strURL;            Uri uri = new Uri("http://" + new Uri(strURL).Host);            try            {                string strRs = string.Empty;                HttpWebRequest myHttpWebRequest = WebRequest.Create(strURL) as HttpWebRequest;                myHttpWebRequest.AllowAutoRedirect = true;                myHttpWebRequest.KeepAlive = true;                myHttpWebRequest.Accept = "image/gif,image/x-xbitmap,image/jpeg,image/pjpeg," +                "application/vnd.ms-excel,application/msword,application/x-shockwave-flash,*/*";                myHttpWebRequest.Referer = strRefer;                myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)";                if (string.IsNullOrEmpty(contentType))                {                    contentType = "application/x-www-form-urlencoded";                }                myHttpWebRequest.ContentType = contentType;                myHttpWebRequest.Method = method;                myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip,deflate");                myHttpWebRequest.CookieContainer = this.Container;// new CookieContainer();                byte[] postData = Encoding.GetEncoding(code).GetBytes(strArgs);                myHttpWebRequest.ContentLength = postData.Length;                Stream poststream = myHttpWebRequest.GetRequestStream();                poststream.Write(postData, 0, postData.Length);                poststream.Close();                HttpWebResponse response = myHttpWebRequest.GetResponse() as HttpWebResponse;                CookieCollection cc = myHttpWebRequest.CookieContainer.GetCookies(uri);                response.Cookies = cc;                foreach (Cookie c in cc)                {                    string s = c.Name + c.Value + c.Domain + c.Path;                }                string sss = response.Headers.Get("Set-Cookie");                string header = response.Headers.ToString();                this.CookieHeader = myHttpWebRequest.CookieContainer.GetCookieHeader(uri);                string ss = response.Headers.Get("Location");                StreamReader sr = null;                sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(code));                strRs = sr.ReadToEnd();                sr.Close();                response.Close();                return strRs;            }            catch (Exception e)            {                throw new Exception(e.Message);            }        }        /// <summary>        /// 在postlogin陈宫登录后记录下header中的cookie,然后获取此网站上其他页面的内容        /// </summary>        /// <param name="strUrl"></param>        /// <param name="strRefer"></param>        /// <param name="codeName"></param>        /// <returns></returns>        public string GetPage(string strUrl, string strRefer, string codeName)        {            return this.GetPage(strUrl, strRefer, codeName, string.Empty);        }        public string GetPage(string strURL, string strRefer, string codename, string contentType)        {            if (this.Container == null) this.Container = new CookieContainer();            strRefer = strURL;            Uri uri = new Uri("http://" + new Uri(strURL).Host);            string strResult = string.Empty;            HttpWebRequest myHttpWebRequest = WebRequest.Create(strURL) as HttpWebRequest;            myHttpWebRequest.AllowAutoRedirect = true;            myHttpWebRequest.KeepAlive = false;            myHttpWebRequest.CookieContainer = this.Container;            myHttpWebRequest.Accept = "image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/vnd.ms-excel,application/msword,application/x-shockwave-flash,*/*";            myHttpWebRequest.Referer = strRefer;            myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)";            if (string.IsNullOrEmpty(contentType))            {                contentType = "application/x-www-form-urlencoded";            }            myHttpWebRequest.ContentType = contentType;            myHttpWebRequest.Method = "GET";            myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip,deflate");            if (!string.IsNullOrEmpty(this.CookieHeader) && this.CookieHeader.Length > 0)            {                //myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader);                //  myHttpWebRequest.CookieContainer.SetCookies(uri, this.CookieHeader);            }            HttpWebResponse response = null;            StreamReader sr = null;            response = myHttpWebRequest.GetResponse() as HttpWebResponse;            response.Cookies = myHttpWebRequest.CookieContainer.GetCookies(uri);            foreach (Cookie c in response.Cookies)            {                string s = c.Name + c.Value + c.Domain + c.Path;            }            string location = response.Headers.Get("Location");            #region read            bool isGzip = !string.IsNullOrEmpty(response.ContentEncoding) && response.ContentEncoding.ToLower() == "gzip";            Stream streamReceive = null;            if (isGzip)            {                streamReceive = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);            }            else            {                streamReceive = response.GetResponseStream();            }            sr = new StreamReader(streamReceive, Encoding.GetEncoding(codename));            if (response.ContentLength > 1)            {                strResult = sr.ReadToEnd();            }            else            {                char[] buffer = new char[256];                int count = 0;                StringBuilder sb = new StringBuilder();                while ((count = sr.Read(buffer, 0, buffer.Length)) > 0)                {                    sb.Append(new string(buffer));                }                strResult = sb.ToString();            }            #endregion            sr.Close();            response.Close();            return strResult;        }    }}
  相关解决方案