Example #1
0
        /// <summary>
        /// Sends a direct HTTP request.
        /// </summary>
        /// <param name="request">The HTTP Request.</param>
        /// <param name="options">Some options for the WebRequestClient.</param>
        /// <returns>A string, as the response of the page.</returns>
        /// <exception cref="GdWebException">An exception from the Boomlings API, which can be disabled to just return the code itself.</exception>
        public static string SendRequest(WebRequest request, WebRequestClientOptions options = null)
        {
            if (options == null)
            {
                options = new WebRequestClientOptions();
            }

            var setupRequest = new HttpRequestMessage(request.Method, request.Url);

            foreach (var kvp in request.Headers)
            {
                setupRequest.Headers.Add(kvp.Key, kvp.Value);
            }

            setupRequest.Content = request.Content;
            var result = "";

            Task.Run(async() =>
            {
                using (var response = await client.SendAsync(setupRequest))
                {
                    result = await response.Content.ReadAsStringAsync();

                    if (int.TryParse(result, out var errorCode) && options.IgnoreGdExceptions == false)
                    {
                        throw new GdWebException(((GdErrorType)errorCode).GetDescription())
                        {
                            ErrorType = (GdErrorType)errorCode
                        }
                    }
                    ;
                }
            }).Wait();

            return(result);
        }
Example #2
0
 public WebRequestClient(WebRequestClientOptions options)
 {
     Options = options;
 }