public static async Task <ISimpleResponse <T> > SendMessage <T>(ISimpleRestClient client, ISimpleMessage message) { T messageTypeToReturn = Activator.CreateInstance <T>(); ISimpleResponse <T> toReturn = new SimpleResponse <T>(); HttpClient restClient = new HttpClient(); Windows.Web.Http.HttpMethod method = message.HttpMethod == HttpMethod.Get ? Windows.Web.Http.HttpMethod.Get : Windows.Web.Http.HttpMethod.Post; // Add Global Request Parameters foreach (var globalRequestParameter in client.GlobalRequestParameters) { message.Parameters[globalRequestParameter.Key] = globalRequestParameter.Value; } Uri uriWithQueryString = await MessageHelper.BuildUri(baseUrl : client.BaseUrl, endPointPath : message.EndPointPath, queryStringParameters : message.Parameters); HttpRequestMessage requestMessage = new HttpRequestMessage(method, uriWithQueryString); // Add Headers foreach (var header in client.Headers) { requestMessage.Headers.Add(header.Key, header.Value); } HttpResponseMessage response = await restClient.SendRequestAsync(requestMessage); toReturn.DataRaw = response.Content; toReturn.DataConverted = Deserializer.JsonToObject <T>(response.Content.ToString()); return(toReturn); }
public bool Execute(Windows.Web.Http.HttpMethod method, string url) { bool result = false; Task.Run(async() => { result = await ExecuteAsync(method, url); }).Wait(); return(result); }
/// <summary> /// Sends a WebDav/Http-Request to the Webdav-Server /// </summary> /// <param name="networkCredential"></param> /// <param name="requestUrl"></param> /// <param name="method"></param> /// <param name="contentStream">contentStream is not needed if sending a PROPFIND</param> /// <param name="customHeaders"></param> public WebDavRequest(NetworkCredential networkCredential, Uri requestUrl, HttpMethod method, Stream contentStream = null, Dictionary <string, string> customHeaders = null) { _networkCredential = networkCredential; _requestUrl = requestUrl; _method = method; _contentStream = contentStream; _customHeaders = customHeaders; var filter = new HttpBaseProtocolFilter { AllowUI = false }; if (networkCredential.UserName != string.Empty && networkCredential.Password != string.Empty) { filter.ServerCredential = new PasswordCredential(requestUrl.DnsSafeHost, networkCredential.UserName, networkCredential.Password); } _httpClient = new HttpClient(filter); }
protected async Task SendRequest(String serviceURL, Windows.Web.Http.HttpMethod http_method, String requestBody, IDictionary <String, String> requestProperties) { HttpClient httpClient = new HttpClient(); CancellationTokenSource cts = new CancellationTokenSource(); try { Uri resourceAddress = new Uri(serviceURL); HttpRequestMessage request = new HttpRequestMessage(http_method, resourceAddress); if (!string.IsNullOrEmpty(requestBody)) { Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(requestBody ?? "")); request.Content = new HttpStreamContent(stream.AsInputStream()); } if (requestProperties != null && requestProperties.Count > 0) { foreach (var key in requestProperties.Keys) { request.Properties.Add(new KeyValuePair <string, object>(key, requestProperties[key])); } } HttpResponseMessage response = await httpClient.SendRequestAsync( request, HttpCompletionOption.ResponseHeadersRead).AsTask(cts.Token); SetResponseHeaders(response.Headers); this.responseData = await response.Content.ReadAsStringAsync(); this.statusCode = (int)response.StatusCode; } catch (TaskCanceledException tce) { this.responseData = string.Empty; this.errorMessage = "Request canceled!"; throw tce; } catch (Exception ex) { this.responseData = string.Empty; this.errorMessage = "Error: " + ex.Message; throw ex; } }
/// <summary> /// Sends a WebDav/Http-Request to the Webdav-Server /// </summary> /// <param name="networkCredential"></param> /// <param name="requestUrl"></param> /// <param name="method"></param> /// <param name="contentStream">contentStream is not needed if sending a PROPFIND</param> /// <param name="customHeaders"></param> public WebDavRequest(NetworkCredential networkCredential, Uri requestUrl, HttpMethod method, Stream contentStream = null, Dictionary <string, string> customHeaders = null) { _networkCredential = networkCredential; _requestUrl = requestUrl; _method = method; _contentStream = contentStream; _customHeaders = customHeaders; var filter = new HttpBaseProtocolFilter { AllowUI = false }; if (networkCredential.UserName != string.Empty && networkCredential.Password != string.Empty) { filter.ServerCredential = new PasswordCredential(requestUrl.DnsSafeHost, networkCredential.UserName, networkCredential.Password); } filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);//when a self signed cert was allowed by the user it might still have a wrong name (demo vm for example with a dynamic ip) _httpClient = new HttpClient(filter); }
private async Task <bool> ExecuteAsync(Windows.Web.Http.HttpMethod method, string url) { ClearResponseData(); if (string.IsNullOrEmpty(url)) { throw new ArgumentException("The url value is empty!"); } String fullUrl = url; if (queryParams != null && queryParams.Count > 0) { ICollection <String> keyEnum = queryParams.Keys; fullUrl += "?"; foreach (var key in keyEnum) { fullUrl = key + "=" + queryParams[key] + "&"; } fullUrl = fullUrl.Substring(0, fullUrl.Length - 1); } bool successful = true; try { await this.SendRequest(fullUrl, method, requestBody, requestHeaders); } catch (Exception e) { errorMessage = e.Message; successful = false; } finally { ClearRequestData(); } return(successful); }