public static async void APIPost(string page, FormUrlEncodedContent http_content, Action <string> followup_success, Action followup_res_is_null, Action <string> followup_exception_catch) { ModuloReloaded6.App app = App.Current as App; HttpClient client = app.Client; //if(client == null) // client.CancelPendingRequests(); client = new HttpClient(); using (client) using (HttpResponseMessage response = await client.PostAsync(page, http_content)) using (HttpContent content = response.Content) { try { var result = await content.ReadAsStringAsync(); if (result == null) { Device.BeginInvokeOnMainThread(() => followup_res_is_null()); } else { Device.BeginInvokeOnMainThread(() => followup_success(result)); } } catch (Exception ex) { Device.BeginInvokeOnMainThread(() => followup_exception_catch(ex.ToString())); } } }
private async void APIGetTest() { string page = "http://145.24.222.229:8081/webapi/public/api/teachers"; ModuloReloaded6.App app = App.Current as App; HttpClient client = app.Client; string result = ""; using (client) using (HttpResponseMessage response = await client.GetAsync(page)) using (HttpContent content = response.Content) { try { result = await content.ReadAsStringAsync(); if (result == null) { Device.BeginInvokeOnMainThread(() => lol("result == null")); //Navigation.RemovePage(this); } else { Device.BeginInvokeOnMainThread(() => lol(result)); //Navigation.PushAsync(new APITestPage(result)); //Navigation.RemovePage(this); } } catch (Exception ex) { lol(ex.ToString()); } } }
public static async void APIWaitGet(string page, Action <string> followup_success, Action followup_res_is_null, Action <string> followup_exception_catch) { ModuloReloaded6.App app = App.Current as App; HttpClient client = app.Client; string result = ""; HttpResponseMessage response = null; Device.BeginInvokeOnMainThread(async() => response = await client.GetAsync(page)); //using(client) //using(HttpResponseMessage response = await client.GetAsync(page)) using (HttpContent content = response.Content) { try { Device.BeginInvokeOnMainThread(async() => result = await content.ReadAsStringAsync()); //result = await content.ReadAsStringAsync(); if (result == null) { Device.BeginInvokeOnMainThread(() => followup_res_is_null()); } else { Device.BeginInvokeOnMainThread(() => followup_success(result)); } } catch (Exception ex) { Device.BeginInvokeOnMainThread(() => followup_exception_catch(ex.ToString())); } } }
private async Task <string> MyTask() { ModuloReloaded6.App app = App.Current as App; HttpClient client = app.Client; string page = "http://api.openweathermap.org/data/2.5/weather?q={city%20name}"; string result = ""; using (client) using (HttpResponseMessage response = await client.GetAsync(page)) using (HttpContent content = response.Content) { try { result = await content.ReadAsStringAsync(); if (result == null) { //Navigation.PushAsync(new APITestPage("result == null")); //Navigation.RemovePage(this); } else { return(result); //Navigation.PushAsync(new APITestPage(result)); //Navigation.RemovePage(this); } } catch (Exception ex) { Console.WriteLine("MyStackTrace: {0}", ex.ToString()); } } return(result); }