/// <summary>
        /// Retrieves bytes from the server
        /// </summary>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static byte[] HttpRequestBytes(HttpRequestSettings settings)
        {
            var client = new HttpUtilsWebClient(settings);

            if (settings.Content != null)
            {
                if (!string.IsNullOrEmpty(settings.ContentType))
                {
                    client.Headers["Content-type"] = settings.ContentType;
                }

                if (settings.Content is string)
                {
                    settings.CapturedRequestContent = settings.Content as string;
                    settings.ResponseByteData       = client.UploadData(settings.Url, settings.HttpVerb, settings.Encoding.GetBytes(settings.CapturedRequestContent));
                }
                else if (settings.Content is byte[])
                {
                    settings.ResponseByteData = client.UploadData(settings.Url, settings.Content as byte[]);
                }
                else
                {
                    throw new ArgumentException("Data must be either string or byte[].");
                }
            }
            else
            {
                settings.ResponseByteData = client.DownloadData(settings.Url);
            }

            settings.Response = client.Response;

            return(settings.ResponseByteData);
        }
        /// <summary>
        /// Retrieves and Http request and returns data as a string.
        /// </summary>
        /// <param name="settings">Pass HTTP request configuration parameters object to set the URL, Verb, Headers, content and more</param>
        /// <returns>string of HTTP response</returns>
        public static string HttpRequestString(HttpRequestSettings settings)
        {
            var client = new HttpUtilsWebClient(settings);            

            if (settings.HttpVerb == "GET")
                settings.CapturedResponseContent = client.DownloadString(settings.Url);
            else
            {
                if (!string.IsNullOrEmpty(settings.ContentType))
                    client.Headers["Content-type"] = settings.ContentType;

                if (settings.Content is string)
                {
                    settings.CapturedRequestContent = settings.Content as string;
                    settings.CapturedResponseContent = client.UploadString(settings.Url, settings.HttpVerb, settings.CapturedRequestContent);
                }
                else if (settings.Content is byte[])
                {
                    settings.ResponseByteData = client.UploadData(settings.Url, settings.Content as byte[]);
                    settings.CapturedResponseContent = Encoding.UTF8.GetString(settings.ResponseByteData);
                }
                else
                    throw new ArgumentException("Data must be either string or byte[].");
            }

            settings.Response = client.Response;
            

            return settings.CapturedResponseContent;
        }
示例#3
0
        /// <summary>
        /// Retrieves and Http request and returns data as a string.
        /// </summary>
        /// <param name="settings">Pass HTTP request configuration parameters object to set the URL, Verb, Headers, content and more</param>
        /// <returns>string of HTTP response</returns>
        public static string HttpRequestString(HttpRequestSettings settings)
        {
            var client = new HttpUtilsWebClient(settings);

            if (settings.Credentials != null)
            {
                client.Credentials = settings.Credentials;
            }

            if (settings.Proxy != null)
            {
                client.Proxy = settings.Proxy;
            }

            if (settings.Headers != null)
            {
                foreach (var header in settings.Headers)
                {
                    client.Headers[header.Key] = header.Value;
                }
            }


            if (settings.HttpVerb == "GET")
            {
                settings.CapturedResponseContent = client.DownloadString(settings.Url);
            }
            else
            {
                if (!string.IsNullOrEmpty(settings.ContentType))
                {
                    client.Headers["Content-type"] = settings.ContentType;
                }

                if (settings.Content is string)
                {
                    settings.CapturedRequestContent  = settings.Content as string;
                    settings.CapturedResponseContent = client.UploadString(settings.Url, settings.HttpVerb, settings.CapturedRequestContent);
                }
                else if (settings.Content is byte[])
                {
                    settings.ResponseByteData        = client.UploadData(settings.Url, settings.Content as byte[]);
                    settings.CapturedResponseContent = Encoding.UTF8.GetString(settings.ResponseByteData);
                }
                else
                {
                    throw new ArgumentException("Data must be either string or byte[].");
                }
            }

            settings.Response = client.Response;


            return(settings.CapturedResponseContent);
        }