public void Login() { var getUuidStr = HttpClient.GetStringAsync(string.Format(WeChatHttpProUrl.GetUUIdUrl, DateTime.Now.ToUnixJs())).Result; if (HttpWeChatHelp.GetUuid(getUuidStr, out (string code, string uuid)uuid) && uuid.code.Equals("200")) { var code = string.Empty; //微信返回的二维码状态 try { var getQrcodeBytes = HttpClient.GetByteArrayAsync(string.Format(WeChatHttpProUrl.GetQrCodeUrl, uuid.uuid)).Result; CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30)); QRCodeChangeEvent?.Invoke(this, new QrCodeChangEventArgs(uuid.uuid, getQrcodeBytes)); while (true) { cancellationTokenSource.Token.ThrowIfCancellationRequested(); var statusStr = HttpClient.GetStringAsync(string.Format(WeChatHttpProUrl.GetQrStatusUrl, uuid.uuid)).Result; if (Regex.IsMatch(statusStr, "window.code=([\\d]+)")) { code = Regex.Match(statusStr, "window.code=([\\d]+)").Groups[1].Value; if (Enum.TryParse(code, out EQrCodeStatus status)) { QRCodeStatusChangeEvent?.Invoke(this, new QrCodeStatusChangEventArgs(uuid.uuid, status)); if (code == "200") { string redirect_uri = RegexHelper.GetValue("(?<=window.redirect_uri=\").*?(?=\")", statusStr); if (UrlHelp.ParseUrl(redirect_uri, out string baseUrl, out NameValueCollection nvc)) { AuthorizationInfo.Init(nvc["ticket"], nvc["uuid"], nvc["lang"], nvc["scan"]); WeChatData = new WeChatData(this); InitDatabeforeEvent?.Invoke(this, new EventArgs()); WeChatData.InitData(); InitDatacompleteEvent?.Invoke(this, new EventArgs()); //初始化消息监听 m_weChatMessageMonitor = new WeChatMessageMonitor(this, this.m_cancellationToken.Token); m_weChatMessageMonitor.SyncCheck(); } else { throw new AnalyzeLoginParameterException(statusStr, null); } } if (code == "408" || code == "200") { break; } } } } }
public HttpWeChat() { HttpClient = new HttpClient(); AuthorizationInfo = new AuthorizationInfo(this); }