예제 #1
0
        /// <summary>
        /// HTTP request with byte return type
        /// For a more detailed documentation see: https://github.com/FrendsPlatform/Frends.Web#HttpRequestBytes
        /// </summary>
        /// <param name="input">Input parameters</param>
        /// <param name="options">Optional parameters with default values</param>
        /// <returns>Object with the following properties: string BodyBytes, Dictionary(string,string) Headers. int StatusCode</returns>
        public static async Task <object> HttpRequestBytes([PropertyTab] Input input, [PropertyTab] Options options, CancellationToken cancellationToken)
        {
            var httpClient = GetHttpClientForOptions(options);
            var headers    = GetHeaderDictionary(input.Headers);

            using (var content = GetContent(input, headers))
            {
                var responseMessage = await GetHttpRequestResponseAsync(
                    httpClient,
                    input.Method.ToString(),
                    input.Url,
                    content,
                    headers,
                    options,
                    cancellationToken)
                                      .ConfigureAwait(false);

                cancellationToken.ThrowIfCancellationRequested();

                var response = new HttpByteResponse()
                {
                    BodyBytes   = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false),
                    ContentType = responseMessage.Content.Headers.ContentType,
                    StatusCode  = (int)responseMessage.StatusCode,
                    Headers     = GetResponseHeaderDictionary(responseMessage.Headers, responseMessage.Content.Headers)
                };

                if (!responseMessage.IsSuccessStatusCode && options.ThrowExceptionOnErrorResponse)
                {
                    throw new WebException($"Request to '{input.Url}' failed with status code {(int)responseMessage.StatusCode}.");
                }

                return(response);
            }
        }
예제 #2
0
파일: Web.cs 프로젝트: karoraita/Frends.Web
        /// <summary>
        /// HTTP request with byte return type
        /// For a more detailed documentation see: https://github.com/FrendsPlatform/Frends.Web#HttpRequestBytes
        /// </summary>
        /// <param name="input">Input parameters</param>
        /// <param name="options">Optional parameters with default values</param>
        /// <returns>Object with the following properties: string BodyBytes, Dictionary(string,string) Headers. int StatusCode</returns>
        public static async Task <object> HttpRequestBytes([CustomDisplay(DisplayOption.Tab)] Input input, [CustomDisplay(DisplayOption.Tab)] Options options, CancellationToken cancellationToken)
        {
            using (var handler = new WebRequestHandler())
            {
                cancellationToken.ThrowIfCancellationRequested();
                handler.SetHandleSettingsBasedOnOptions(options);

                using (var httpClient = new HttpClient(handler))
                {
                    var responseMessage = await GetHttpRequestResponseAsync(httpClient, input, options, cancellationToken).ConfigureAwait(false);

                    cancellationToken.ThrowIfCancellationRequested();

                    var response = new HttpByteResponse()
                    {
                        BodyBytes   = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false),
                        ContentType = responseMessage.Content.Headers.ContentType,
                        StatusCode  = (int)responseMessage.StatusCode,
                        Headers     = GetResponseHeaderDictionary(responseMessage.Headers, responseMessage.Content.Headers)
                    };

                    if (!responseMessage.IsSuccessStatusCode && options.ThrowExceptionOnErrorResponse)
                    {
                        throw new WebException($"Request to '{input.Url}' failed with status code {(int)responseMessage.StatusCode}.");
                    }

                    return(response);
                }
            }
        }