首页 > 其他分享 >Web Services使用SOAP Header

Web Services使用SOAP Header

时间:2023-09-28 15:24:39浏览次数:40  
标签:Web string System public Header Services Soap

https://blog.csdn.net/szg3827/article/details/2232809

https://www.cnblogs.com/zxh1919/p/7670110.html

许多的公司都有自己的web服务来支撑自己系统内的运营逻辑,并且是非公开的,那么如何对自己的web服务进行验证呢?不可能任何一个知道你的webservice url 的人都可以去调用你的服务,那企业内部那么多数据岂不全被剽窃?我在这开头只是言明web服务验证的重要性,接下来,我将从比较基础的讲起如何使用soapheader来验证。

在Web Services方法进行通信使用SOAP(简单对象访问协议)遵循标准的SOAP格式,该格式的一部分是在XML文档中编码的数据。XML文档包含一个Envelope根元素(由必需的Body元素和可选的Header元素构成)。Body元素由特定于消息的数据构成。可选的Header元素可以包含不与特定消息直接相关的其他信息。

      一、定义和处理SOAP Header

      在ASP.NET创建的Web Services可以定义和操作SOAP Header。通过在特定的SOAP Header中定义数据类并从SoapHeader类(在System.Web.Services.Protocols命名空间下)派生,便可完成SOAP Header的定义。

 1:  [WebService(Namespace = "http://tempuri.org/")]
 2:  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 3:  [System.ComponentModel.ToolboxItem(false)]
 4:  public class WebService_SoapHeader : System.Web.Services.WebService
 5:  {
 6:      //定义MySoapHeader变量用来保存SoapHeader值
 7:      public MySoapHeader mySoapHeader;
 8:   
 9:      [WebMethod]
10:      [SoapHeader("mySoapHeader")]
11:      public string HelloWorld(string name)
12:      {
13:          return "Hello,"+name;
14:      }
15:  }
16:   
17:  public class MySoapHeader : SoapHeader
18:  {
19:      public int UserID;
20:      public DateTime LoginTime;
21:      public string UserName;
22:  }
23:   

       查看生成的SOAP的Post内容:

POST /WebService_SoapHeader.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloWorld"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MySoapHeader xmlns="http://tempuri.org/">
      <UserID>int</UserID>
      <LoginTime>dateTime</LoginTime>
      <UserName>string</UserName>
    </MySoapHeader>
  </soap:Header>
  <soap:Body>
    <HelloWorld xmlns="http://tempuri.org/">
      <name>string</name>
    </HelloWorld>
  </soap:Body>
