SoapHeader用户身份验证信息

SoapHeader 多数情况下用来传递用户身份验证信息,当然它的作用远不止如此,有待于在实际应用中发掘。 

SoapHeader 缺省情况下由客户端代理对象发送给 WebService,当然我们可以通过 WebMethodAttribute.Direction 来改变传送方向。 

SoapHeader 使用步骤: 

(1) 创建继承自 System.Web.WebServices.SoapHeader 的自定义 SoapHeader 类型。 
(2) 在 WebService 中创建拥有 public 访问权限的自定义 SoapHeader 字段。 
(3) 在需要使用 SoapHeader 的 WebMethod 上添加 SoapHeaderAttribute 访问特性。SoapHeaderAttribute 构造必须指定 memberName 参数,就是我们在第二步中申明的字段名称。 
(4) 生成器会自动为客户端生成同名的自定义 SoapHeader 类型,只不过比起我们在 WebService 端创建的要复杂一些。同时还会为代理类型添加一个 soapheaderValue 属性。 

在下面的演示代码,客户端将传递一个自定义 MyHeader 到 WebService。请注意,我们尽管在 WebService 中申明了 MyHeader 字段,但并没有创建对象实例,这是因为客户端传递过来的 XML 中包含了 SoapHeader 信息,基础结构会自动解析并创建对象实例,然后赋值给 my 字段。至于客户端,自然需要创建一个 MyHeader 对象实例,并赋值给 WebService.MyHeaderValue 属性。SoapHeaderAttribute.Direction 缺省就是 In,下面例子中的 "Direction = SoapHeaderDirection.In " 可以省略。 

WebServices.cs 
public class MyHeader : SoapHeader 

  public string Username; 
  public string Password; 


[WebService(Namespace = "http://www.rainsts.net/ ", Description= "我的Web服务 ")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
public class WebService : System.Web.Services.WebService  

  public MyHeader my; 

  [WebMethod] 
  [SoapHeader( "my ", Direction = SoapHeaderDirection.In)] 
  public void TestSoapHeadIn() 
  { 
  System.Diagnostics.Debug.Write(my.Username); 
  System.Diagnostics.Debug.Write(my.Password); 
  } 


Client.cs 
WebService ws = new WebService(); 

MyHeader head = new MyHeader(); 
head.Username = "u2 "; 
head.Password = "p2 "; 

ws.MyHeadValue = head; 
ws.TestSoapHeadIn(); 

我们改写一下,将传递方向改为从 WebService 到客户端。自然我们需要调整 "Direction = SoapHeaderDirection.Out ",在 WebMethod 中我们还必须创建 MyHeader 实例,因为这次我们不会接受到客户端传递的 SoapHeader 了。客户端代理对象调用 WebMethod 后就可以使用 MyHeaderValue 属性访问其内容了。 

WebServices.cs 
public class MyHeader : SoapHeader 

  public string Username; 
  public string Password; 


[WebService(Namespace = "http://www.rainsts.net/ ", Description= "我的Web服务 ")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
public class WebService : System.Web.Services.WebService  

  public MyHeader my; 

  [WebMethod] 
  [SoapHeader( "my ", Direction = SoapHeaderDirection.Out)] 
  public void TestSoapHeadOut() 
  { 
  my = new MyHeader(); 
  my.Username = "u1 "; 
  my.Password = "p1 "; 
  } 


Client.cs 
WebService ws = new WebService(); 
ws.TestSoapHeadOut(); 

Console.WriteLine(ws.MyHeaderValue.Username); 
Console.WriteLine(ws.MyHeaderValue.Password);

转载于:https://www.cnblogs.com/lcl_1015/articles/1782464.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 SOAP 头中添加自定义信息,你需要使用 SOAP 头处理程序(SOAP Header Handler)来实现。在 C++ 中,可以通过以下步骤来实现: 1. 创建一个 SOAP 头处理程序类,继承自 gsoapsoap_header 类。 2. 在类中添加要添加到 SOAP 头中的自定义信息字段。 3. 实现 soap_header 类的 virtual 函数,用于在 SOAP 消息中添加自定义信息。 4. 在 gsoap 生成的客户端或服务端代码中,使用 soap_register_plugin() 函数注册 SOAP 头处理程序。 下面是一个简单的示例代码,用于在 SOAP 头中添加自定义信息: ```c++ #include "soapH.h" class MySoapHeader : public soap_header { public: int myCustomField; MySoapHeader() : myCustomField(0) {} virtual int put(struct soap *soap, const char *tag, int id, void *ptr, size_t len) { if (strcmp(tag, "MyCustomField") == 0) { soap_element(soap, "MyCustomField", &myCustomField, sizeof(myCustomField), XSD_INT); return SOAP_OK; } return soap_header::put(soap, tag, id, ptr, len); } }; int main() { struct soap soap; soap_init(&soap); // 创建 SOAP 头处理程序 MySoapHeader header; header.myCustomField = 123; // 注册 SOAP 头处理程序 soap_register_plugin(&soap, &header); // 发送 SOAP 消息 // ... soap_destroy(&soap); soap_end(&soap); soap_done(&soap); return 0; } ``` 在上面的代码中,MySoapHeader 类继承自 soap_header 类,添加了一个名为 myCustomField 的自定义字段。在 put() 函数中,如果 SOAP 消息中的标签为 "MyCustomField",则将 MyCustomField 字段添加到 SOAP 消息中。最后,在 main() 函数中,创建了一个 MySoapHeader 实例并注册到 soap 对象中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值