HOW TO:定義及處理 SOAP 標頭
本主題專門說明舊有技術。 應該使用下列建立 XML Web Service 及 XML Web Service 用戶端: Windows Communication Foundation.
程式碼範例
使用 ASP.NET 所建立的 Web 服務可以定義和操作 SOAP 標頭。定義 SOAP 標頭時,會透過定義表示特定 SOAP 標頭中的資料的類別,並從 SoapHeader 類別衍生此類別來完成。
若要定義表示 SOAP 標頭的類別
使用與 SOAP 標頭之根項目相符的名稱,建立衍生自 SoapHeader 類別的類別。
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 標頭
將公用成員新增至實作 Web 服務 (其屬於代表 SOAP 標頭的型別) 的類別。
[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
將 SoapHeader 屬性套用至每個要用來處理 SOAP 標頭的 Web 服務方法。將 SoapHeader 屬性 (Attribute) 的MemberName 屬性 (Property) 設定為第一個步驟中建立之成員變數的名稱。
[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 屬性 (Attribute) 的 MemberName 屬性 (Property)。此外,SoapHeader 屬性還會套用至指定了 myHeaderMemberVariable
的 MyWebMethod
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="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 要求中含有已將 UserName
項目設定為 Admin
的 MyHeader
SOAP 標頭,則會另外執行其他的程式碼。也就是說,下列 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