コード例 #1
0
        /// <summary>
        /// Retrieves bytes from the server
        /// </summary>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static async Task <byte[]> HttpRequestBytesAsync(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       = await client.UploadDataTaskAsync(settings.Url, settings.HttpVerb, settings.Encoding.GetBytes(settings.CapturedRequestContent));
                }
                else if (settings.Content is byte[])
                {
                    settings.ResponseByteData = await client.UploadDataTaskAsync(settings.Url, settings.Content as byte[]);
                }
                else
                {
                    throw new ArgumentException("Data must be either string or byte[].");
                }
            }
            else
            {
                settings.ResponseByteData = await client.DownloadDataTaskAsync(settings.Url);
            }

            settings.Response = client.Response;

            return(settings.ResponseByteData);
        }
コード例 #2
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 async Task<string> HttpRequestStringAsync(HttpRequestSettings settings)
        {
            var client = new HttpUtilsWebClient(settings);

            
            if (settings.HttpVerb == "GET")
                settings.CapturedResponseContent = await client.DownloadStringTaskAsync(new Uri(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 = await client.UploadStringTaskAsync(settings.Url, settings.HttpVerb, settings.CapturedRequestContent);
                }
                else if (settings.Content is byte[])
                {
                    settings.ResponseByteData = await client.UploadDataTaskAsync(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 async Task <string> HttpRequestStringAsync(HttpRequestSettings settings)
        {
            var client = new HttpUtilsWebClient();

            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 = await client.DownloadStringTaskAsync(new Uri(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 = await client.UploadStringTaskAsync(settings.Url, settings.HttpVerb, settings.CapturedRequestContent);
                }
                else if (settings.Content is byte[])
                {
                    settings.ResponseByteData = await client.UploadDataTaskAsync(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);
        }