static async void Login(string userName, string pwd) { // Create a New HttpClient object. HttpClient client = new HttpClient(); // Call asynchronous network methods in a try/catch block to handle exceptions try { HttpResponseMessage response = await client.GetAsync($"http://39.108.122.78:63439/api/connect?userName={userName}&userPwd={pwd}"); //response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); ReturnModel s = JsonConvert.DeserializeObject <ReturnModel>(responseBody); // Above three lines can be replaced with new helper method below // string responseBody = await client.GetStringAsync(uri); if (s.Code == "00000") { Toast.MakeText(Forms.Context, "登录成功", ToastLength.Short).Show(); var sqliteInfo = new Sqlite2(); sqliteInfo.CreateTable <UserLocalInfo>(); UserLocalInfo model = new UserLocalInfo() { Id = 1, UserName = userName, Pwd = pwd }; sqliteInfo.Insert(model); var page = new Match(); Application.Current.MainPage = new Match(); } else { Toast.MakeText(Forms.Context, "用户名或密码不正确", ToastLength.Short).Show(); return; } } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); } // Need to call dispose on the HttpClient object // when done using it, so the app doesn't leak resources client.Dispose(); }
private async void Register(string userName, string pwd) { // Create a New HttpClient object. HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync($"http://39.108.122.78:63439/api/connectregister?userName={userName}&pwd={pwd}"); //response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); ReturnModel s = JsonConvert.DeserializeObject <ReturnModel>(responseBody); // Above three lines can be replaced with new helper method below // string responseBody = await client.GetStringAsync(uri); if (s.Code == "00000") { Toast.MakeText(Forms.Context, "注册成功", ToastLength.Short).Show(); } else { Toast.MakeText(Forms.Context, "此账号已经被注册", ToastLength.Short).Show(); } }