Beispiel #1
0
        internal SDataResponse(WebResponse response, string redirectLocation)
        {
            var httpResponse = response as HttpWebResponse;

            _statusCode = httpResponse != null ? httpResponse.StatusCode : 0;

            MediaType contentType;

            if (MediaTypeNames.TryGetMediaType(response.ContentType, out contentType))
            {
                _contentType = contentType;
            }

            _eTag     = response.Headers[HttpResponseHeader.ETag];
            _location = response.Headers[HttpResponseHeader.Location] ?? redirectLocation;
            _files    = new List <AttachedFile>();

            if (_statusCode != HttpStatusCode.NoContent && _contentType != null)
            {
                using (var responseStream = response.GetResponseStream())
                {
                    string boundary;

                    if (_contentType == MediaType.Multipart && TryGetMultipartBoundary(response.ContentType, out boundary))
                    {
                        var multipart = MimeMessage.Parse(responseStream, boundary);
                        var isFirst   = true;

                        foreach (var part in multipart)
                        {
                            if (isFirst)
                            {
                                _contentType = MediaTypeNames.GetMediaType(part.ContentType);
                                _content     = LoadContent(part.Content, _contentType.Value);
                                isFirst      = false;
                            }
                            else
                            {
                                var fileName = part.ContentDisposition != null ? part.ContentDisposition.FileName : null;
                                _files.Add(new AttachedFile(part.ContentType, fileName, part.Content));
                            }
                        }
                    }
                    else
                    {
                        _content = LoadContent(responseStream, _contentType.Value);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="innerException"></param>
        public SDataException(WebException innerException)
            : base(innerException.Message, innerException, innerException.Status, innerException.Response)
        {
            if (Response == null)
            {
                return;
            }

            var httpResponse = Response as HttpWebResponse;

            _statusCode = httpResponse != null ? httpResponse.StatusCode : (HttpStatusCode?)null;
            MediaType mediaType;

            if (MediaTypeNames.TryGetMediaType(Response.ContentType, out mediaType) && mediaType == MediaType.Xml)
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var responseStream = Response.GetResponseStream())
                    {
                        responseStream.CopyTo(memoryStream);
                    }

                    memoryStream.Seek(0, SeekOrigin.Begin);
                    _diagnoses = memoryStream.DeserializeXml <Diagnoses>();

                    if (_diagnoses == null)
                    {
                        memoryStream.Seek(0, SeekOrigin.Begin);
                        var diagnosis = memoryStream.DeserializeXml <Diagnosis>();

                        if (diagnosis != null)
                        {
                            _diagnoses = new Collection <Diagnosis> {
                                diagnosis
                            };
                        }
                    }
                }
            }
        }