public static async Task <BitmapImage> DownloadFile(Uri uri) { Windows.Web.Http.HttpClient http = new Windows.Web.Http.HttpClient(); try { IBuffer buffer = await http.GetBufferAsync(uri); BitmapImage img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(buffer); stream.Seek(0); await img.SetSourceAsync(stream); await StorageImageFolder(stream, uri); return(img); } } catch (Exception) { return(null); } finally { http.Dispose(); } }
public void Dispose() { if (myClient != null) { myClient.Dispose(); } }
public async Task <Answer> PostAsync(string api, string content) { Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(); try { var postContent = new Windows.Web.Http.HttpStringContent(content); Uri resourceAddress = new Uri(api); Windows.Web.Http.HttpResponseMessage response = await client.PostAsync(resourceAddress, postContent); string responseContent = await response.Content.ReadAsStringAsync(); if (response.StatusCode == Windows.Web.Http.HttpStatusCode.Ok) { return(new Answer(responseContent, ResponseStatus.Success)); } else if (response.StatusCode == Windows.Web.Http.HttpStatusCode.BadRequest) { return(new Answer(responseContent, ResponseStatus.InvalidToken)); } else if ((int)response.StatusCode == 429) { return(new Answer(responseContent, ResponseStatus.InvalidToken)); } } catch (HttpRequestException) { client.Dispose(); } catch (Exception) { client.Dispose(); } Uri dataUri = new Uri("ms-appx:///DataModel/ConnectionError.json"); var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(dataUri); string jsonText = await Windows.Storage.FileIO.ReadTextAsync(file); return(new Answer(jsonText, ResponseStatus.ConnectionError)); }