如何:定义和处理 SOAP 标头
本主题专门介绍一项旧有技术。现在应通过使用以下链接来创建 XML Web 服务和 XML Web 服务客户端: Windows Communication Foundation.
代码示例
使用 ASP.NET 创建的 Web 服务可以定义和操作 SOAP 标头。通过在特定 SOAP 标头中定义表示数据的类并从 SoapHeader 类派生它,便可完成 SOAP 标头的定义。
定义表示 SOAP 标头的类
创建一个从 SoapHeader 类派生的类,其名称与 SOAP 标头的根元素匹配。
public class MyHeader : SoapHeader
Public Class MyHeader : Inherits SoapHeader
添加公共字段或属性,使其名称和每个元素的各个数据类型与 SOAP 标头中的对应项匹配。
例如,给定以下 SOAP 标头,后面的类定义一个表示该 SOAP 标头的类。
<soap:Header xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/"> <MyHeader xmlns="https://www.contoso.com"> <Created>dateTime</Expires> <Expires>long</Expires> </MyHeader> </soap:Header> public class MyHeader : SoapHeader { public DateTime Created; public long Expires; }
Public Class MyHeader : Inherits SoapHeader Public Created As DateTime Public Expires As Long End Class
处理 Web 服务内的 SOAP 标头
向实现表示 SOAP 标头的类型的 Web 服务的类中添加一个公共成员。
[WebService(Namespace="https://www.contoso.com")] public class MyWebService { // Add a member variable of the type deriving from SoapHeader. public MyHeader timeStamp;
<WebService(Namespace:="https://www.contoso.com")> _ Public Class MyWebService ' Add a member variable of the type deriving from SoapHeader. Public TimeStamp As MyHeader
向旨在处理 SOAP 标头的每个 Web 服务方法应用一个 SoapHeader 特性。将 SoapHeader 特性的 MemberName 属性设置为在第一步中创建的成员变量的名称。
[WebMethod] [SoapHeader("timeStamp")] public void MyWebMethod()
<WebMethod, SoapHeader("TimeStamp")> _ Public Sub MyWebMethod()
在应用了 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!"); }
<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 属性。此外,还对指定 myHeaderMemberVariable
的 MyWebMethod
Web 服务方法应用了 SoapHeader 特性。在 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="https://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.
}
}
}
<%@ 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:="https://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 请求有一个 MyHeader
SOAP 标头,并且有一个 UserName
元素设置为 Admin
,则会执行附加代码。也就是说,下面的 SOAP 请求会导致该代码执行。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<MyHeader xmlns="https://www.contoso.com">
<Created>dateTime</Created>
<Expires>long</Expires>
</MyHeader>
</soap:Header>
<soap:Body>
<MyWebMethod xmlns="https://www.contoso.com" />
</soap:Body>
</soap:Envelope>
另请参见
参考
SoapHeader
SoapHeaderAttribute
SoapUnknownHeader
SoapHeaderException