using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
namespace WebService1
{
/**//// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
public MySoapHeader header = new MySoapHeader();
[WebMethod]
[SoapHeader("header")]
public string HelloWorld()
{
if (header == null)
{
return "您没有设置SoapHeader,不能正常访问此服务!";
}
if (header.UserName != "jillzhang" || header.Pwd != "123456")
{
return "您提供的身份验证信息有误,不能正常访问此服务!";
}
return "Hello World";
}
}
}
第四步:创建一个调用Xml Web Service的Console应用程序,如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
localhost.Service1 ws = new ConsoleApplication1.localhost.Service1();
//ws.MySoapHeaderValue = new ConsoleApplication1.localhost.MySoapHeader();
//ws.MySoapHeaderValue.UserName = "jillzhang";
//ws.MySoapHeaderValue.Pwd = "123456";
Console.WriteLine(ws.HelloWorld());
}
}
}
下面的分析,对于大家来说,应该是最重要的,很多人不清楚SoapHeader的工作原理,为什么这么怪异的写法竟能产生神奇的效果,下面我将不同情形下的Soap消息解析出来,大家仔细观察这个信息,并可以清晰地掌握了SoapHeader的工作原理了.
首先,先看看没有设置SoapHeader的情况下,Soap消息为:
-----Soap请求 在 2007年05月22日 12时39分40秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorld xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>
-----Soap响应 在 2007年05月22日 12时39分40秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorldResponse xmlns="http://tempuri.org/"><HelloWorldResult>您提供的身份验证信息有误,不能正常访问此服务!</HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>
再看看在设置了SoapHeader之后的Soap的请求和响应信息
-----Soap请求 在 2007年05月22日 12时42分20秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><MySoapHeader xmlns="http://tempuri.org/"><UserName>jillzhang</UserName><Pwd>123456</Pwd></MySoapHeader></soap:Header><soap:Body><HelloWorld xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>
-----Soap响应 在 2007年05月22日 12时42分20秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorldResponse xmlns="http://tempuri.org/"><HelloWorldResult>Hello World</HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>
点正是通过这个节点,SoapMessage将信息传递给了网络服务端,网络服务端便可以从中解析出来,并加以处理,从上面的SoapMessage中,我们也看出,用户名和密码是以明文的格式传输的,这样,SoapHeader就更像Http协议中的Cookie了,我们可以参考Cookie的使用,来扩展SoapHeader,让它变得更加安全些,但总的看来,通过直接设置SoapHeader的方法提高安全性还是有一定限制的。在安全不是特别重要的应用情形中,推荐采用此种解决方案,因为它方便快捷,灵活易用。
下一节,我将介绍一下,如何获取SoapMessage.