SOAP object over HTTP post in C# .NET

前端 未结 2 1171
心在旅途
心在旅途 2021-02-08 01:56

I am trying to compose a SOAP message(including header) in C# .NET to send to a URL using HTTP post. The URL I want to send it to is not a web-service, it just receives SOAP mes

相关标签:
2条回答
  • 2021-02-08 02:20

    your code above was missing a parenthesis and had an extra comma, i fixed it here:

    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") 
                ) 
            ) 
        )
    ); 
    
    0 讨论(0)
  • 2021-02-08 02:28

    First you need to create a valid XML. I use Linq to XML to achieve this, like follow:

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

    Then I send it using (EDIT: added XDocument.ToString() down here.)

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

    If I have to read some response, I do (this is followup of the above code):

                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
                        }
                    }
                }
    
    0 讨论(0)
提交回复
热议问题