async void LoginBtnClicked(object sender, EventArgs s) { LoginRequest loginReq = new LoginRequest(); try { loginReq.name = nameInput.Text; string before_hash = nameInput.Text + ":::" + passInput.Text; byte[] data = System.Text.Encoding.UTF8.GetBytes(before_hash); var hasher = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha512); byte[] hash = hasher.HashData(data); loginReq.hashed = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower(); } catch (NullReferenceException e) { DependencyService.Get <IMyFormsToast>().Show("NULL EXCEPTION ERROR: name,passがNullです:" + e.Message); return; } // サーバにログイン WrappedHttpClient wHttpClient = new WrappedHttpClient(); String baseURL = ServerInfo.url; String jsonString = JsonConvert.SerializeObject(loginReq); var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage response = await wHttpClient.PostAsync(baseURL + "/login", content); String result = await response.Content.ReadAsStringAsync(); User user = JsonConvert.DeserializeObject <User>(result); if (user.id == 0) { // ログイン失敗 DependencyService.Get <IMyFormsToast>().Show("ログイン失敗"); return; } else { //Login出来たフラグを立ててメインページに遷移 App.IsUserLoggedIn = true; Application.Current.Properties["user"] = user; Navigation.InsertPageBefore(new madaaru_mk2Page(), this); await Navigation.PopAsync(); } }
public async Task <P_Res_Buy_Thing> PostBuyThingInfo(Bought_thing bt) { User userInfo = (User)Application.Current.Properties["user"]; int user_id = userInfo.id; string serverUrl = ServerInfo.url; string searchAPIUrl = "/buy_things?"; string reqUrl = $"{serverUrl}{searchAPIUrl}?user_id={user_id}"; //buy_things?user_id=1; string jsonString = JsonConvert.SerializeObject(bt); WrappedHttpClient whc = new WrappedHttpClient(); var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage response = await whc.PostAsync(reqUrl, content); string result = await response.Content.ReadAsStringAsync(); P_Res_Buy_Thing prbt = JsonConvert.DeserializeObject <P_Res_Buy_Thing>(result); return(prbt); }
async void SignupBtnClicked(object sender, EventArgs s) { SignupReq signupReq = new SignupReq(); try { signupReq.name = nameInput.Text; if (passInput.Text != passInput2.Text) { DependencyService.Get <IMyFormsToast>().Show("パスワードが一致しません"); return; } signupReq.password = passInput.Text; } catch (NullReferenceException e) { DependencyService.Get <IMyFormsToast>().Show("NULL EXCEPTION ERROR: name,passがNullです:" + e.Message); return; } // サーバにサインアップ WrappedHttpClient wHttpClient = new WrappedHttpClient(); String baseURL = ServerInfo.url; String jsonString = JsonConvert.SerializeObject(signupReq); var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage response = await wHttpClient.PostAsync(baseURL + "/users", content); String result = await response.Content.ReadAsStringAsync(); SignupRes signupRes = JsonConvert.DeserializeObject <SignupRes>(result); if (signupRes.status != 200) { // サインアップ失敗 DependencyService.Get <IMyFormsToast>().Show("サインアップ失敗"); return; } else { //Login出来たフラグを立ててメインページに遷移 Navigation.InsertPageBefore(new LoginPage(), this); await Navigation.PopAsync(); } }
//買う商品の情報オブジェクトBought_thingsを受け取ってJson化してPost後、 //サーバーから帰ってくる[次にこの商品を買うべき日付]をオブジェクト化して変えす //public async Task<List<Next_buy_date>> PostBoughtThingsInfo(Bought_things bt) public async Task <Next_buy_date> PostBoughtThingInfo(Bought_thing bt) { string serverUrl = ServerInfo.url; string APIUrl = "/bought_things"; string reqUrl = $"{serverUrl}{APIUrl}"; string jsonString = JsonConvert.SerializeObject(bt); //HttpClient hc = new HttpClient(); WrappedHttpClient whc = new WrappedHttpClient(); var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); //HttpResponseMessage response = await hc.PostAsync(reqUrl, content); HttpResponseMessage response = await whc.PostAsync(reqUrl, content); string result = await response.Content.ReadAsStringAsync(); Next_buy_date NBD = JsonConvert.DeserializeObject <Next_buy_date>(result); return(NBD); }
//Expendables情報をpost後、その情報をもう一度返す //返り値のExpendablesは使い道ないので捨てて良い //なら返り値要らないのでは public async Task <Expendables> PostExpendablesInfo(Bought_expendable be) { string serverUrl = ServerInfo.url; string APIUrl = "/expendables.json"; string reqUrl = $"{serverUrl}{APIUrl}"; string jsonString = JsonConvert.SerializeObject(be); //HttpClient hc = new HttpClient(); WrappedHttpClient whc = new WrappedHttpClient(); var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); //HttpResponseMessage response = await hc.PostAsync(reqUrl, content); HttpResponseMessage response = await whc.PostAsync(reqUrl, content); //result string => "" //resut無いのに必要? string result = await response.Content.ReadAsStringAsync(); Expendables ex = JsonConvert.DeserializeObject <Expendables>(result); return(ex); }