private void button1_Click(object sender, EventArgs e) { if (IdTextBox.Text.Trim().Length == 0) { MessageBox.Show("输入登陆ID", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (PwdTextBox.Text.Trim().Length == 0) { MessageBox.Show("输入密码", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { progressBar1.Show(); button1.Enabled = false; try { var request = new RestRequest("dlicense/v2/account/handshake", Method.GET); request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; var asyncHandle = client.ExecuteAsync <HandshakeResponse>(request, response => { if (!response.IsSuccessful) { ShowError(response.ErrorMessage); return; } HandshakeResponse handshake = response.Data; if (handshake != null && handshake.Status == 0) { Login(IdTextBox.Text, PwdTextBox.Text, handshake); } else { ShowError(handshake == null ? null : handshake.Msg); } }); } catch { progressBar1.Hide(); button1.Enabled = true; } } }
private void Login(string Id, string pwd, HandshakeResponse res) { var request = new RestRequest("dlicense/v2/account/login", Method.POST); request.RequestFormat = DataFormat.Json; request.AddHeader("timestamp", res.Timestamp); byte[] data = System.Text.Encoding.ASCII.GetBytes(pwd + "4969fj#k23#"); byte[] encryptedPwdByte; SHA1 sha = new SHA1CryptoServiceProvider(); encryptedPwdByte = sha.ComputeHash(data); System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (byte curByte in encryptedPwdByte) { sb.Append(curByte.ToString("x2")); } string encryptedPwd = sb.ToString(); sb = new System.Text.StringBuilder(); sb.Append("{\"phone\":\""); sb.Append("15372098619"); sb.Append("\","); sb.Append("\"password\":\""); sb.Append(encryptedPwd); sb.Append("\""); sb.Append("}"); string body = sb.ToString(); string token = body + "xgx3d*fe3478$ukx"; System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); byte[] srcBytes = System.Text.Encoding.UTF8.GetBytes(token); byte[] destBytes = md5.ComputeHash(srcBytes); sb = new System.Text.StringBuilder(); foreach (byte curByte in destBytes) { sb.Append(curByte.ToString("x2")); } request.AddHeader("token", sb.ToString()); request.AddParameter("application/json", body, ParameterType.RequestBody); request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; var asyncHandle = client.ExecuteAsync <LoginResponse>(request, response => { if (!response.IsSuccessful) { ShowError(response.ErrorMessage); return; } LoginResponse loginRes = response.Data; if (loginRes != null && loginRes.Status == 0) { AppConfig.Login = loginRes; if (loginRes.Status == 0) { QueryAccount(); } } else { ShowError(loginRes == null ? null : loginRes.Msg); } }); }