async public Task <byte[]> PostFormUrlEncodedAsync(string url, byte[] postData, RequestAuthorization authorization = null, int timeOutSeconds = 20) { var dataString = Encoding.UTF8.GetString(postData); url = CheckUrl(url); using (var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new StringContent(dataString, Encoding.UTF8, "application/x-www-form-urlencoded") }) { request.AddAuthentication(authorization); var cts = new CancellationTokenSource(timeOutSeconds * 1000); HttpResponseMessage responseMessage = null; try { var client = Create(url); responseMessage = await client.SendAsync(request, cts.Token); if (responseMessage.IsSuccessStatusCode) { var bytes = await responseMessage.Content.ReadAsByteArrayAsync(); return(bytes); } else { throw new HttpServiceException($"Request returned Statuscode { responseMessage.StatusCode }"); } } catch /*(TaskCanceledException ex)*/ { //if (ex.CancellationToken == cts.Token) { throw new System.Exception("The http operation is canceled (timed out)!"); } } finally { if (responseMessage != null) { responseMessage.Dispose(); } } } }
async public Task <string> PostJsonAsync(string url, string json, RequestAuthorization authorization = null, int timeOutSeconds = 20) { url = CheckUrl(url); using (var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new StringContent(json, Encoding.UTF8, "application/json") }) { request.AddAuthentication(authorization); var cts = new CancellationTokenSource(timeOutSeconds * 1000); HttpResponseMessage responseMessage = null; try { var client = Create(url); responseMessage = await client.SendAsync(request, cts.Token); if (responseMessage.IsSuccessStatusCode) { var response = await responseMessage.Content.ReadAsStringAsync(); return(response); } else { throw new HttpServiceException($"Request returned Statuscode { responseMessage.StatusCode }"); } } catch /*(TaskCanceledException ex)*/ { //if (ex.CancellationToken == cts.Token) { throw new System.Exception("The http operation is canceled (timed out)!"); } } finally { if (responseMessage != null) { responseMessage.Dispose(); } } } }
//public HttpService(IHttpClientFactory httpClientFactory, // IOptionsMonitor<HttpServiceOptions> optionsMonitor) //{ // _httpClientFactory = httpClientFactory; // _options = optionsMonitor?.CurrentValue ?? new HttpServiceOptions(); //} #region Get async public Task <byte[]> GetDataAsync(string url, RequestAuthorization authorization = null, int timeOutSeconds = 20) { url = CheckUrl(url); using (var request = new HttpRequestMessage(HttpMethod.Get, url)) { // ToDo: brauch man, wenn man Google Tiles downloade möchte... //request.Headers.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0")); request.AddAuthentication(authorization); var cts = new CancellationTokenSource(timeOutSeconds * 1000); HttpResponseMessage responseMessage = null; try { if (authorization?.ClientCerticate == null) { var client = Create(url); responseMessage = await client.SendAsync(request, cts.Token); } else { using (var clientHandler = new HttpClientHandler()) { clientHandler.ClientCertificateOptions = ClientCertificateOption.Manual; clientHandler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12; clientHandler.ClientCertificates.Add(authorization.ClientCerticate); using (var client = new HttpClient(clientHandler)) { responseMessage = await client.SendAsync(request, cts.Token); } } } if (responseMessage.IsSuccessStatusCode) { var bytes = await responseMessage.Content.ReadAsByteArrayAsync(); return(bytes); } else { throw new HttpServiceException($"Request returned Statuscode { responseMessage.StatusCode }"); } } catch /*(TaskCanceledException ex)*/ { //if (ex.CancellationToken == cts.Token) { throw new System.Exception("The http operation is canceled (timed out)!"); } } finally { if (responseMessage != null) { responseMessage.Dispose(); } } } }