本文为大家分享了Asp.net页面中调用以SOAP头作验证的web services操作步骤,供大家参考,具体内容如下
第一步:用来作SOAP验证的类必须从SoapHeader类派生,类中Public的属性将出现在自动产生XML节点中,即:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<soap:Header>
<UserName>
string
</UserName>
<Pwd>
string
</Pwd>
</UserSoapHeader>
</soap:Header>
public
class
UserSoapHeader : SoapHeader
{
private
string
_userName;
private
string
_pwd;
//public的属性将自动生成xml结点
public
string
UserName
{
get
{
return
_userName; }
set
{ _userName = value; }
}
public
string
Pwd
{
get
{
return
_pwd; }
set
{ _pwd = value; }
}
}
|
第二步:
在WebServices服务类中添加一个public的属性(必须public),类型为从UserSoapHeader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public
class
WebService : System.Web.Services.WebService
{
//此属性将作为验证属性
//方法的SoapHeaderAttribute中的名称与此变量一致
public
UserSoapHeader userHeader;
public
WebService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
[SoapHeader(
"userHeader"
)]
//这里很重要,名称要和定义的验证属性名称一致!
public
string
HelloWorld()
{
//进入此方法后,userHeader将自动有值
if
(userHeader !=
null
)
{
return
"this is retVal : "
+ userHeader.UserName;
}
return
" check not successed "
;
}
}
|
第三步:在客户端进行调用:
1. 添加WEB引用
2. 实例化服务类
3. 实例化SOAP头(在客户端将会自动生成作来作验证的属性;该属性类型为:UserSoapHeader;该属性的名称为:UserSoapHeaderValue) ;自动生成的属性生成规则为:验证类型名称+Value;
4. 调用服务提供的方法。
1
2
3
4
5
6
7
|
WebService s =
new
WebService();
UserSoapHeader a =
new
UserSoapHeader();
a.UserName =
"admin"
;
a.Pwd =
"zz"
;
s.UserSoapHeaderValue = a;
//此属性是自动生成的
Response.Write( s.HelloWorld() );
// this is retVal : admin
|