public static async Task<string> HttpPostStreamAsync(string url, StorageFile reqFile, CancellationToken token = new CancellationToken()) { using (var httpFilter = new HUwpHttpFilter()) using (var client = new HttpClient(httpFilter)) { var form = new HttpMultipartFormDataContent(); var inStream = await reqFile.OpenAsync(FileAccessMode.Read); form.Add(new HttpStreamContent(inStream), "file", reqFile.Name); var response = await client.PostAsync(new Uri(url), form); response.EnsureSuccessStatusCode(); var resStr = await response.Content.ReadAsStringAsync(); return resStr; } }
public static async Task HttpGetFileAsync(string url, StorageFile targetFile, Action<int, int> progressAction, CancellationToken token = new CancellationToken()) { using (var httpFilter = new HUwpHttpFilter()) using (var client = new HttpClient(httpFilter)) using (var response = await client.SendRequestAsync(new HttpRequestMessage(HttpMethod.Get, new Uri(url)), HttpCompletionOption.ResponseHeadersRead)) { response.EnsureSuccessStatusCode(); using (var resStream = await response.Content.ReadAsInputStreamAsync()) using (var classicResStream = resStream.AsStreamForRead()) { var resContentLength = response.Content.Headers.ContentLength; using (var fileStream = await targetFile.OpenStreamForWriteAsync()) { var buf = new byte[1000000]; var offset = 0; fileStream.Seek(0, SeekOrigin.Begin); while (true) { int read = await classicResStream.ReadAsync(buf, 0, buf.Length, token); progressAction.Invoke(offset, (int)resContentLength); if (read <= 0) break; await fileStream.WriteAsync(buf, 0, read, token); offset += read; } progressAction.Invoke((int)resContentLength, (int)resContentLength); } } } }
public static async Task<string> HttpPostStrAsync(string url, string body, CancellationToken token = new CancellationToken()) { using (var httpFilter = new HUwpHttpFilter()) using (var client = new HttpClient(httpFilter)) using (var response = await client.PostAsync(new Uri(url), new HttpStringContent(body))) { response.EnsureSuccessStatusCode(); var resStr = await response.Content.ReadAsStringAsync(); return resStr; } }
public static async Task<string> HttpGetStringAsync(string url, CancellationToken token = new CancellationToken()) { using (var httpFilter = new HUwpHttpFilter()) using (var client = new HttpClient(httpFilter)) using (var response = await client.SendRequestAsync(new HttpRequestMessage(HttpMethod.Get, new Uri(url)))) { response.EnsureSuccessStatusCode(); var resStr = await response.Content.ReadAsStringAsync(); return resStr; } }