Pregunta

Estoy tratando de componer un mensaje SOAP (incluyendo la cabecera) en C # .NET para enviar a una dirección URL mediante HTTP POST. La URL Quiero enviarlo a no es un servicio web, que sólo recibe mensajes SOAP para extraer información de ella con el tiempo. Cualquier ideas sobre cómo hacer esto?

¿Fue útil?

Solución

En primer lugar es necesario crear un XML válido. Yo uso de LINQ to XML para lograr esto, como sigue:

XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
var document = new XDocument(
               new XDeclaration("1.0", "utf-8", String.Empty),
               new XElement(soapenv + "Envelope",
                   new XAttribute(XNamespace.Xmlns + "soapenv", soapenv),
                   new XElement(soapenv + "Header",
                       new XElement(soapenv + "AnyOptionalHeader",
                           new XAttribute("AnyOptionalAttribute", "false"),
                       )
                   ),
                   new XElement(soapenv + "Body",
                       new XElement(soapenv + "MyMethodName",
                            new XAttribute("AnyAttributeOrElement", "Whatever")
                       )
                   )
                );

A continuación, se lo envío usando ( editar . XDocument.ToString() añadido aquí abajo)

            var req = WebRequest.Create(uri);
            req.Timeout = 300000;  //timeout
            req.Method = "POST";
            req.ContentType = "text/xml;charset=UTF-8";

            using (var writer = new StreamWriter(req.GetRequestStream()))
            {
                writer.WriteLine(document.ToString());
                writer.Close();
            }

Si tengo que leer algún tipo de respuesta, yo (esto se followup del código anterior):

            using (var rsp = req.GetResponse())
            {
                req.GetRequestStream().Close();
                if (rsp != null)
                {
                    using (var answerReader = 
                                new StreamReader(rsp.GetResponseStream()))
                    {
                        var readString = answerReader.ReadToEnd();
                        //do whatever you want with it
                    }
                }
            }

Otros consejos

Su código de seguridad le faltaba un paréntesis y tenía una coma adicional, lo fijé aquí:

XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/"; 
var document = new XDocument( 
    new XDeclaration("1.0", "utf-8", String.Empty), 
    new XElement(soapenv + "Envelope", 
        new XAttribute(XNamespace.Xmlns + "soapenv", soapenv), 
        new XElement(soapenv + "Header", 
            new XElement(soapenv + "AnyOptionalHeader", 
                new XAttribute("AnyOptionalAttribute", "false")
            ) 
        ), 
        new XElement(soapenv + "Body", 
            new XElement(soapenv + "MyMethodName", 
                new XAttribute("AnyAttributeOrElement", "Whatever") 
            ) 
        ) 
    )
); 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top