public async Task<string> RegisterUserJsonRequest(UserModel model) { using (var client = new HttpClient()) { var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"); var result = await client.PostAsync("http://xamarin-rest-service.azurewebsites.net/demo/registeruser", content); return await result.Content.ReadAsStringAsync(); } }
public async void CallMyService() { ServiceWrapper serviceWrapper = new ServiceWrapper(); var model = new UserModel { Username = "******", Password = "******" }; var result = await serviceWrapper.GetData("test"); txtMessages.Text = "get call says: " + result; result = await serviceWrapper.RegisterUserJsonRequest(model); txtMessages.Text = "json post call says: " + result; result = await serviceWrapper.RegisterUserFormRequest(model); txtMessages.Text = "form post request says: " + result; }
public async Task<string> RegisterUserFormRequest(UserModel model) { using (var client = new HttpClient()) { var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Username", model.Username), new KeyValuePair<string, string>("Password", model.Password) }); var result = await client.PostAsync("http://xamarin-rest-service.azurewebsites.net/demo/registeruser", content); return await result.Content.ReadAsStringAsync(); } }
public HttpResponseMessage RegisterUser(UserModel model) { if (!ModelState.IsValid) { return Request.CreateResponse(HttpStatusCode.BadRequest, "invalid model"); } try { //some other code to actually register a user somewhere return Request.CreateResponse(HttpStatusCode.OK, "true"); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); } }
protected async override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Get our button from the layout resource Button button = FindViewById<Button>(Resource.Id.MyButton); ServiceWrapper serviceWrapper = new ServiceWrapper(); var model = new UserModel { Username = "******", Password = "******" }; var result = await serviceWrapper.GetData("test"); button.Text = "get call says: " + result; result = await serviceWrapper.RegisterUserJsonRequest(model); button.Text = "json post call says: " + result; result = await serviceWrapper.RegisterUserFormRequest(model); button.Text = "form post request says: " + result; }