Web服務端代碼
<%
@ WebService Language
=
"
C#
"
Class
=
"
WebService
"
%>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
... {
public AuthHeaderCS sHeader;
[WebMethod(Description = "使用SOAP頭條目實現用戶名和密碼驗證")]
[SoapHeader("sHeader")]
public string SecureMethod()
...{
if (sHeader == null)
...{
return "錯誤,請輸入驗證信息﹗";
}
string UserName = sHeader.UserName;
string PassWord = sHeader.PassWord;
if (AuthenticateUser(UserName, PassWord))
...{
return "驗證通過,您的用戶名為︰" + UserName + ",密碼為︰" + PassWord;
}
else
...{
return "驗證失敗,請重新輸入用戶名和密碼﹗";
}
}
private bool AuthenticateUser(string UserName, string PassWord)
...{
if ((UserName == "John") && (PassWord == "John"))
...{
return true;
}
else
...{
return false;
}
}
}
public class AuthHeaderCS : SoapHeader
... {
public string UserName;
public string PassWord;
}
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
... {
public AuthHeaderCS sHeader;
[WebMethod(Description = "使用SOAP頭條目實現用戶名和密碼驗證")]
[SoapHeader("sHeader")]
public string SecureMethod()
...{
if (sHeader == null)
...{
return "錯誤,請輸入驗證信息﹗";
}
string UserName = sHeader.UserName;
string PassWord = sHeader.PassWord;
if (AuthenticateUser(UserName, PassWord))
...{
return "驗證通過,您的用戶名為︰" + UserName + ",密碼為︰" + PassWord;
}
else
...{
return "驗證失敗,請重新輸入用戶名和密碼﹗";
}
}
private bool AuthenticateUser(string UserName, string PassWord)
...{
if ((UserName == "John") && (PassWord == "John"))
...{
return true;
}
else
...{
return false;
}
}
}
public class AuthHeaderCS : SoapHeader
... {
public string UserName;
public string PassWord;
}
<%
...
@ Page Language="C#"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< script runat ="server" > ...
protected void Page_Load(object sender, EventArgs e)
...{
WebService myserver = new WebService();
AuthHeaderCS header = new AuthHeaderCS();
try
...{
Response.Write("第一次調用︰" + myserver.SecureMethod() + "<br />");
}
catch (Exception Err)
...{
Response.Write(Err.Message + "<br />");
}
try
...{
header.UserName = "John";
header.PassWord = "Wang";
myserver.sHeader = header;
Response.Write("第二次調用︰" + myserver.SecureMethod() + "<br />");
}
catch (Exception Err)
...{
Response.Write(Err.Message + "<br />");
}
try
...{
header.UserName = "John";
header.PassWord = "John";
myserver.sHeader = header;
Response.Write("第三次調用︰" + myserver.SecureMethod() + "<br />");
}
catch (Exception Err)
...{
Response.Write(Err.Message + "<br />");
}
}
</ script >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 使用SOAP頭條目實現用戶名和密碼驗證 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
</ div >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< script runat ="server" > ...
protected void Page_Load(object sender, EventArgs e)
...{
WebService myserver = new WebService();
AuthHeaderCS header = new AuthHeaderCS();
try
...{
Response.Write("第一次調用︰" + myserver.SecureMethod() + "<br />");
}
catch (Exception Err)
...{
Response.Write(Err.Message + "<br />");
}
try
...{
header.UserName = "John";
header.PassWord = "Wang";
myserver.sHeader = header;
Response.Write("第二次調用︰" + myserver.SecureMethod() + "<br />");
}
catch (Exception Err)
...{
Response.Write(Err.Message + "<br />");
}
try
...{
header.UserName = "John";
header.PassWord = "John";
myserver.sHeader = header;
Response.Write("第三次調用︰" + myserver.SecureMethod() + "<br />");
}
catch (Exception Err)
...{
Response.Write(Err.Message + "<br />");
}
}
</ script >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 使用SOAP頭條目實現用戶名和密碼驗證 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
</ div >
</ form >
</ body >
</ html >