日期:2014-05-17  浏览次数:20570 次

关于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);