</soap:Envelope>

 

      二、客户端使用Soap Header

      引用或使用Wsdl.exe工具可以生成Web Services的引用辅助类。Web Services定义的Soap Header会生成对应的代码:

 1:  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
 2:  [System.SerializableAttribute()]
 3:  [System.Diagnostics.DebuggerStepThroughAttribute()]
 4:  [System.ComponentModel.DesignerCategoryAttribute("code")]
 5:  [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
 6:  public partial class MySoapHeader : object, System.ComponentModel.INotifyPropertyChanged {
 7:   
 8:      private int userIDField;
 9:   
10:      private System.DateTime loginTimeField;
11:   
12:      private string userNameField;
13:   
14:      private System.Xml.XmlAttribute[] anyAttrField;
15:   
16:      /// <remarks/>
17:      [System.Xml.Serialization.XmlElementAttribute(Order=0)]
18:      public int UserID {
19:          get {
20:              return this.userIDField;
21:          }
22:          set {
23:              this.userIDField = value;
24:              this.RaisePropertyChanged("UserID");
25:          }
26:      }
27:   
28:      /// <remarks/>
29:      [System.Xml.Serialization.XmlElementAttribute(Order=1)]
30:      public System.DateTime LoginTime {
31:          get {
32:              return this.loginTimeField;
33:          }
34:          set {
35:              this.loginTimeField = value;
36:              this.RaisePropertyChanged("LoginTime");
37:          }
38:      }
39:   
40:      /// <remarks/>
41:      [System.Xml.Serialization.XmlElementAttribute(Order=2)]
42:      public string UserName {
43:          get {
44:              return this.userNameField;
45:          }
46:          set {
47:              this.userNameField = value;
48:              this.RaisePropertyChanged("UserName");
49:          }
50:      }
51:   
52:      /// <remarks/>
53:      [System.Xml.Serialization.XmlAnyAttributeAttribute()]
54:      public System.Xml.XmlAttribute[] AnyAttr {
55:          get {
56:              return this.anyAttrField;
57:          }
58:          set {
59:              this.anyAttrField = value;
60:              this.RaisePropertyChanged("AnyAttr");
61:          }
62:      }
63:   
64:      public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
65:   
66:      protected void RaisePropertyChanged(string propertyName) {
67:          System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
68:          if ((propertyChanged != null)) {
69:              propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
70:          }
71:      }
72:  }
73:   

       对于Web Method,Soap Header会变成Web Method的第一个参数:

1:  public string HelloWorld(Client.SoapHeaderService.MySoapHeader MySoapHeader, string name) {
2:      Client.SoapHeaderService.HelloWorldRequest inValue = new Client.SoapHeaderService.HelloWorldRequest();
3:      inValue.MySoapHeader = MySoapHeader;
4:      inValue.name = name;
5:      Client.SoapHeaderService.HelloWorldResponse retVal = ((Client.SoapHeaderService.WebService_SoapHeaderSoap)(this)).HelloWorld(inValue);
6:      return retVal.HelloWorldResult;
7:  }
8:   

       客户端测试代码,首先定义Soap Header,再把Soap Header作为Web Method的参数传递:

 1:   static void Main(string[] args)
 2:   {
 3:       SoapHeaderService.WebService_SoapHeaderSoapClient client = new SoapHeaderService.WebService_SoapHeaderSoapClient();
 4:   
 5:       SoapHeaderService.MySoapHeader soapHeader = new SoapHeaderService.MySoapHeader();
 6:       soapHeader.UserID = 1;
 7:       soapHeader.UserName = "User1";
 8:       soapHeader.LoginTime = DateTime.Now;
 9:   
10:       client.HelloWorld(soapHeader, "UserA");
11:  }
12:   

       利用tcpTrace可以查看Post的内容:

image

        在Web Service的Web Method接收到Soap Header:

image

 

      三、SoapHeader的Direction属性

      SoapHeaderDirection有四个值,分别为In、Out、InOut、Flaut。模式值是In。

      1、使用In定义Soap Header,In方式跟上面的例子一样。

      2、使用Out定义Soap Header:

 1:  public class WebService_SoapHeader : System.Web.Services.WebService
 2:  {
 3:      public MySoapHeader mySoapHeader;
 4:   
 5:      [WebMethod]
 6:      [SoapHeader("mySoapHeader",Direction=SoapHeaderDirection.Out)]
 7:      public string HelloWorld(string name)
 8:      {
 9:          return "Hello,"+name;
10:      }
11:  }
12:   

      Soap的Reaspone内容:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MySoapHeader xmlns="http://tempuri.org/">
      <UserID>int</UserID>
      <LoginTime>dateTime</LoginTime>
      <UserName>string</UserName>
    </MySoapHeader>
  </soap:Header>
  <soap:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
      <HelloWorldResult>string</HelloWorldResult>
    </HelloWorldResponse>
  </soap:Body>
</soap:Envelope>

       3、使用InOut定义Soap Header:

1:  [WebMethod]
2:  [SoapHeader("mySoapHeader",Direction=SoapHeaderDirection.InOut)]
3:  public string HelloWorld(string name)
4:  {
5:      return "Hello,"+name;
6:  }
7:   

       Soap的Request:

POST /WebService_SoapHeader.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloWorld"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MySoapHeader xmlns="http://tempuri.org/">
      <UserID>int</UserID>
      <LoginTime>dateTime</LoginTime>
      <UserName>string</UserName>
    </MySoapHeader>
  </soap:Header>
  <soap:Body>
    <HelloWorld xmlns="http://tempuri.org/">
      <name>string</name>
    </HelloWorld>
  </soap:Body>
</soap:Envelope>

       Soap的Response:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MySoapHeader xmlns="http://tempuri.org/">
      <UserID>int</UserID>
      <LoginTime>dateTime</LoginTime>
      <UserName>string</UserName>
    </MySoapHeader>
  </soap:Header>
  <soap:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
      <HelloWorldResult>string</HelloWorldResult>
    </HelloWorldResponse>
  </soap:Body>
</soap:Envelope>

       4、使用Fault定义Soap Header

1:  [WebMethod]
2:  [SoapHeader("mySoapHeader",Direction=SoapHeaderDirection.Fault)]
3:  public string HelloWorld(string name)
4:  {
5:      return "Hello,"+name;
6:  }
7:   

        使用Fault,Soap的描述跟使用Out基本一致:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MySoapHeader xmlns="http://tempuri.org/">
      <UserID>int</UserID>
      <LoginTime>dateTime</LoginTime>
      <UserName>string</UserName>
    </MySoapHeader>
  </soap:Header>
  <soap:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
      <HelloWorldResult>string</HelloWorldResult>
    </HelloWorldResponse>
  </soap:Body>
</soap:Envelope>

 

       四、处理未知的Soap Header

      ASP.NET Web Service通过SoapUnknownHeader来标识未知的Soap Header,SOAP规范通过mustUnderstand特性来表示对Soap Header的要求,当mustUnderstand特性设置为true时,如果传入未知的Soap Header将引发异常。

      注意:ASP.NET 使用DidUnderstand 属性来与 Web Services的方法进行通信。它不属于 SOAP 规范;它的值不会出现在 SOAP 请求或 SOAP 响应的任何部分中。

      五、Soap Header的异常处理

      当 Web Services检测到与处理 SOAP Header有关的错误时,应该会引发 SoapHeaderException。利用此异常类,Web Services可以正确地设置响应的格式。

标签:Web,string,System,public,Header,Services,Soap
From: https://www.cnblogs.com/Dongmy/p/17733091.html

相关文章

  • weblogic乱码报错解决思路
    目录1.集群备份weblogic虚拟机快照备份2.查看主节点控制台面板状态3.尝试启动程序失败4.查看162.主节点日志5.发现程序中乱码6.修改乱码名称7.尝试启动,新的报错还是显示乱码8.修改乱码9.点击更新程序,继续报错9.1依然是乱码10.更新,程序部署路径,换新程序,11.删除程序,处理程序中的乱码......
  • webstorage
    title:Web存储方式tags:-WebStorage-htmlcategories:干垃圾keywords:Web存储方式description:Web存储方式top:repost:truehot:trueothers:date:2019-01-1719:30:07{%noteinfono-icon%}Web的几种存储方式介绍web存储方式有哪些?如下图,常见的浏览......
  • websocket
    title:Websocket简介tags:WebSocketcategories:干垃圾keywords:WebSocketdescription:Websocket简介top:repost:hot:others:date:2020-01-1113:13:13{%notewarningno-icon%}  随着网际网络的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了......
  • java.lang.IllegalStateException: javax.websocket.server.ServerContainer not avai
    spring项目能正常运行,但是单元测试报错错误原因注册WebSocket的Bean与springboot内带tomcat冲突解决办法1.注释该类里面的代码(不推荐)2.@springBootTest注解添加webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT@SpringBootTest注解中,给出了webEnvironment参......
  • 等待多个元素 放入一个列表 WebDriverWait(driver,10).until(EC.visibility_of_all_el
     这里需要百度的热点新闻标题是多个元素    #导包fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC......
  • Web-入门-SpringBoot快速入门 创建springboot web项目
    web入门spring官网spring发展到今天已经形成了一种开发生态圈,spring提供了若干个子项目,为每个项目用于完成特定的功能。这些框架都是基于一个基础框架:直接基于SpringFramework基础框架进行开发会有两大难题:1.配置繁琐。2.入门难度大。所以spring家族意识到了这一点,......
  • webpack - plugins
    (1).插件的作用用于bundle文件的优化资源管理和环境变量注入作用于整个构建过程可以理解为:loader做不了的,给plugins来完成.(2).常用的plugin:CommonsChunkPlugin:将chunk相同的模块代码提取成公共js,如引入同一个js文件CleanWebpackPlugin:清理构建目录ExtractTextWebpackPlugin:......
  • webpack - plugins
    (1).插件的作用用于bundle文件的优化资源管理和环境变量注入作用于整个构建过程可以理解为:loader做不了的,给plugins来完成.(2).常用的plugin:CommonsChunkPlugin:将chunk相同的模块代码提取成公共js,如引入同一个js文件CleanWebpackPlugin:清理构建目录ExtractTextWebpackPlugin:将......
  • 模块化打包工具-初识Webpack
    1.为什么需要模块化打包工具在上一篇文章中提到的ESModule可以帮助开发者更好地组织代码,完成js文件的模块化,基本解决了模块化的问题,但是实际开发中仅仅完成js文件的模块化是不够的,尤其是面对一个较为庞大的工程项目的时候,主要仍有以下几个问题需要解决:ESModule是ES6新语法,一......
  • Webpack报错Error: error:0308010C:digital envelope routines::unsupported处理
    在学习组件库流程打包的时候报错找不到module,后来改了版本又报错Error:error:0308010C:digitalenveloperoutines::unsupported报错原因:node17+版本对发布的OpenSSL3.0,而OpenSSL3.0对允许算法和密钥大小增加了严格的限制,可能会对生态系统造成一些影响.解决方案:在网上搜索......