C# 中的 SOAP 请求和响应

本文关键字:求和 响应 请求 SOAP 中的 | 更新日期: 2023-09-27 18:32:53

我为Soap XML请求编写了c#代码,我已经验证了生成XML类的代码。

我的问题是如何使用 c# 代码发送请求和接收响应。

请善待我的简单或基本错误,因为我是 XML 的新手,但您的帮助将不胜感激。

SOAP XML 请求:

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ce="http://www." xmlns:os="http://www.domainname.com/schema/soap/v1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <soapenv:Header /> 
    <soapenv:Body>
        <ce:message>
            <ce:m_control>
                <os:control_timestamp>2001-12-31T12:00:00</os:control_timestamp> 
                <os:message_id>000000000000000000000000000000000</os:message_id> 
                <os:message_type>Contract Enquiry Request</os:message_type> 
                <os:message_version>ce/v2.2/NameContractRequest</os:message_version> 
                <os:expected_response_type>synchronous</os:expected_response_type> 
                <os:initiator_id>initiator_id</os:initiator_id> 
                <os:initiator_orchestration_id>initiator_orchestration_id</os:initiator_orchestration_id> 
                <os:KeyInfo>
                    <ds:X509Data>
                        <ds:X509IssuerSerial>
                            <ds:X509IssuerName>CN=OSIS Customer CA, O=Origo Secure Internet Services Ltd., CN=OSIS Customer CA</ds:X509IssuerName> 
                            <ds:X509SerialNumber>111111111111111111111111111111111111</ds:X509SerialNumber> 
                        </ds:X509IssuerSerial>
                        <ds:X509SubjectName>C=GB, O=FirmID3400010000023NR11QQ, OU=CPS - www.unipass.co.uk/cps, OU=Warning/Terms of Use - www.unipass.co.uk/tou, OU=EmployeeID10101101010101, OU=TPSP2, OU=BPNR1 1QQ, CN=Testt Orgg/emailAddress=Fname.Lname@aviva.co.uk</ds:X509SubjectName> 
                    </ds:X509Data>
                </os:KeyInfo>
                <os:responder_id>os:responder_id</os:responder_id> 
            </ce:m_control>
            <ce:m_content>
                <ce:b_control>
                    <ce:contract_enquiry_reference>TestRequest</ce:contract_enquiry_reference> 
                </ce:b_control>
                <ce:intermediary>
                    <ce:FirmFSARef id="idvalue14">456123</ce:FirmFSARef> 
                </ce:intermediary>
                <ce:request_scope>
                    <ce:contract_details_required_ind>No</ce:contract_details_required_ind> 
                    <ce:valuation_currency>GBP</ce:valuation_currency> 
                    <ce:fund_code_type_required>SEDOL</ce:fund_code_type_required> 
                    <ce:valuation_request ce:type="Current" /> 
                </ce:request_scope>
                <ce:contract>
                    <ce:contract_reference_number>TL12345678</ce:contract_reference_number> 
                </ce:contract>
            </ce:m_content>
        </ce:message>
    </soapenv:Body>
</soapenv:Envelope>
####

