写个程序登陆58同城

代码 http://pan.baidu.com/s/1i3kqYjV

主要代码,还是比较复杂的,58验证比较难破

        /// <summary>

        /// 获取城市编码

        /// </summary>

        /// <param name="unumber">石家庄:sjz,北京:bj</param>

        /// <returns></returns>

        public string GetCityCode()

        {

            string citycode = "";

            //从网站上取

            //site = CommunBase.Core.GetCityCodeByWeb(siteid, cityname);

            string siteURL = "http://www.58.com/changecity.aspx";

            citycode = "";//默认是无

            string text = GetPageData_UTF8(siteURL);//html文本

            Regex reg = new Regex("<a href=\"http://(.{1,20}).58.com/\" onclick=\"co\\('(.{1,20})'\\)\">" + cityName + "</a>", RegexOptions.Singleline | RegexOptions.IgnoreCase);

            Match mc = reg.Match(text);

            if (mc.Success)

                citycode = mc.Groups[1].Value.Trim();

            return citycode;

        }



        /// <summary>

        /// 获得城市ID 

        /// </summary>

        /// <param name="cityname"></param>

        /// <returns></returns>

        public string GetCityID()

        {

            string html = GetPageData("http://post.58.com/postindex.htm", "utf-8");

            return GetDizhi_MultiLine(html, "58.com/(\\d*?)/\" onclick=\"\\S*?>" + cityName, 1);

        }

        /// <summary>

        /// 检测是否登陆成功

        /// </summary>

        /// <returns></returns>

        private bool IsLogin()

        {

            string ss = GetPageData("http://my.58.com/", "", "utf-8");

            if (ss.IndexOf("<title>用户中心</title>") > 0)

            {

                return true;

            }

            return false;

        }



        /// <summary>

        /// 登陆主方法

        /// </summary>

        /// <returns></returns>

        public bool DoLogin()

        {

            try

            {

                if (IsLogin()) return true;

                //Caiji caiji = new Caiji();

                city = GetCityCode();

                cityID = GetCityID();

                string user = siteAccount; //用户名

                string pass = sitePassword; //密码

                string html = GetPageData("http://" + Core.city + ".58.com/", "http://www.58.com/changecity.aspx", "utf-8");

                html = GetPageData("http://passport.58.com/login", "http://" + Core.city + ".58.com/", "utf-8");

                string str2 = GetDizhi_MultiLine(html, "timespan[\\s\\S]*?(\\d{13})", 1);

                long timespan = 0;

                try

                {

                    timespan = Convert.ToInt64(str2);

                }

                catch { timespan = 1411093327735; }

                string str3 = md5(pass);

                string str4 = md5(str3 + str2.Substring(5, 6));//P1

                string str5 = str3.Substring(8, 0x10);

                string str6 = string.Empty;

                for (int i = str5.Length - 1; i > -1; i--)

                {

                    str6 = str6 + str5[i];

                }

                string str7 = md5(str6 + str2.Substring(5, 6));//P2

                //这个也需要取

                string subResult = GetDizhi_MultiLine(html, "\"010001\",\\s*\"\\s*([\\w]*?)\"\\)\\);", 1);

                string str9 = "encryptString('" + str2 + HttpUtility.UrlEncode(pass) + "', '010001', '" + subResult + "')";

                string str10 = RsaUtils.EncryptString(str2 + HttpUtility.UrlEncode(pass), "010001", subResult, 130);

                string ptk = GetDizhi_MultiLine(html, "id=\"ptk\" value=\"(\\w*?)\"", 1);

                string cd = GetDizhi_MultiLine(html, "id=\"cd\" value=\"(\\d{4})\"", 1);

                //string isweak = "0";

                data =

                    "isweak=0&username=" + user +

                    "&password=password" +

                    "&p1=" + str4 + //正确

                    "&p2=" + str7 + //正确

                    "&p3=" + str10 + //正确

                    "&timesign=" + timespan +

                    "&ptk=" + ptk + //正确

                    "&cd=" + cd + //正确

                    "&mcresult=undefined";

                Cookie ck = new Cookie();

                ck.Name = "58home";

                ck.Value = city;

                ck.Domain = ".58.com";

                ck.Path = "/";

                cc.Add(ck);

                ck = new Cookie();

                ck.Name = "city";

                ck.Value = city;

                ck.Domain = ".58.com";

                ck.Path = "/";

                cc.Add(ck);

                Encoding Ed = Encoding.GetEncoding("utf-8");

                string url = "http://passport.58.com/dounionlogin";

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                request.Method = "POST";

                request.Referer = "http://passport.58.com/login";

                request.ContentType = "application/x-www-form-urlencoded";

                request.Accept =

                    "text/html, application/xhtml+xml, */*";

                request.UserAgent =

                    "Mozilla/5.0 (Windows NT 6.1; WOW64;Trident/7.0; rv:11.0) like Gecko";

                request.Headers.Add("Cache-Control", "no-cache");

                request.AllowAutoRedirect = false;//是否重定向

                request.CookieContainer = cc;

                request.KeepAlive = true;

                request.ServicePoint.Expect100Continue = false;

                byte[] bbb = Ed.GetBytes(data);

                Stream requestStream = request.GetRequestStream();

                requestStream.Write(bbb, 0, bbb.Length);

                requestStream.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                string encoding = response.ContentEncoding;

                if (encoding == null || encoding.Length < 1)

                {

                    encoding = "UTF-8"; //默认编码

                }

                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));

                data = reader.ReadToEnd();





                if (data.IndexOf("my.58.com") == -1)

                {

                    return false;

                }

                else

                {

                    string strCookie = response.Headers["Set-Cookie"];

                    CookieCollection cl = GetCookiesFromReponseHead(strCookie, ".58.com");

                    foreach (Cookie oneck in cl)

                    {

                        Cookie newhk = new Cookie();

                        newhk.Domain = ".58.com";

                        newhk.Name = oneck.Name;

                        newhk.Value = oneck.Value;

                        Cookie ck2 = FindCookie("post.58.com", oneck.Name);

                        if (ck2 == null)

                        {

                            cc.Add(newhk);

                        }

                        else

                        {

                            ck2 = newhk;

                        }

                    }

                    response.Close();

                    return IsLogin();

                }

            }

            catch (Exception ex) { return false; }

        }

 

你可能感兴趣的:(程序)