繁体   English   中英

如何在XML soap消息中发送“Raw XML”; C#,. Net Web Service

[英]How to send “Raw XML” inside an XML soap message; C#, .Net Web Service

(我使用.Net 2.0 Web服务而不是WCF,因为我的需求很简单,我想要完全的Mono兼容性......)

好的,我是C#/ Visual Studio新手,所以我为基本问题道歉。

我正在与之通信的SOAP服务提供了一个WSDL,我用它来创建.Net Web服务引用。 到现在为止还挺好。

但是,服务期望有效负载是XML字符串,就像这样(例子......)

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <Search xmlns="http://foo.bar/bazservice/">
      <session>ABCDEF0123456789</session>
      <options>

<![CDATA[<Search>
  <SearchConditions>
    <Condition>
      <Field>1234</Field>
      <Value>Test Record</Value>
    </Condition>
  </SearchConditions>
  <Fields>
    <Field>5678</Field>
  </Fields>
</Search>]]>

      </options>
    </Search>
  </soap:Body>
</soap:Envelope>

所以我使用生成的代码调用我的函数,visual studio对我来说非常好,对我来说很像:

  // Build an XML thing, and encapsulate it in a CDATA
  XElement searchQuery = this.BuildSearchXml(params, search);
  XCData cdata = new XCData(searchQuery.ToString());

  // XML String looks good...
  Console.WriteLine(cdata.ToString() + Environment.NewLine);

  // Let's send XML payload to service...
  search client = this.Connect("search");

  // Search() expects the second argument to be an XML string...
  var returnVal = client.Search(session, cdata.ToString(), 1);

  return returnVal.ToString();

不幸的是,Web服务粘合代码,在生成的魔法中的某个地方,自动对XML字符串进行urlen编码!

在做一些tcpdump调查时,这是实际发送的有效负载:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <Search xmlns="http://foo.bar/bazservice/">
      <session>ABCDEF0123456789</session>
      <options>

&lt;![CDATA[&lt;Search&gt;
  &lt;SearchConditions&gt;
    &lt;Condition&gt;
      &lt;Field&gt;1234&lt;/Field&gt;
      &lt;Value&gt;Test Record&lt;/Value&gt;
    &lt;/Condition&gt;
  &lt;/SearchConditions&gt;
  &lt;Fields&gt;
    &lt;Field&gt;5678&lt;/Field&gt;
  &lt;/Fields&gt;
&lt;/Search&gt;]]&gt;

      </options>
    </Search>
  </soap:Body>
</soap:Envelope>

我的问题是:如何继续使用Web引用魔法,因为它是使用WSDL生成的,但是告诉它不要 URLEncode我的原始XML字符串,而是按原样发送它?!

我是新手 - 我正在查看生成的代码(在。\\ Web References \\ MyService \\ Reference.cs中),但我不知所措。

在此先感谢任何帮助,指出我正确的方向!

...

编辑:为Reference.cs添加了代码

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

//
// This source code was auto-generated by Microsoft.VSDesigner, Version 4.0.30319.42000.
//
#pragma warning disable 1591

namespace MyService.Search {
    using System;
    using System.Web.Services;
    using System.Diagnostics;
    using System.Web.Services.Protocols;
    using System.Xml.Serialization;
    using System.ComponentModel;

    // ..... snip other stuff ..... //

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.6.1055.0")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Web.Services.WebServiceBindingAttribute(Name="searchSoap", Namespace="http://foo.bar/bazservice")]
    public partial class search : System.Web.Services.Protocols.SoapHttpClientProtocol {

        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://foo.bar/bazservice/Search", RequestNamespace="http://foo.bar/bazservice", ResponseNamespace="http://foo.bar/bazservice", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public string ExecuteSearch(string sessionToken, string searchOptions, int pageNumber) {
            object[] results = this.Invoke("Search", new object[] {
                        sessionToken,
                        searchOptions,
                        pageNumber});
            return ((string)(results[0]));
        }

        public void ExecuteSearchAsync(string sessionToken, string searchOptions, int pageNumber) {
            this.ExecuteSearchAsync(sessionToken, searchOptions, pageNumber, null);
        }

        // ..... snip other methods ..... //
    }
}

#pragma warning restore 1591

这不是URL编码。 这是XML编码(实体引用)。 它通常在功能上等同于CDATA块。 不要担心格式化 - 它完全按照它应该做的。 只需确保服务的行为正常。

https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM