Share via


방법: SOAP 헤더 정의 및 처리

이 항목은 레거시 기술과 관련된 것입니다. 이제 XML Web services와 XML Web services 클라이언트는 다음을 사용하여 만들어야 합니다. Windows Communication Foundation.

코드 예제

ASP.NET을 사용하여 만든 웹 서비스는 SOAP 헤더를 정의하고 조작할 수 있습니다. 특정 SOAP 헤더에서 데이터를 나타내는 클래스를 정의한 후 SoapHeader 클래스에서 파생시켜 SOAP 헤더를 정의합니다.

SOAP 헤더를 나타내는 클래스를 정의하려면

  1. SOAP 헤더의 루트 요소와 일치하는 이름으로 SoapHeader 클래스에서 파생되는 클래스를 만듭니다.

    public class MyHeader : SoapHeader
    
    Public Class MyHeader : Inherits SoapHeader
    
  2. SOAP 헤더의 각 요소에 대해 이름과 해당 데이터 형식이 일치하는 public 필드 또는 속성을 추가합니다.

    예를 들어, 다음 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
    

웹 서비스에서 SOAP 헤더를 처리하려면

  1. SOAP 헤더를 나타내는 형식의 웹 서비스를 구현하는 클래스에 public 멤버를 추가합니다.

    [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 헤더를 처리할 각 웹 서비스 메서드에 SoapHeader 특성을 적용합니다. SoapHeader 특성의 MemberName 속성을 첫 번째 단계에서 만든 멤버 변수의 이름으로 설정합니다.

        [WebMethod]
        [SoapHeader("timeStamp")]
        public void MyWebMethod()
    
        <WebMethod, SoapHeader("TimeStamp")> _ 
        Public Sub MyWebMethod()
    
  3. SoapHeader 특성이 적용되는 각 웹 서비스 메서드 내에서 첫 번째 단계에서 만든 멤버 변수에 액세스하여 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을 사용하여 만든 웹 서비스에서 SOAP 헤더를 정의하고 처리하는 방법을 보여 줍니다. MyWebService 웹 서비스에는 myHeaderMemberVariable라는 멤버 변수가 있습니다. 이 변수는 SoapHeader(MyHeader)에서 파생되는 형식이며 SoapHeader 특성의 MemberName 속성으로 설정됩니다. 또한 SoapHeader 특성은 myHeaderMemberVariable을 지정하는 MyWebMethod 웹 서비스 메서드에 적용됩니다. MyWebMethod 웹 서비스 메서드 내에서 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

개념

XML Web services 클라이언트 빌드

기타 리소스

SOAP 헤더 사용
ASP.NET을 사용하는 XML Web services