/// <summary> /// 登陆当前客户端。 /// </summary> /// <param name="http">使用的连接</param> /// <param name="userName">用户名</param> /// <param name="passwordMd5">加密后的密码</param> /// <exception cref="LogOnException">登陆失败</exception> public static IAsyncAction LogOn(HttpClient http, string userName, string passwordMd5) { return(Run(async token => { try { await sendUdpRequest(userName).ConfigureAwait(false); if (!waiter.WaitOne(1000)) { throw new LogOnException(LogOnExceptionType.ConnectError); } if (type != -101L) { throw new LogOnException(LogOnExceptionType.AuthError); } var pass = new byte[49]; pass[0] = ((byte)(int)(userID & 0xFF)); for (var i = 0; i < 32; i++) { pass[i + 1] = (byte)passwordMd5[i]; } Array.Copy(challenge, 0, pass, 33, challenge.Length); var sendPass = MD5Helper.GetMd5Hash(pass); var action = http.PostStrAsync(srunUri, $"action=login&username={userName}&password={sendPass}&drop=0&type=11&n=120&ac_id=1&mac={MacAddress.Current}&chap=1"); token.Register(action.Cancel); var res = await action; if (res.StartsWith("login_error")) { throw LogOnException.GetByErrorString(res.Substring(res.IndexOf('#') + 1)); } //模拟网页方式登陆相关代码 //action = http.PostStrAsync(logOnUri, $"action=login&username={userName}&password={{MD5_HEX}}{passwordMd5}&type=1&ac_id=1&mac={MacAddress.Current}"); //res = await action; //if(!res.StartsWith("E")) // return; //else // throw LogOnException.GetByErrorString(res); } catch (OperationCanceledException) { throw; } catch (LogOnException) { throw; } catch (Exception ex) { throw new LogOnException(LogOnExceptionType.ConnectError, ex); } })); }
/// <summary> /// 异步登陆网络。 /// </summary> /// <exception cref="WebConnect.LogOnException">在登陆过程中发生错误。</exception> public IAsyncAction LogOnAsync() { return(Run(async token => { string res = null; IAsyncOperation <string> post = null; token.Register(() => post?.Cancel()); using (var http = new HttpClient()) { http.DefaultRequestHeaders.UserAgent.Add(new HttpProductInfoHeaderValue("Mozilla", "5.0")); http.DefaultRequestHeaders.UserAgent.Add(new HttpProductInfoHeaderValue("Windows NT 10.0")); Func <Task <bool> > check = async() => { try { post = http.PostStrAsync(logOnUri, "action=check_online"); return "online" == await post; } catch (OperationCanceledException) { throw; } catch (Exception ex) { throw new LogOnException(LogOnExceptionType.ConnectError, ex); } }; Func <Task <bool> > logOn = async() => { try { post = http.PostStrAsync(logOnUri, $"action=login&username={userName}&password={{MD5_HEX}}{passwordMd5}&type=1&ac_id=1&mac={MacAddress.Current}"); res = await post; if (!res.StartsWith("E")) { return true; } else { throw LogOnException.GetByErrorString(res); } } catch (OperationCanceledException) { throw; } catch (LogOnException) { throw; } catch (Exception ex) { throw new LogOnException(LogOnExceptionType.ConnectError, ex); } }; try { if (this.IsOnline = await check()) { return; } if (this.IsOnline = await logOn()) { return; } this.IsOnline = false; } catch (OperationCanceledException) { return; } } })); }