public T Execute <T>(String methodName, Object[] parameters, ICollection <Attachment> attachments, ref HashSet <Part> responseParts) { foreach (Attachment attachment in attachments) { if (!attachment.File.Exists) { throw new XmlRpcException("The attachment '" + attachment.Name + "' does not exist"); } } MethodRequest request = new MethodRequest(GetParameters(parameters)); request.MethodName = methodName; XmlRpcResponse response = Request(request, attachments); T objectToReturn = Deserialize <T>(response.getXmlDocumentResponse()); responseParts = response.GetResponseParts(); //objectToReturn.ResponseParts = response.GetResponseParts(); return(objectToReturn); }
private static void SendXmlDocumentPart(MethodRequest requestDoc, Stream @out) { String newBoundary = "--" + boundary + "\r\n"; String contentDisposition = "Content-Disposition: form-data; name=\"xmlrpc\"; filename=\"xmlrpc.xml\"\r\n\r\n"; Write(newBoundary, @out); Write(contentDisposition, @out); XmlSerializerFactory factory = new XmlSerializerFactory(); XmlSerializer serializer = factory.CreateSerializer(requestDoc.GetType()); #if DEBUG MemoryStream memoryStream = new MemoryStream(); XmlTextWriter xmlTextWriter2 = new XmlTextWriter(memoryStream, Encoding.UTF8); serializer.Serialize(xmlTextWriter2, requestDoc); String xml = Encoding.Default.GetString(memoryStream.ToArray()); Debug.WriteLine(xml); #endif XmlTextWriter xmlTextWriter = new XmlTextWriter(@out, Encoding.Default); serializer.Serialize(xmlTextWriter, requestDoc); //Write(requestDoc.OuterXml,@out); }
private XmlRpcResponse Request(MethodRequest requestDoc, ICollection <Attachment> attachments) { Stream @out = null; try { String url = this.config.ServerUri.ToString(); if (!url.EndsWith(SUFIX_GATEWAY)) { if (!url.EndsWith("/")) { url += "/"; } url += SUFIX_GATEWAY; } HttpWebRequest connection = (HttpWebRequest)HttpWebRequest.Create(url); if (config.UsesProxy) { connection.Proxy = new WebProxy(config.ProxyServer.Host, config.ProxyPort); } if (config.HasUserName) { connection.Credentials = new NetworkCredential(config.UserName, config.Password); } connection.Method = "POST"; @out = SendHeaders(connection); SendXmlDocumentPart(requestDoc, @out); foreach (Attachment attachment in attachments) { FileInfo file = attachment.File; String name = attachment.Name; SendFile(file, name, @out); } WriteEnd(@out); @out.Close(); try { WebResponse webResponse = connection.GetResponse(); if (connection.HaveResponse) { HttpWebResponse response = (HttpWebResponse)webResponse; String contentType = response.ContentType; Stream @in = response.GetResponseStream(); switch (response.StatusCode) { case HttpStatusCode.OK: return(GetResponse(@in, contentType)); default: throw new HttpException(response.StatusDescription, response.StatusCode, GetDetail(@in, response.CharacterSet)); } } else { throw new HttpException("Is not possible to get a response from the uri " + connection.RequestUri, HttpStatusCode.NotFound, ""); } } catch (WebException e) { HttpWebResponse webResponse = (HttpWebResponse)e.Response; Stream @in = webResponse.GetResponseStream(); throw new HttpException(e.Message, webResponse.StatusCode, GetDetail(@in, webResponse.CharacterSet)); } } catch (IOException ioe) { throw new XmlRpcException(ioe.Message, ioe); } finally { if (@out != null) { @out.Close(); } } }