示例#1
0
        /// <summary>
        /// Retrieves a string by using a http post to a remote url.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private async Task <HttpResult> LoadFromUrlByPost(HttpDataRequest request)
        {
            var uri      = BuildRequestUrl(request);
            var content  = CreateHttpContentFromRequest(request);
            var response = await client.PostAsync(uri, content);

            var result = await ConvertHttpResponseToHttpResult(response, request.ResultIsList);

            return(result);
        }
示例#2
0
        /// <summary>
        /// Retrieves a string and the request result details from an url. Is encapsulated in a HttpResult.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private async Task <HttpResult> LoadFromUrlByGet(HttpDataRequest request)
        {
            var query = BuildRequestUrl(request);
            HttpResponseMessage response = null;

            response = await client.GetAsync(query);

            var result = await ConvertHttpResponseToHttpResult(response, request.ResultIsList);

            return(result);
        }
示例#3
0
 /// <summary>
 /// Loads a string from a remote endpoint.
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public async Task <HttpResult> LoadFromUrl(HttpDataRequest request)
 {
     if (request.Method == HttpMethod.Get)
     {
         return(await LoadFromUrlByGet(request));
     }
     else if (request.Method == HttpMethod.Post)
     {
         return(await LoadFromUrlByPost(request));
     }
     else
     {
         throw new ArgumentException($"Currently only Get and Post are supported but {request.Method} was requested.");
     }
 }
示例#4
0
        /// <summary>
        /// Builds an url containing the base url, remote endpoint and url parameters.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private string BuildRequestUrl(HttpDataRequest request)
        {
            var builder = new UriBuilder(baseUrl);

            builder.Path = request.TotalPath;

            var query = HttpUtility.ParseQueryString(builder.Query);

            foreach (var pair in request.UrlParameters)
            {
                query[pair.Key] = pair.Value;
            }
            builder.Query = query.ToString();
            return(builder.ToString());
        }
示例#5
0
        /// <summary>
        /// Creates the content for a http post.
        /// </summary>
        /// <param name="request"></param>
        /// <returns><see cref="StringContent"/> if the request specifies a body, <see cref="FormUrlEncodedContent"/> is the request specifies form url encoded parameters or an empty <see cref="StringContent"/> if nothing is specified.</returns>
        /// <exception cref="ArgumentException"></exception>
        private HttpContent CreateHttpContentFromRequest(HttpDataRequest request)
        {
            if (!string.IsNullOrEmpty(request.Body) && request.FormParameters.Count > 0)
            {
                throw new ArgumentException("A request has a body set as well as form parameters. Only one can be processed at a time.");
            }

            if (!string.IsNullOrEmpty(request.Body))
            {
                return(new StringContent(request.Body));
            }
            else if (request.FormParameters.Count > 0)
            {
                return(new FormUrlEncodedContent(request.FormParameters));
            }
            else
            {
                return(new StringContent(string.Empty));
            }
        }