public void ReadXml(XPathNavigator node, XmlResolver resolver) { if (node.NodeType == XPathNodeType.Element) { if (node.MoveToFirstAttribute()) { do { switch (node.LocalName) { case "media-type": this.MediaType = node.Value; break; case "boundary": this.Boundary = node.Value; break; } } while (node.MoveToNextAttribute()); node.MoveToParent(); } if (node.MoveToChild(XPathNodeType.Element)) { XPathHttpMultipartItem currentItem = null; do { if (node.NamespaceURI == XPathHttpClient.Namespace) { switch (node.LocalName) { case "header": if (currentItem == null) { currentItem = new XPathHttpMultipartItem(); } currentItem.Headers.Add(node.GetAttribute("name", ""), node.GetAttribute("value", "")); break; case "body": if (currentItem == null) { currentItem = new XPathHttpMultipartItem(); } currentItem.Body = new XPathHttpBody(); currentItem.Body.ReadXml(node, resolver); this.Items.Add(currentItem); currentItem = null; break; } } } while (node.MoveToNext(XPathNodeType.Element)); node.MoveToParent(); } } }
void LoadContent(HttpWebRequest httpRequest, XPathHttpMultipart multipart) { string boundary = multipart.Boundary; if (String.IsNullOrEmpty(boundary)) { boundary = "----------" + DateTime.Now.Ticks.ToStringInvariant(); } Encoding encoding = Encoding.UTF8; string newLine = Environment.NewLine; byte[] newLineBytes = encoding.GetBytes(newLine); var headers = new List <byte[]>(); byte[] footer = encoding.GetBytes(String.Concat("--", boundary, "--", newLine)); long contentLength = 0; for (int i = 0; i < multipart.Items.Count; i++) { XPathHttpMultipartItem item = multipart.Items[i]; var sb = new StringBuilder() .Append("--") .Append(boundary) .Append(newLine); foreach (string key in item.Headers) { sb.Append(key) .Append(": ") .Append(item.Headers[key]) .Append(newLine); } sb.Append(newLine); byte[] header = encoding.GetBytes(sb.ToString()); headers.Add(header); contentLength += header.LongLength; contentLength += item.Body.PrepareContent(this.ItemFactory, this.Resolver); contentLength += newLineBytes.LongLength; } contentLength += footer.LongLength; httpRequest.ContentLength = contentLength; httpRequest.ContentType = String.Concat(multipart.MediaType, "; boundary=", boundary); using (Stream requestStream = httpRequest.GetRequestStream()) { for (int i = 0; i < multipart.Items.Count; i++) { byte[] header = headers[i]; requestStream.Write(header, 0, header.Length); using (Stream contentStream = multipart.Items[i].Body.GetContentStream()) { contentStream.CopyTo(requestStream); requestStream.Write(newLineBytes, 0, newLineBytes.Length); } } requestStream.Write(footer, 0, footer.Length); } }
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()); }
void EnsureValidForHttpRequest() { if (this.Href == null) { throw new InvalidOperationException("Href cannot be null."); } if (String.IsNullOrEmpty(this.Method)) { throw new InvalidOperationException("Method cannot be null or empty."); } if (this.Multipart != null && this.Body != null) { throw new InvalidOperationException("Multipart and Body properties are mutually exclusive."); } if (!String.IsNullOrEmpty(this.Username)) { if (String.IsNullOrEmpty(this.Password)) { throw new InvalidOperationException("Password cannot be null or empty if Username isn't."); } if (String.IsNullOrEmpty(this.AuthMethod)) { throw new InvalidOperationException("AuthMethod cannot be null or empty if Username isn't."); } } else if (this.SendAuthorization) { throw new InvalidOperationException("SendAuthorization cannot be true if Username is null or empty."); } if (this.Body != null) { if (String.IsNullOrEmpty(this.Body.MediaType)) { throw new InvalidOperationException("Body.MediaType cannot be null or empty."); } } else if (this.Multipart != null) { if (this.Multipart.Items.Count == 0) { throw new InvalidOperationException("A multipart request must have at least one part."); } if (String.IsNullOrEmpty(this.Multipart.MediaType)) { throw new InvalidOperationException("Multipart.MediaType cannot be null or empty."); } if (!MediaTypes.IsMultipart(this.Multipart.MediaType)) { throw new InvalidOperationException("Multipart.MediaType must be set to a multipart media type."); } for (int i = 0; i < this.Multipart.Items.Count; i++) { XPathHttpMultipartItem item = this.Multipart.Items[i]; if (item.Body == null) { throw new InvalidOperationException("Multipart.Items[" + i + "].Body cannot be null."); } if (String.IsNullOrEmpty(item.Body.MediaType)) { throw new InvalidOperationException("Multipart.Items[" + i + "].MediaType cannot be null or empty."); } if (item.Body.Content == null && item.Body.Src == null) { throw new InvalidOperationException("All multipart bodies must have content."); } } } }