Пример #1
0
        XPathHttpResponse GetResponse(HttpWebResponse httpResponse)
        {
            var response = new XPathHttpResponse {
                Status  = httpResponse.StatusCode,
                Message = httpResponse.StatusDescription,
                Headers = { httpResponse.Headers }
            };

            if (httpResponse.ContentLength > 0)
            {
                string mediaType = "";

                try {
                    var contentType = new ContentType(httpResponse.ContentType);
                    mediaType = contentType.MediaType;
                } catch (FormatException) {
                } catch (ArgumentException) {
                }

                if (MediaTypes.IsMultipart(mediaType))
                {
                    var multipart = new XPathHttpMultipart {
                        MediaType = mediaType
                    };

                    using (Stream responseBody = httpResponse.GetResponseStream()) {
                        multipart.Deserialize(responseBody, this.StatusOnly);
                    }

                    response.Multipart = multipart;
                }
                else
                {
                    var body = new XPathHttpBody {
                        MediaType = mediaType
                    };

                    if (!String.IsNullOrEmpty(httpResponse.CharacterSet))
                    {
                        try {
                            body.Encoding = Encoding.GetEncoding(httpResponse.CharacterSet);
                        } catch (ArgumentException) { }
                    }

                    if (!this.StatusOnly)
                    {
                        using (Stream responseBody = httpResponse.GetResponseStream())
                            body.Deserialize(responseBody, httpResponse.ResponseUri, this.ItemFactory, this.OverrideMediaType);
                    }

                    response.Body = body;
                }
            }

            return(response);
        }
Пример #2
0
        XPathHttpResponse GetResponse(HttpWebResponse httpResponse)
        {
            var response = new XPathHttpResponse {
            Status = httpResponse.StatusCode,
            Message = httpResponse.StatusDescription,
            Headers = { httpResponse.Headers }
             };

             if (httpResponse.ContentLength > 0) {

            string mediaType = "";

            try {
               var contentType = new ContentType(httpResponse.ContentType);
               mediaType = contentType.MediaType;

            } catch (FormatException) {
            } catch (ArgumentException) {
            }

            if (MediaTypes.IsMultipart(mediaType)) {

               var multipart = new XPathHttpMultipart {
                  MediaType = mediaType
               };

               using (Stream responseBody = httpResponse.GetResponseStream()) {
                  multipart.Deserialize(responseBody, this.StatusOnly);
               }

               response.Multipart = multipart;

            } else {

               var body = new XPathHttpBody {
                  MediaType = mediaType
               };

               if (!String.IsNullOrEmpty(httpResponse.CharacterSet)) {
                  try {
                     body.Encoding = Encoding.GetEncoding(httpResponse.CharacterSet);
                  } catch (ArgumentException) { }
               }

               if (!this.StatusOnly) {

                  using (Stream responseBody = httpResponse.GetResponseStream())
                     body.Deserialize(responseBody, httpResponse.ResponseUri, this.ItemFactory, this.OverrideMediaType);
               }

               response.Body = body;
            }
             }

             return response;
        }
Пример #3
0
        public XPathItem[] SendRequest(XPathNavigator request, string href, IEnumerable <XPathItem> bodies)
        {
            XPathItemFactory itemFactory = this.ItemFactory;
            XmlResolver      resolver    = this.Resolver;

            if (itemFactory == null)
            {
                throw new InvalidOperationException("ItemFactory cannot be null.");
            }

            int bodiesLength = (bodies != null) ?
                               bodies.Count()
            : 0;

            XPathHttpRequest xpathRequest;

            if (request == null)
            {
                if (String.IsNullOrEmpty(href))
                {
                    throw new ArgumentException("href cannot be null or empty if request is null.", "href");
                }

                xpathRequest = new XPathHttpRequest {
                    Method      = WebRequestMethods.Http.Get,
                    Href        = new Uri(href, UriKind.Absolute),
                    Resolver    = resolver,
                    ItemFactory = itemFactory
                };

                if (bodiesLength > 0)
                {
                    throw new ArgumentException("Cannot use the bodies parameter when request is null.", "bodies");
                }
            }
            else
            {
                xpathRequest = new XPathHttpRequest {
                    Resolver    = resolver,
                    ItemFactory = itemFactory
                };
                xpathRequest.ReadXml(request);

                if (String.IsNullOrEmpty(href))
                {
                    if (xpathRequest.Href == null)
                    {
                        throw new ArgumentException("href cannot be null or empty if request.Href is null.", "href");
                    }
                }
                else
                {
                    xpathRequest.Href = new Uri(href);
                }

                if (xpathRequest.Body != null)
                {
                    if (bodiesLength > 0)
                    {
                        if (bodiesLength > 1)
                        {
                            throw new ArgumentException("bodies must have a single item when request.Body is not null.", "bodies");
                        }

                        xpathRequest.Body.Content = bodies.Single();
                    }
                }
                else if (xpathRequest.Multipart != null)
                {
                    if (bodiesLength > 0)
                    {
                        if (bodiesLength != xpathRequest.Multipart.Items.Count)
                        {
                            throw new ArgumentException("The number of items in bodies must match the multipart request bodies.", "bodies");
                        }

                        for (int i = 0; i < xpathRequest.Multipart.Items.Count; i++)
                        {
                            XPathHttpMultipartItem item = xpathRequest.Multipart.Items[i];

                            if (item.Body != null)
                            {
                                item.Body.Content = bodies.Skip(i).First();
                            }
                        }
                    }
                }
                else if (bodiesLength > 0)
                {
                    throw new ArgumentException("If bodies is not empty request.Body or request.Multipart cannot be null.", "request");
                }
            }

            XPathHttpResponse xpathResponse = xpathRequest.GetResponse();
            XPathNavigator    responseEl    = itemFactory.CreateElement(xpathResponse);

            var result = new List <XPathItem>();

            result.Add(responseEl);

            if (xpathResponse.Body != null)
            {
                if (xpathResponse.Body.Content != null)
                {
                    result.Add(xpathResponse.Body.Content);
                }
            }
            else if (xpathResponse.Multipart != null)
            {
                foreach (XPathHttpMultipartItem item in xpathResponse.Multipart.Items)
                {
                    if (item.Body.Content != null)
                    {
                        result.Add(item.Body.Content);
                    }
                }
            }

            return(result.ToArray());
        }