Guid CEGuid = Guid.NewGuid((; string GuidString = CEGuid.ToString((;

        string CEVersion = "";
        string URL = "";
        string ResponderId = "";
        string ContractDetailsRequired = "Yes";
        using (XmlTextWriter xmlRequestWriter = new XmlTextWriter(@"C:/Unipass/PensionRequest.xml", Encoding.UTF8))
        {
            xmlRequestWriter.WriteStartDocument();
            xmlRequestWriter.WriteComment("This file is generated by the program.");
            xmlRequestWriter.WriteStartElement("soapenv:Envelope");
            xmlRequestWriter.WriteAttributeString("xmlns:soapenv", null, "http://schemas.xmlsoap.org/soap/envelope/");
            xmlRequestWriter.WriteAttributeString("xmlns:ce", null, "http://www.WhateveryDomain.com/schema/ce/v2.2/WhateverRequest");
            xmlRequestWriter.WriteAttributeString("xmlns:os", null, "http://www.WhateveryDomain.com/schema/soap/v1");
            xmlRequestWriter.WriteAttributeString("xmlns:ds", null, "http://www.w3.org/2000/09/xmldsig#");
            xmlRequestWriter.WriteStartElement("soapenv:Header");
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteStartElement("ce:message");
            xmlRequestWriter.WriteStartElement("ce:m_control");
            xmlRequestWriter.WriteElementString("os:control_timestamp", DateTime.Now.ToString("s"));
            xmlRequestWriter.WriteElementString("os:message_id", GuidString);
            xmlRequestWriter.WriteElementString("os:message_type", "Contract Enquiry Request");
            xmlRequestWriter.WriteElementString("os:message_version", "ce/v2.2/WhateverRequest");
            xmlRequestWriter.WriteElementString("os:expected_response_type", "synchronous");
            xmlRequestWriter.WriteElementString("os:initiator_id", "initiator_id");
            xmlRequestWriter.WriteElementString("os:initiator_orchestration_id", "initiator_orchestration_id");
            xmlRequestWriter.WriteStartElement("os:KeyInfo");
            xmlRequestWriter.WriteStartElement("ds:X509Data");
            xmlRequestWriter.WriteStartElement("ds:X509IssuerSerial");
            xmlRequestWriter.WriteElementString("ds:X509IssuerName", "CN=OSIS Customer CA, O=Origo Secure Internet Services Ltd., CN=OSIS Customer CA");
            xmlRequestWriter.WriteElementString("ds:X509SerialNumber", "111111111111111111111111111111111");
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteElementString("ds:X509SubjectName", "C=GB, O=FirmID3400010000023NR11QQ, OU=CPS - www.unipass.co.uk/cps, OU=Warning/Terms of Use - www.unipass.co.uk/tou, OU=EmployeeID01200012000003, OU=TPSP2, OU=BPNR1 1QQ, CN=Testt Orgg/emailAddress=fname.lastname@aviva.co.uk");
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteElementString("os:responder_id", "os:responder_id");
            xmlRequestWriter.WriteEndElement();

            xmlRequestWriter.WriteStartElement("ce:m_content");
            xmlRequestWriter.WriteStartElement("ce:b_control");
            xmlRequestWriter.WriteElementString("ce:contract_enquiry_reference", "TestRequest");
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteStartElement("ce:intermediary");
            xmlRequestWriter.WriteStartElement("ce:FirmFSARef");
            xmlRequestWriter.WriteElementString("id", "456123");//="idvalue14">
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteStartElement("ce:request_scope");
            xmlRequestWriter.WriteElementString("ce:contract_details_required_ind", "Yes");
            xmlRequestWriter.WriteElementString("ce:valuation_currency", "GBP");
            xmlRequestWriter.WriteElementString("ce:fund_code_type_required", "SEDOL");
            xmlRequestWriter.WriteStartElement("ce:valuation_request");
            xmlRequestWriter.WriteElementString("ce:type", "Current");//"ce:type","Current"
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteStartElement("ce:contract");
            xmlRequestWriter.WriteElementString("ce:contract_reference_number", "PP12345678");
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndElement(); 
            xmlRequestWriter.WriteEndElement();
            xmlRequestWriter.WriteEndDocument();
            xmlRequestWriter.Flush();

请告诉我如何使用上面的代码发送请求并接收来自WCF服务的响应。

即使我添加了 wsdl 引用,但我不知道如何在下面的行中传递 XmlElement[]。

serviceReference.getDetail(ref XmlElement[] Any(;

下面是引用类中的一个方法。

   [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(Namespace="http://www.domainname.com/whatever/webname/schema/ce/v2.2/NameContractServic" +
        "e", ConfigurationName="TestingPension.NameContractServiceDetailType")]
    public interface NameContractServiceDetailType {
        // CODEGEN: Generating message contract since the wrapper namespace (http://www.domainname.com/schema/ce/v2.2/NameContractRequest) of message getDetailRequest does not match the default value (http://www.domainname.com/name/name/schema/ce/v2.2/NameContractService)
        [System.ServiceModel.OperationContractAttribute(Action="http://www.origostandards.com/schema/ce/v2.2/CEPensionSingleContract#getDetail", ReplyAction="*")]
        [System.ServiceModel.FaultContractAttribute(typeof(SoapReqResWebApplication.TestingPension.Error[]), Action="http://www.domainname.com/schema/ce/v2.2/NameContract#getDetail", Name="errors", Namespace="http://www.domainname.com/schema/tech/v1.0/SOAPFaultDetail")]
        [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
        SoapReqResWebApplication.TestingPension.getDetailResponse getDetail(SoapReqResWebApplication.TestingPension.getDetailRequest request);
    }
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    [System.ServiceModel.MessageContractAttribute(WrapperName="message", WrapperNamespace="http://www.domainname.com/schema/ce/v2.2/NameContractRequest", IsWrapped=true)]
    public partial class getDetailRequest {
        [System.ServiceModel.MessageBodyMemberAttribute(Namespace="", Order=0)]
        [System.Xml.Serialization.XmlAnyElementAttribute()]
        public System.Xml.XmlElement[] Any;
        public getDetailRequest() {
        }
        public getDetailRequest(System.Xml.XmlElement[] Any) {
            this.Any = Any;
        }
    }

@CodeCaster我尝试了您的代码: var documentToSend = new XmlDocument((; 待办事项:添加您喜欢
的所有元素 using (XmlTextWriter xmlRequestWriter = new XmlTextWriter(@"C:/Unipass/Request.xml", Encoding.UTF8(( { xmlRequestWriter.WriteStartDocument((; xmlRequestWriter.WriteStartElement("ce:message"(; xmlRequestWriter.WriteEndElement((; xmlRequestWriter.WriteEndDocument((; xmlRequestWriter.Flush((; documentToSend.Save(xmlRequestWriter(; 创建一个将根消息节点作为唯一元素的数组。 var xmlToSend = new XmlElement[] { documentToSend.DocumentElement };xmlToSend = null
}可能是我做错了,你能告诉我为什么 xmlToSend 为空吗?

C# 中的 SOAP 请求和响应

您似乎正在手动编写代码以将消息序列化为 SOAP/XML。SOAP 请求示例是一个实际的实例文档,而不是 WSDL,是吗?这是 WCF 为您执行的操作之一 - 您不需要生成自己的 SOAP 消息。

在 WCF 中,您专注于要传输的对象类型和要支持的操作。然后,WCF 会为您完成 SOAP 序列化的所有繁重工作。这确实是WCF的重点。

很难

从示例 SOAP 消息中破译您到底要做什么,因此我建议查看一个简单的 WCF 示例之一,例如从此处开始

http://msdn.microsoft.com/en-us/library/vstudio/ms751514(v=vs.90(.aspx

使用 wsHttpBinding 或 basicHttpBinding 启动并运行一个简单的绑定(这些是 WCF 中的 SOAP 绑定(。然后使用 Fiddler 或类似方法捕获 HTTP 请求,并查看 WCF 在 SOAP 信封生成方面为您做了什么。

然后,您应该能够将其转换为您自己的案例。

鉴于生成的方法签名getDetailRequest(System.Xml.XmlElement[] Any),WCF 的代理生成器似乎无法从提供的 WSDL/XSD 创建类。像 SoapUI 这样的工具对 WSDL 有什么看法?

解决此问题的一种方法是手动生成消息,这只是<ce:message>..</ce:message>块。调用服务方法时,WCF 会将其包装在适当的 SOAP 信封中。

你必须在XmlDocument类中玩一玩,但我认为这样的事情可以做到:

// Create an XmlDocument and fill it
var documentToSend = new XmlDocument();
// TODO: add all elements you like
// Create an array with the root message node as only element.
var xmlToSend = new XmlElement[] { documentToSend.DocumentElement };
// Call the service.
var response = serviceReference.getDetail(xmlToSend);

另一种方法是在 C# 中手动重新创建请求类,并在发送请求时将其实例填充并序列化为 XML。最终的方法是修复 XML,以便添加服务引用 (SvcUtil( 可以从服务的元数据生成类。

至于您的编辑,您不是将 XML 元素写入documentToSend,而是将它们写入您的文件。