【问题标题】:How to edit SOAP requst body如何编辑 SOAP 请求正文
【发布时间】:2023-04-04 11:20:01
【问题描述】:

我使用 Visual Studio 添加了一个 Web 服务并编写了以下代码:

ReceiverClient rc = new ReceiverClient();
            
            
var task = rc.sendDocumentAsync("arg1", "arg2", "arg3");
            
            
var resutl = task.Result;

所以它创建了请求:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
  <s:Header>
    <a:Action>sendDocument</a:Action>
    <transportHeader xmlns="http://example.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <authInfo>
        <clientEntityId>clientId</clientEntityId>
      </authInfo>
    </transportHeader>
    <a:MessageID>messageId</a:MessageID>
    <a:ReplyTo>
      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
    </a:ReplyTo>
    <a:To></a:To>
    <wsse:Security>
    </wsse:Security>
  </s:Header>
  <s:Body xmlns:d2p1="http://docs.oasis-open.org/wss/2004/02/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" d2p1:Id="BodyID-6b6c35aa-a174-4b47-a1bf-bafa61efed78">
    <sendDocument xmlns="http://example.com">
      <oid xmlns="">oid</oid>
      <service xmlns="">service</service>
      <document xmlns="">document</document>
    </sendDocument>
  </s:Body>
</s:Envelope>

我需要用真实ID替换clientId,有没有办法以某种方式编辑请求正文?

【问题讨论】:

    标签: c# .net soap wsdl


    【解决方案1】:

    使用 Xml Linq:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.IO;
    
    namespace ConsoleApplication193
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                string xml = File.ReadAllText(FILENAME);
                XDocument doc = XDocument.Parse(xml);
    
                XElement id = doc.Descendants().Where(x => x.Name.LocalName == "clientEntityId").FirstOrDefault();
                id.SetValue("123");
      
            }
        }
    
    }
    

    【讨论】:

    • 是的,但是这个 xml 文件没有存储在我的电脑里 :(
    • XDocument 可以使用 URI 而不是文件名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 2019-07-07
    • 2018-11-26
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    相关资源
    最近更新 更多