private async Task<StringReponse> DownloadString(Uri url) { try { using (var client = new HttpClient( new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate })) { client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate"); var resp = await client.GetAsync(url); var res = new StringReponse() { Response = await resp.Content.ReadAsStringAsync() }; if (resp.IsSuccessStatusCode) return res; res.ErrorMessage = "Request not successfull: Status Code " + resp.StatusCode + " returned. Message: " + res.Response; return res; } } catch (Exception ex) { LogHelper.Instance.Log(LogLevel.Error, "DownloadStringAsync failed for url " + url, this, ex); return new StringReponse() { ErrorMessage = "Request failed for url " + url }; } }
private async Task<StringReponse> PostForString(Uri url, string content) { try { using (var client = new HttpClient( new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate })) { client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate"); var credentials = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("json", content) }); var res = await client.PostAsync(url, credentials); var resp = new StringReponse() { Response = await res.Content.ReadAsStringAsync() }; if (res.IsSuccessStatusCode) return resp; resp.ErrorMessage = "Request not successfull: Status Code " + res.StatusCode + " returned. Message: " + resp.Response; return resp; } } catch (Exception ex) { LogHelper.Instance.Log(LogLevel.Error, "DownloadStringAsync failed for url " + url, this, ex); return new StringReponse() { ErrorMessage = "Request failed for url " + url }; } }