Example #1
0
        public static async Task <RequestData> FromRequest(HttpRequest request)
        {
            var headers = new List <HeaderData>(request.Headers.Count);

            foreach (var header in request.Headers)
            {
                foreach (var value in header.Value)
                {
                    headers.Add(new HeaderData
                    {
                        Name  = header.Key,
                        Value = value
                    });
                }
            }

            string body = null;

            if (request.Body != null)
            {
                if (request.ContentType.IsTextContent())
                {
                    using (var sr = new StreamReader(request.Body))
                    {
                        body = await sr.ReadToEndAsync();
                    }
                }
                else
                {
                    body = await Base64StreamReader.ReadStreamAsBase64String(request.Body);
                }
            }

            int id = Interlocked.Increment(ref LastRequestId);

            return(new RequestData
            {
                Id = id,
                Method = request.Method,
                Uri = request.GetDisplayUrl(),
                Headers = headers,
                Body = body,
                ContentType = request.ContentType
            });
        }
Example #2
0
        public static async Task <ResponseData> FromResponse(HttpResponseMessage response)
        {
            var headers = new List <HeaderData>();

            foreach (var header in response.Headers)
            {
                foreach (var value in header.Value)
                {
                    headers.Add(new HeaderData
                    {
                        Name  = header.Key,
                        Value = value
                    });
                }
            }

            string body = null;

            if (response.Content != null)
            {
                if (response.Content.Headers.ContentType.IsTextContent())
                {
                    body = await response.Content.ReadAsStringAsync();
                }
                else
                {
                    var responseStream = await response.Content.ReadAsStreamAsync();

                    body = await Base64StreamReader.ReadStreamAsBase64String(responseStream);
                }
            }

            return(new ResponseData
            {
                StatusCode = (int)response.StatusCode,
                Headers = headers,
                Body = body,
                ContentType = response.Content.Headers.ContentType?.ToString()
            });
        }