C#中採用HTTP方式發送並處理SOAP格式的XML資料

來源:互聯網
上載者:User

在C#用開發網路應用的時候,我們經常會遇到需要將一個SOAP格式的訊息(包括Header)使用HTTP的POST方式發送到某個指定的URL這類問題。這裡總結一下這一技術的的實現方法:
首先,我們需要建立一個有效XML檔案,可以使用LINQ to XML來實現,如下:

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")
                       )
                   )
                );
然後,就可以使用它了:
            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();
            }
如果,我們還要讀取對方伺服器返回的訊息,那麼可以這樣:
            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
                    }
                }
            }

對於如下一段SOAP格式的XML資料,又可以怎麼處理呢?
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <env:Header>
  <NotifySOAPHeader xmlns="http://www.csapi.org/schema/parlayx/common/v2_0">
  <ServiceID>3001400002</ServiceID>
  <SPrevID>fanxiang123</SPrevID>
  <SPrevPassword>12345678</SPrevPassword>
  <TransactionID>01022016400400000025</TransactionID>
  </NotifySOAPHeader>
  </env:Header>
  <env:Body>
  <notifySmsReception xmlns="http://www.csapi.org/schema/parlayx/sms/notification/v2_0/local">
  <registrationIdentifier/>
  <message>
  <message>aaaa</message>
  <senderAddress>tel:+861067453262</senderAddress>
  <smsServiceActivationNumber>tel:10661112</smsServiceActivationNumber>
  </message>
  </notifySmsReception>
  </env:Body>
</env:Envelope>

其實很簡單:
XmlDocument xdoc = new XmlDocument();
xdoc.Load("Data.xml");

string s = xdoc.DocumentElement["env:Header"]["NotifySOAPHeader"].InnerXml;
Console.WriteLine(s);

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.