Domanda

Sto cercando di comporre un messaggio SOAP (intestazione compresa) in C # .NET per inviare a un URL utilizzando HTTP post. L'URL voglio inviarlo a non è un web-service, solo riceve messaggi SOAP per eventualmente estrarre informazioni da esso. Tutte le idee su come fare questo?

È stato utile?

Soluzione

Per prima cosa è necessario creare un XML valido. Io uso LINQ to XML per raggiungere questo obiettivo, come segue:

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")
                       )
                   )
                );

Poi mando utilizzando ( Modifica :. XDocument.ToString() aggiunto quaggiù)

            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();
            }

Se devo leggere qualche risposta, lo faccio (questo è followup del codice di cui sopra):

            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
                    }
                }
            }

Altri suggerimenti

Il tuo codice di cui sopra mancava una parentesi e aveva una virgola in più, ho fissato qui:

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") 
            ) 
        ) 
    )
); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top