/// <summary>
        /// Executes asynchronous request and returns wrapped object for the http response with headers, content ... etc
        /// </summary>
        /// <returns></returns>
        public Task <ResponseRichResult> ExecuteRichAsync()
        {
            var task = this.ExecuteWebResposeAsync()
                       .ContinueWith(t =>
            {
                using (var response = t.Result)
                    using (var stream = response.GetResponseStream())
                        using (var ms = new MemoryStream())
                        {
                            var buffer = new byte[16 * 1024];

                            int read;
                            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                ms.Write(buffer, 0, read);
                            }

                            var headers = (response.Headers?.AllKeys ?? new String[0])
                                          .Select(x => new { key = x, value = response.Headers[x] })
                                          .ToDictionary(x => x.key, x => x.value);

                            var cookies = response.Cookies.OfType <Cookie>().ToList();

                            return(ResponseRichResult.Create(statusCode: response.StatusCode, bytes: ms.ToArray(), headers: headers, cookies: cookies, charset: response.CharacterSet));
                        }
            });

            return(task);
        }
Esempio n. 2
0
        private static String GetMimeType(ResponseRichResult httpResult)
        {
            var format = httpResult.GetHeaderValue("Content-Type").Split(';')[0];

            if (httpResult.HasHeader("Content-Disposition"))
            {
                var contentDispositionStr = httpResult.GetHeaderValue("Content-Disposition");

                // for cases when dublicate header is send (example: https://uodo.gov.pl/pl/file/701) which is interpreted by the .net as comma separated header values (https://tools.ietf.org/html/rfc7230#section-3.2.2)
                // we ca try to get single value from the aggregated header values, otherwise content disposition is throwing exception during creation
                contentDispositionStr = contentDispositionStr.Split(',').Distinct().Single();
                var fileNameIndex = contentDispositionStr.IndexOf("filename=");
                if (fileNameIndex >= 0)
                {
                    var before          = contentDispositionStr.Substring(0, fileNameIndex);
                    var fileNameContent = contentDispositionStr.Substring(fileNameIndex + "filename=".Length).Replace(" ", "_");
                    contentDispositionStr = $"{before}filename={fileNameContent}";
                }

                contentDispositionStr = contentDispositionStr.EncodeNonAsciiCharacters();
                var contentDisposition = new ContentDisposition(contentDispositionStr);
                var fileName           = contentDisposition.FileName;

                format = MimeTypesMap.GetMimeType(fileName);
            }

            return(format);
        }