Share via


方法 : SOAP ヘッダーを定義および処理する

このトピックの対象は、レガシ テクノロジに特定されています。XML Web サービスと XML Web サービス クライアントは以下を使用して作成してください。 Windows Communication Foundation.

コード例

ASP.NET を使用して作成された Web サービスでは SOAP ヘッダーを定義および操作できます。SOAP ヘッダーを定義するには、特定の SOAP ヘッダーにあるデータを表すクラスを定義し、SoapHeader クラスから派生させます。

SOAP ヘッダーを表すクラスを定義するには

  1. SOAP ヘッダーのルート要素と一致する名前で、SoapHeader クラスから派生するクラスを作成します。

    public class MyHeader : SoapHeader
    
    Public Class MyHeader : Inherits SoapHeader
    
  2. 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 ヘッダーを処理するには

  1. 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
    
  2. SOAP ヘッダーを処理する各 Web サービス メソッドに SoapHeader 属性を適用します。SoapHeader 属性の MemberName プロパティとして、最初の手順で作成したメンバー変数の名前を設定します。

        [WebMethod]
        [SoapHeader("timeStamp")]
        public void MyWebMethod()
    
        <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!");
        }
    
        <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 属性は、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 要求では、追加のコードが実行されます。

<?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

概念

XML Web サービス クライアントの作成

その他のリソース

SOAP ヘッダーの使用
ASP.NET を使用した XML Web サービス