通过c#代码创建wsa soap

时间:2013-11-11 10:18:04

标签: c# soap

我需要在我的c#codebase中使用第三方服务,该服务要求将节点添加到请求中。 (跟进问题ws-addressing in soap request programatically

到目前为止我能够实现的是

    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Header xmlns:soap="http://www.w3.org/2005/08/addressing">
      <wsa:Action xmlns:wsa="Action">http://tempuri.org/GetDetails</wsa:Action>
      <wsa:To xmlns:wsa="To">SAMPLEURL.svc/DP2Svcs11</wsa:To>
   </soap:Header>
   <soap:Body>
      <GetBookingDetails xmlns="http://tempuri.org/">
         <UserName>testt</UserName>
         <Password>testt</Password>

      </GetBookingDetails>
   </soap:Body>
</soap:Envelope>

我最终需要点击该服务

 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
      <wsa:Action>http://tempuri.org/GetDetails</wsa:Action>
      <wsa:To>http://SAMPLEURL.svc/DP2Svcs11</wsa:To>
   </soap:Header>

   <soap:Body>
      <GetBookingDetails xmlns="http://tempuri.org/">
         <UserName>testt</UserName>
         <Password>test</Password>
      </GetBookingDetails>
   </soap:Body>
</soap:Envelope>

有人可以帮助如何更改提到的名称空间吗?

用于生成上述响应的代码是

XmlDocument xmlDoc = new XmlDocument();
        inwardStream.Position = 0;
        var streamReader = new StreamReader(inwardStream);
        var streamWriter = new StreamWriter(outwardStream);
        message = streamReader.ReadToEnd();

        xmlDoc.LoadXml(message);
        var insertNode = (((xmlDoc).LastChild));
        var headerNode = xmlDoc.CreateElement("soap", "Header", "http://www.w3.org/2005/08/addressing");

        //var actionNode = xmlDoc.CreateElement("wsa", "Action", "http://www.w3.org/2004/12/addressing");
        //var actionNodeTo = xmlDoc.CreateElement("wsa", "To", "http://www.w3.org/2004/12/addressing");
        var actionNode = xmlDoc.CreateElement("wsa:Action", "Action");
        var actionNodeTo = xmlDoc.CreateElement("wsa:To", "To");

        actionNode.InnerText = "http://tempuri.org/IFlightBookingService/GetBookingDetails";
        actionNodeTo.InnerText = "http://54.251.105.26/GQWCF_FlightEngine/FlightBookingService.svc/DP2Svcs11";

        headerNode.AppendChild(actionNode);
        headerNode.AppendChild(actionNodeTo);
        (((((xmlDoc))).LastChild)).InsertBefore(headerNode, (((((xmlDoc))).LastChild)).FirstChild);

1 个答案:

答案 0 :(得分:0)

修改专栏:

var actionNode = xmlDoc.CreateElement("wsa:Action", "Action");
        var actionNodeTo = xmlDoc.CreateElement("wsa:To", "To");

为:

var actionNode = xmlDoc.CreateElement("wsa:Action");
        var actionNodeTo = xmlDoc.CreateElement("wsa:To");