定义和处理 SOAP 头

 

如何:定义和处理 SOAP 头

 

代码示例

使用 ASP.NET 创建的 Web 服务可以定义和处理 SOAP 头。定义 SOAP 头的过程是:定义表示特定 SOAP 头中数据的类,然后从 SoapHeader 类中派生该类。

定义表示 SOAP 头的类

  1. 创建一个从 SoapHeader 类派生的类,其名称与 SOAP 头的根元素匹配。

    public class MyHeader : SoapHeader
    

     

    Visual Basic
    Public Class MyHeader : Inherits SoapHeader
    
  2. 添加公共字段或属性,与 SOAP 头中每个元素的名称及其各自的数据类型匹配。

    例如,在给定以下 SOAP 头的情况下,其后的类定义一个表示 SOAP 头的类。

    <soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <MyHeader xmlns="http://www.contoso.com">
        <Created>dateTime</Expires>
        <Expires>long</Expires>
      </MyHeader>
    </soap:Header>
    
    public class MyHeader : SoapHeader 
    {
       public DateTime Created;
       public long Expires;
    }
    

     

    Visual Basic
    Public Class MyHeader : Inherits SoapHeader 
       Public Created As DateTime
       Public Expires As Long
    End Class
    

在 Web 服务中处理 SOAP 头

  1. 将公共成员添加到实现表示 SOAP 头的类型的 Web 服务的类。

    [WebService(Namespace="http://www.contoso.com")]
    public class MyWebService 
    {
        // Add a member variable of the type deriving from SoapHeader.
        public MyHeader timeStamp;
    

     

    Visual Basic
    <WebService(Namespace:="http://www.contoso.com")> _
    Public Class MyWebService
        ' Add a member variable of the type deriving from SoapHeader.
        Public TimeStamp As MyHeader
    
  2. SoapHeader 特性应用于要处理 SOAP 头的每个 Web 服务方法。将 SoapHeader 特性的 MemberName 属性设置为第一步中创建的成员变量的名称。

        [WebMethod]
        [SoapHeader("timeStamp")]
        public void MyWebMethod()
    

     

    Visual Basic
        <WebMethod, SoapHeader("TimeStamp")> _ 
        Public Sub MyWebMethod()
    
  3. 在应用 SoapHeader 特性的每个 Web 服务方法中,访问在第一步中创建的成员变量以处理在 SOAP 头中发送的数据。

        [WebMethod]
        [SoapHeader("myHeaderMemberVariable")]
        public string MyWebMethod() 
        {
            // Verify that the client sent the SOAP Header.
            if (timeStamp == null) timeStamp = new MyHeader();
            // Set the value of the SoapHeader returned to the client.
            timeStamp.Expires = 60000;
            timeStamp.Created = DateTime.UtcNow;
    
            return("Hello World!");
        }
    

     

    Visual Basic
        <WebMethod,SoapHeader("TimeStamp", _
                              Direction:=SoapHeaderDirection.InOut)> _ 
        Public Function MyWebMethod() As String
            ' Process the SoapHeader.
            If (TimeStamp Is Nothing) Then
                TimeStamp = New MyHeader
            End If
            TimeStamp.Expires = 60000
            TimeStamp.Created = DateTime.UtcNow
    
            Return "Hello World!"
        End Function
    

示例

下面的代码示例演示如何在使用 ASP.NET 创建的 Web 服务中定义和处理 SOAP 头。MyWebService Web 服务具有一个名为 myHeaderMemberVariable 的成员变量,该成员变量属于从 SoapHeader (MyHeader) 派生的类型,设置为 SoapHeader 特性的 MemberName 属性。另外,SoapHeader 特性应用于指定 myHeaderMemberVariableMyWebMethod Web 服务方法。在 MyWebMethod Web 服务方法中,通过访问 myHeaderMemberVariable 来获取 SOAP 头的 Username XML 元素的值。

<%@ WebService Language="C#" Class="MyWebService" %>
using System.Web.Services;
using System.Web.Services.Protocols;

// Define a SOAP header by deriving from the SoapHeader class.
public class MyHeader : SoapHeader 
{
   public DateTime Created;
   public long Expires;
}

[WebService(Namespace="http://www.contoso.com")]
public class MyWebService 
{
    // Add a member variable of the type deriving from SoapHeader.
    public MyHeader myHeaderMemberVariable;
 
    // Apply a SoapHeader attribute.
    [WebMethod]
    [SoapHeader("myHeaderMemberVariable")]
    public void MyWebMethod() 
    {
        // Process the SoapHeader.
        if (myHeaderMemberVariable.Username == "admin")
        {
           // Do something interesting.
        }
    }
}
Visual Basic
<%@ WebService Language="VB" Class="MyWebService" %>
Imports System.Web.Services
Imports System.Web.Services.Protocols

' Define a SOAP header by deriving from the SoapHeader base class.
Public Class MyHeader : Inherits SoapHeader 
    Public Username As String
    Public Password As String
End Class

<WebService(Namespace:="http://www.contoso.com")> _
Public Class MyWebService 
    ' Add a member variable of the type deriving from SoapHeader.
    Public myHeaderMemberVariable As MyHeader
 
    ' Apply a SoapHeader attribute.
    <WebMethod, SoapHeader("myHeaderMemberVariable")> _
    Public Sub MyWebMethod()
        ' Process the SoapHeader.
        If (myHeaderMemberVariable.Username = "admin") Then
           ' Do something interesting.
        End If
    End Sub
End Class

在上一个示例中,如果对 MyWebMethod 的 SOAP 请求包含 UserName 元素设置为 AdminMyHeader SOAP 头,还将执行附加的代码。即,以下 SOAP 请求将引起该代码的执行。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MyHeader xmlns="http://www.contoso.com">
      <Created>dateTime</Created>
      <Expires>long</Expires>
    </MyHeader>
  </soap:Header>
  <soap:Body>
    <MyWebMethod xmlns="http://www.contoso.com" />
  </soap:Body>
</soap:Envelope>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值