【问题标题】:Create a SOAP header?创建 SOAP 标头?
【发布时间】:2010-10-19 14:23:06
【问题描述】:

如何添加创建 SOAP Web 服务标头?

示例

<soap:Header>
    <myHeader xmlns="https://www.domain.com">
        <Username>string</Username>
        <Password>string</Password>
    </myHeader>
</soap:Header>

源代码

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;


namespace TestWebServices
{
    /// <summary>
    /// Summary description
    /// </summary>
    [WebService(Namespace = "https://Test.com")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Testing : System.Web.Services.WebService
    {

        [WebMethod]
        public string GetTestValue()
        {


            return "xyz";
        }
    }

}

【问题讨论】:

    标签: c# web-services api soap


    【解决方案1】:

    怎么样:

    public class MyHeader : SoapHeader
    {
        public string Username;
        public string Password;
    }
    

    这里有更多关于这个主题的内容:

    Using SOAP Headers (MSDN - archived)

    Defining and Processing SOAP Headers (MSDN)

    【讨论】:

      【解决方案2】:

      如果您需要精细控制soap 标头xml 的呈现方式(在与用java 编写的Web 服务交互时发生),您始终可以通过实现IXmlSerializable 覆盖所有呈现

      [XmlRoot("customHeader", Namespace = "http://somecompany.com/webservices/security/2012/topSecret")]
      public class customHeader: SoapHeader, IXmlSerializable
      {
          public customHeader()
          {
              Actor = "http://schemas.xmlsoap.org/soap/actor/next";
              MustUnderstand = false;
          }
      
          public System.Xml.Schema.XmlSchema GetSchema()
          {
              return null;
              //throw new NotImplementedException();
          }
      
          public void ReadXml(XmlReader reader)
          {
              //throw new NotImplementedException();
          }
      
          public void WriteXml(XmlWriter writer)
          {
              writer.WriteAttributeString("soap:actor", Actor);
              writer.WriteAttributeString("soap:mustUnderstand", MustUnderstand ? "1" : "0");
              writer.WriteRaw("some encrypted data");
              //get it exactly the way you want it in here without mucking with Xml* property attributes for hours on end
              //writer.WriteElement(...);
          }
      }
      

      【讨论】:

      • 有没有办法使用这种类型的代码向 SOAP Header 添加自定义元素?
      【解决方案3】:

      带有标头的 SOAP 消息可能如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      
        <soap:Header>
          <Username>string</Username>
          <Password>string</Password>
        </soap:Header>
      
      </soap:Envelope>
      

      【讨论】:

      • OP 已经发布了该 XML 消息。他们需要知道如何在 C# 中以编程方式执行此操作。
      【解决方案4】:
      EndpointAddressBuilder builder = new EndpointAddressBuilder(client.Endpoint.Address);
      AddressHeader header = AddressHeader.CreateAddressHeader("apiKey", "http://tempuri.org", "longapikeyhere");
      
      builder.Headers.Add(header);
      client.Endpoint.Address = builder.ToEndpointAddress();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-26
        • 1970-01-01
        • 1970-01-01
        • 2012-07-11
        相关资源
        最近更新 更多