Beispiel #1
0
        /// <summary>
        /// 检测手机是否扫码
        /// </summary>
        private void CheckSacnLogin()
        {
            try
            {
                byte[]    userAvatar = null;
                ScanState scanState  = ScanState.UnKnown;
                while (syncPolling && (scanState != ScanState.Login))
                {
                    string timespan = Utils.GetTimeStamp();
                    string loginUrl = string.Format("https://login.wx2.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid={0}&tip=0&r={1}&_={2}", uuid, Utils.Get_r(), Utils.GetTimeStamp());
                    //采用长轮询的方式,25秒返内回一次检测数据。
                    string checkResult = httpClient.GetString(loginUrl);
                    Utils.Debug("CheckSacnLogin " + checkResult);
                    if (checkResult.IndexOf("window.code=408;") != -1)
                    {
                        scanState = ScanState.Timeout;
                    }
                    else if (checkResult.IndexOf("window.code=201;") != -1)
                    {
                        scanState = ScanState.Scan;
                        //有些号没有头像就跳过这个步骤
                        if (checkResult.IndexOf("window.userAvatar") != -1)
                        {
                            //扫码返回的头像是base64格式,需要转化
                            string subStr           = "window.code=201;window.userAvatar = 'data:img/jpg;base64,";
                            string base64UserAvatar = checkResult.Substring(subStr.Length, checkResult.Length - subStr.Length - 2);
                            byte[] arr = Convert.FromBase64String(base64UserAvatar);
                            userAvatar = arr;
                            asyncOperation.Post(
                                new SendOrPostCallback((obj) =>
                            {
                                CheckScanComplete?.Invoke(this, new TEventArgs <byte[]>((byte[])obj));
                            }), userAvatar);
                        }
                    }
                    else if (checkResult.IndexOf("window.code=200;") != -1)
                    {
                        scanState = ScanState.Login;
                        string subStr = "window.code=200;\nwindow.redirect_uri=\"";
                        cookieRedirectUri = checkResult.Substring(subStr.Length, checkResult.Length - subStr.Length - 2);
                        //跳转登录页获取cookie,并且获取关键参数,根据跳转地址,获相应提交地址
                        string cookieRedirectResult = httpClient.LoginString(cookieRedirectUri);
                        if (cookieRedirectUri.StartsWith("https://wx2.qq.com"))
                        {
                            host       = "https://wx2.qq.com";
                            pushHost   = "https://webpush.wx2.qq.com";
                            uploadHost = "https://file.wx2.qq.com";
                        }
                        else if (cookieRedirectUri.StartsWith("https://wx8.qq.com"))
                        {
                            host       = "https://wx8.qq.com";
                            pushHost   = "https://webpush.wx8.qq.com";
                            uploadHost = "https://file.wx8.qq.com";
                        }
                        else if (cookieRedirectUri.StartsWith("https://web2.wechat.com"))
                        {
                            host       = "https://web2.wechat.com";
                            pushHost   = "https://webpush.web2.wechat.com";
                            uploadHost = "https://file.web2.wechat.com";
                        }
                        else if (cookieRedirectUri.StartsWith("https://web.wechat.com"))
                        {
                            host       = "https://web.wechat.com";
                            pushHost   = "https://webpush.web.wechat.com";
                            uploadHost = "https://file.web.wechat.com";
                        }
                        else
                        {
                            host       = "https://wx.qq.com";
                            pushHost   = "https://webpush.wx.qq.com";
                            uploadHost = "https://file.wx.qq.com";
                        }

                        httpClient.Referer = host;
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.LoadXml(cookieRedirectResult);
                        //如果返回异常,则可能被暂封,无法登陆网页版
                        if (xmlDoc["error"]["ret"].InnerText != "0")
                        {
                            throw new Exception(xmlDoc["error"]["message"].InnerText);
                        }
                        else
                        {
                            baseRequest.Sid  = xmlDoc["error"]["wxsid"].InnerText;
                            baseRequest.Uin  = Convert.ToInt64(xmlDoc["error"]["wxuin"].InnerText);
                            baseRequest.Skey = xmlDoc["error"]["skey"].InnerText;
                            passTicket       = xmlDoc["error"]["pass_ticket"].InnerText;
                        }
                    }
                    else if (checkResult.IndexOf("window.code=400;") != -1)
                    {
                        scanState = ScanState.Expires;
                        GetLoginQrCode();
                    }
                    else
                    {
                        scanState = ScanState.UnKnown;
                    }
                    Thread.Sleep(1000);
                }
            }
            catch (Exception ex)
            {
                FileLog.Exception("CheckSacnLogin", ex);
                asyncOperation.Post(
                    new SendOrPostCallback((obj) =>
                {
                    ExceptionCatched?.Invoke(this, new TEventArgs <Exception>((Exception)obj));
                }), ex);
                throw ex;
            }
        }
Beispiel #2
0
        /// <summary>
        /// 模拟获取微信接口中的r
        /// </summary>
        /// <returns></returns>
        public static int Get_r()
        {
            string timespan = Utils.GetTimeStamp();

            return((int)(Convert.ToInt64(timespan) / 888));
        }