zoukankan      html  css  js  c++  java
  • [转]如何:定义和处理 SOAP 标头

    本文转自:http://msdn.microsoft.com/zh-cn/library/vstudio/8728chd5(v=vs.100).aspx

    本主题专门介绍一项旧有技术。现在应通过使用以下链接来创建 XML Web 服务和 XML Web 服务客户端:    Windows Communication Foundation    .   

    代码示例

    使用 ASP.NET 创建的 Web 服务可以定义和操作 SOAP 标头。通过在特定 SOAP 标头中定义表示数据的类并从 SoapHeader 类派生它,便可完成 SOAP 标头的定义。

    定义表示 SOAP 标头的类

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

      public class MyHeader : SoapHeader
      
      
      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;
      }
      
      
      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;
      
      <WebService(Namespace:="http://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 属性。此外,还对指定 myHeaderMemberVariableMyWebMethod 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="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.
            }
        }
    }
    
    

    在上一示例中,如果对 MyWebMethod 的 SOAP 请求有一个 MyHeader SOAP 标头,并且有一个 UserName 元素设置为 Admin,则会执行附加代码。也就是说,下面的 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>
    

    另请参见

  • 相关阅读:
    php判断token有效期
    ecshop数据库操作
    CSS3选择器 :nth-child(n) 如何理解
    socket_wirte、socket_read、socket_send、socket_recv
    Mysql按指定顺序排序的两种写法
    Photoshop图片优化文件大小
    Wordpress中的自定义栏目是什么玩意,有什么用?
    Windows使用压缩包安装Mysql5.7.17服务
    Discuz论坛搬家后出现 Unknown column 'app1' in 'where clause' 原因及解决办法
    .PHP后缀大写导致Linux下Composer找不到类
  • 原文地址:https://www.cnblogs.com/freeliver54/p/3532698.html
Copyright © 2011-2022 走看看