//Functions that runs an HTTP request and returns a response as string. public async static Task<string> RunHttpRequestWithStringReturn(Uri uri) { var httpFilter = new HttpBaseProtocolFilter(); httpFilter.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent; var httpClient = new HttpClient(httpFilter); HttpResponseMessage result = new HttpResponseMessage(); try { result = await httpClient.GetAsync(uri); } catch { result.StatusCode = HttpStatusCode.Forbidden; return await Task.FromResult("Server timed out"); ; } if (!result.IsSuccessStatusCode) { return await Task.FromResult("Server timed out"); } return result.Content.ToString(); }
public async void ImportPicture() { var picturePicker = new PicturePicker(); var pictures = await picturePicker.openPicker.PickMultipleFilesAsync(); if (pictures.Count > 0) { var i = 0; IsLoading(true, "importing"); foreach (var picture in pictures) { SetLoadingText("Importing pictures... (" + i + "/" + pictures.Count() + ")"); using (IInputStream stream = await picture.OpenAsync(FileAccessMode.Read)) { var multipartContent = new HttpMultipartFormDataContent(); multipartContent.Add(new HttpStreamContent(stream), "newPicture", picture.Name); var uri = new Uri("http://" + serverIP + "/server/requestHandler.php?operation=import"); var client = new HttpClient(); var response = await client.PostAsync(uri, multipartContent); if (!response.Content.ToString().Contains("Success")) { MessageDialog dialog = new MessageDialog(response.Content.ToString()); await dialog.ShowAsync(); } } i++; } IsLoading(true, "pictures"); await SharedFunctions.GetPicturesAndUpdateGridView(); IsLoading(false, "pictures"); } }