/// <summary> /// Executes a HTTP POST to the supplied uri with the supplied HttpContent object, with optional cookies. Used as the base for other PostAsync methods. /// </summary> /// <param name="uri">Base uri of the POST</param> /// <param name="authentication">Authentication value to be used (if auth required)</param> /// <param name="content">Content of the POST request</param> /// <param name="sourceIp">Source request IP (if identified)</param> /// <param name="cookies">Cookies to be added to the request (if required)</param> /// <returns></returns> protected virtual async Task <RestResponse> PostAsync(string uri, RestAuthentication authentication, HttpContent content, string sourceIp, string xRedirect, IEnumerable <BasicKeyValuePair> cookies = null) { var request = CreateRequest(HttpMethod.Post, new Uri(uri), xRedirect, authentication, sourceIp, cookies); request.Content = content; var response = await _client.SendAsync(request); return(await CreateRestResponse(response)); }
/// <summary> /// Executes a HTTP GET to the supplied uri with optional basic auth, cookies and query params /// </summary> /// <param name="uri">Base uri of GET request</param> /// <param name="authentication">Authentication value to be used (if auth required)</param> /// <param name="xRedirect">x-Redirect header(if identified)</param> /// <param name="sourceIp">Source request IP (if identified)</param> /// <param name="queryParams">Query params to be added to the base url (if required)</param> /// <param name="cookies">Cookies to be added to the request (if required)</param> /// <returns>RestResponse containing status code, headers and content</returns> public virtual async Task <RestResponse> GetAsync(string uri, RestAuthentication authentication, string xRedirect = "APP", string sourceIp = null, IEnumerable <BasicKeyValuePair> queryParams = null, IEnumerable <BasicKeyValuePair> cookies = null) { UriBuilder builder = new UriBuilder(uri); builder.AddQueryParams(queryParams); var request = CreateRequest(HttpMethod.Get, builder.Uri, xRedirect, authentication, sourceIp, cookies); var response = await _client.SendAsync(request); return(await CreateRestResponse(response)); }
/// <summary> /// Executes a HTTP GET to the supplied uri with optional basic auth, cookies and query params /// </summary> /// <param name="uri">Base uri of GET request</param> /// <param name="authentication">Authentication value to be used (if auth required)</param> /// <param name="xRedirect">x-Redirect header(if identified)</param> /// <param name="sourceIp">Source request IP (if identified)</param> /// <param name="queryParams">Query params to be added to the base url (if required)</param> /// <param name="cookies">Cookies to be added to the request (if required)</param> /// <returns>RestResponse containing status code, headers and content</returns> public virtual async Task <String> GetAsyncWithRedirect(string uri, RestAuthentication authentication, MobileConnectRequestOptions requestOptions, string xRedirect = "APP", string sourceIp = null, IEnumerable <BasicKeyValuePair> queryParams = null, IEnumerable <BasicKeyValuePair> cookies = null) { UriBuilder builder = new UriBuilder(uri); builder.AddQueryParams(queryParams); var request = CreateRequest(HttpMethod.Get, builder.Uri, xRedirect, authentication, sourceIp, cookies); var response = await _client.SendAsync(request); return(response.RequestMessage.RequestUri.ToString()); }
private HttpRequestMessage CreateRequest(HttpMethod method, Uri uri, string xRedirect, RestAuthentication authentication, string sourceIp, IEnumerable <BasicKeyValuePair> cookies) { var message = new HttpRequestMessage(method, uri); if (cookies != null && cookies.Any()) { var cookieKeyValues = cookies.Select(x => string.Format("{0}={1}", x.Key, x.Value)); var cookieString = string.Join("; ", cookieKeyValues); message.Headers.Add("Cookie", cookieString); } if (!string.IsNullOrEmpty(sourceIp)) { message.Headers.Add(Headers.X_SOURCE_IP, sourceIp); } if (authentication != null) { message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(authentication.Scheme, authentication.Parameter); } if (xRedirect != null) { message.Headers.Add(Headers.X_REDIRECT, xRedirect); } else { message.Headers.Add(Headers.X_REDIRECT, Constants.Parameters.X_REDIRECT_DEFAULT_VALUE); } return(message); }
/// <summary> /// Executes a HTTP POST to the supplied uri with the supplied content type and content, with optional cookies /// </summary> /// <param name="uri">Base uri of the POST</param> /// <param name="authentication">Authentication value to be used (if auth required)</param> /// <param name="content">Content of the POST request</param> /// <param name="contentType">Content type of the POST request</param> /// <param name="sourceIp">Source request IP (if identified)</param> /// <param name="cookies">Cookies to be added to the request (if required)</param> /// <returns>RestResponse containing status code, headers and content</returns> public virtual async Task <RestResponse> PostAsync(string uri, RestAuthentication authentication, string content, string contentType, string sourceIp, string xRedirect = "APP", IEnumerable <BasicKeyValuePair> cookies = null) { return(await PostAsync(uri, authentication, new StringContent(content, Encoding.UTF8, contentType), sourceIp, xRedirect, cookies)); }
/// <summary> /// Executes a HTTP POST to the supplied uri with application/json content and optional cookies /// </summary> /// <param name="uri">Base uri of the POST</param> /// <param name="authentication">Authentication value to be used (if auth required)</param> /// <param name="content">Object to be serialized as JSON for POST content</param> /// <param name="sourceIp">Source request IP (if identified)</param> /// <param name="xRedirect">X-Redirect header value</param> /// <param name="cookies">Cookies to be added to the request (if required)</param> /// <returns>RestResponse containing status code, headers and content</returns> public virtual async Task <RestResponse> PostAsync(string uri, RestAuthentication authentication, object content, string sourceIp, string xRedirect = Parameters.X_REDIRECT_DEFAULT_VALUE, IEnumerable <BasicKeyValuePair> cookies = null) { var json = JsonConvert.SerializeObject(content); return(await PostAsync(uri, authentication, json, "application/json", sourceIp, xRedirect, cookies)); }
/// <summary> /// Executes a HTTP POST to the supplied uri with x-www-form-urlencoded content and optional cookies /// </summary> /// <param name="uri">Base uri of the POST</param> /// <param name="authentication">Authentication value to be used (if auth required)</param> /// <param name="formData">Form data to be added as POST content</param> /// <param name="xRedirect">x-Redirect header(if identified)</param> /// <param name="sourceIp">Source request IP (if identified)</param> /// <param name="cookies">Cookies to be added to the request (if required)</param> /// <returns>RestResponse containing status code, headers and content</returns> public virtual async Task <RestResponse> PostAsync(string uri, RestAuthentication authentication, IEnumerable <BasicKeyValuePair> formData, string sourceIp, string xRedirect, IEnumerable <BasicKeyValuePair> cookies = null) { var content = new FormUrlEncodedContent(formData.Where(x => !string.IsNullOrEmpty(x.Value)).Select(x => new KeyValuePair <string, string>(x.Key, x.Value))); return(await PostAsync(uri, authentication, content, sourceIp, xRedirect, cookies)); }
private HttpRequestMessage CreateDiscoveryRequest(HttpMethod method, Uri uri, string xRedirect, RestAuthentication authentication, string sourceIp, IEnumerable <BasicKeyValuePair> cookies) { var message = CreateRequest(method, uri, xRedirect, authentication, sourceIp, cookies); message.Headers.Add(Headers.SDK_VERSION, VersionUtils.GetSDKVersion()); return(message); }