Esempio n. 1
0
        public void TestLogin()
        {
            HttpHelper hp = new HttpHelper();

            HttpItem httpItem = new HttpItem
            {
                Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36",
                URL = "http://m.weibo.cn",//URL     必需项    
                Method = "GET",//URL     可选项 默认为Get   
                Header = new WebHeaderCollection(),
                CookieCollection = new CookieCollection(),
                KeepAlive = true,
                AutoRedirectCookie = true,
                Allowautoredirect = true//是否根据301跳转     可选项   
            };
            httpItem.Header.Add("Accept-Encoding", "gzip, deflate, sdch");
            httpItem.Header.Add("Accept-Language", "zh-CN,zh;q=0.8");

            var res = hp.GetHtml(httpItem);

            //编码用户名 @替换为%40 进行Base64编码
            string su = "*****@*****.**".Replace("@", "%40");
            Byte[] bufin = Encoding.UTF8.GetBytes(su);
            su = Convert.ToBase64String(bufin, 0, bufin.Length);

            var callbackStr = string.Format("jsonpcallback{0}", CommonExtension.GetTime());
            var perLoginUrl = string.Format("https://login.sina.com.cn/sso/prelogin.php?checkpin=1&entry=mweibo&su={0}&callback={1}", su, callbackStr);
            httpItem.URL = perLoginUrl;

            var perLoginHtml = hp.GetHtml(httpItem);

            httpItem.Cookie += perLoginHtml.Cookie;


            string postData = string.Format(
    "username={0}&password={1}&savestate=1{2}&ec=0&pagerefer=https%3A%2F%2Fpassport.weibo.cn%2Fsignin%2Fwelcome%3Fentry%3Dmweibo%26r%3Dhttp%253A%252F%252Fm.weibo.cn%252F%26&entry=mweibo&loginfrom=&client_id=&code=&hff=&hfp=",
    "*****@*****.**".Replace("@", "%40"), "zhoulin", "");

            httpItem.Referer = "https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F";
            httpItem.URL = "https://passport.weibo.cn/sso/login";
            httpItem.Postdata = postData;
            httpItem.Method = "post";
            httpItem.ContentType = "application/x-www-form-urlencoded";
            var postHtml = hp.GetHtml(httpItem);

            dynamic postResult = DynamicJson.Parse(postHtml.Html);
            string retcode = postResult.retcode;
            string Uid = postResult.data.uid;

            if (postResult.data.IsDefined("loginresulturl") && !string.IsNullOrEmpty(postResult.data["loginresulturl"]))
            {
                string loginresulturl = postResult.data["loginresulturl"] + "&savestate=1&url=http%3A%2F%2Fm.weibo.cn%2F";
                httpItem.Referer = "https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F";
                httpItem.URL = loginresulturl;
                httpItem.Method = "Get";
                httpItem.ContentType = "";

                var temp0 = hp.GetHtml(httpItem);

            }
            else
            {
                string weibo_com = string.Format("https:{0}&savestate=1&callback=jsonpcallback{1}", postResult.data.crossdomainlist["weibo.com"], GetTime());
                httpItem.Referer = "https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F";
                httpItem.URL = weibo_com;
                httpItem.Method = "Get";
                httpItem.ContentType = "";
                var temp1 = hp.GetHtml(httpItem);

                string sina_com_cn = string.Format("https:{0}&savestate=1&callback=jsonpcallback{1}", postResult.data.crossdomainlist["sina.com.cn"], GetTime());
                httpItem.URL = weibo_com;
                httpItem.Method = "Get";
                httpItem.ContentType = "";
                var temp2 = hp.GetHtml(httpItem);

                string weibo_cn = string.Format("https:{0}&savestate=1&callback=jsonpcallback{1}", postResult.data.crossdomainlist["weibo.cn"], GetTime());
                httpItem.URL = weibo_cn;
                httpItem.Method = "Get";
                httpItem.ContentType = "";
                var temp3 = hp.GetHtml(httpItem);
            }
        }