在webservice中简单使用SoapHeader
环境:visual studio 2005 --ASP.net 在我的例子中,主要是简单的定义一个SoapHeader,并且在客户端和Webservice中使用。 场景描述: ?????? 用户在文本框中输入用户名和密码,点击提交按钮去调用一个webservice的方法来处理。在客户端发送请求的时候,用户名和密码的信息保存在soapheader中。webservice方法进行处理的时候,通过从soapheader中取出用户名和方法进行处理。 ? 1、SoapHeader类的定义 ?? using System.Web.Services.Protocols; ? ?? class MySoapHeader:SoapHeader ?? { ????????? public string userName; ????????? public string password; ?? } ? 2.webservice 中使用soapheader 定义一个webservice,命名为FirstService.asmx(使用自动生成的就可以了,修改一下HelloWord方法) ? public class FirstService : System.Web.Services.WebService {? ??? public FirstService () { ??????? //如果使用设计的组件,请取消注释以下行 ? ?? //声明一个MySoapHeader ? ??? [WebMethod] ? 3.客户端使用soapheader ??? 提交按钮的单击事件 ? protected void Button1_Click(object sender,EventArgs e) ?????? //new 一个webservice的代理类 ??????? FirstService service = new FirstService(); ??????? service.mySoapheader.password = password.Text;
? ?ok,完成,可以发现当点击提交按钮时,用户名和密码是放在soap消息头中的。 ?客户端发送的消息如下: POST /WebSite1/FirstService.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/"> <userName>string</userName> <password>string</password> </MySoapHeader> </soap:Header> <soap:Body> <HelloWorld xmlns="http://tempuri.org/" /> </soap:Body> </soap:Envelope> 这是最基本的使用,如果想增加webservice消息的安全,可以利用soap扩展,对soap消息中的元素进行安全处理。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |