1、 使用.NET类编写SOAP协议调用Web服务 简介:使用.NET类编写SOAP消息,SOAP消息中包含用户的用户帐号,用户密码和帐号ID。使用HttpWebRequest类发送SOAP请求,请求远程服务器上Web服务程序(客户帐户信息),并使用HttpWebResponse类获取服务响应。 知识点: 命名空间:System.Xml 创建XML文档的类:XmlTextWriter 1. 创建 XmlTextWriter 对象,设置用Tab键缩进 代码示例: XmlTextWriter BookWriter = new XmlTextWriter( @"\cata
2、log\books.xml", Encoding.UTF8); BookWriter.Formatting = Formatting.Indented; 2. 编写XML文档的根元素 使用WriteStartDocument()方法和WriteEndDocument()方法创建XML声明 使用WriteStartElement()方法和WriteEndElement()方法创建根元素 代码示例: BookWriter.WriteStartDocument(); BookWriter.WriteStartElement("books"); // 其他元素 BookWri
3、ter.WriteEndElement();
BookWriter.WriteEndDocument();
输出:
4、>
使用WriteStartElement()和WriteEndElement() 方法创建含有下级子元素和属性的元素
代码示例:
BookWriter.WriteStartElement("book");
BookWriter.WriteElementString("price", "19.95");
BookWriter.WriteEndElement();
输出:
5、BookWriter.WriteAttributeString("price", "19.95");
BookWriter.WriteEndElement();
输出:
6、
BookWriter.WriteEndElement();
输出:
7、s:属性的命名空间 URI。 value:属性值。 此方法写出具有用户定义的命名空间前缀的属性,并将其与给定的命名空间进行关联。如果前缀为“xmlns”,则此方法也将此当做命名空间声明对待,并将声明的前缀与给定属性值中提供的命名空间 URI 进行关联。在这种情况下,ns 参数可以为空引用。 代码示例: xtw.WriteStartElement("bookstore"); // Write the namespace declaration xtw.WriteAttributeString( "xmlns", "bk", null, "urn:samples");
8、xtw.WriteStartElement("book"); // Lookup the prefix and then write the ISBN attribute. string prefix = xtw.LookupPrefix("urn:samples"); xtw.WriteStartAttribute(prefix, "ISBN", "urn:samples"); xtw.WriteString("1-861003-78"); xtw.WriteEndAttribute(); // Write the style element xtw.WriteStar
9、tElement(prefix, "style", "urn:samples");
xtw.WriteString("hardcover");
xtw.WriteEndElement();
// Write the end tag for the book and root elements
xtw.WriteEndElement();
xtw.WriteEndElement();
输出:
10、r
任务:演示——使用.NET类构架SOAP协议调用Web服务
第1步:建立BulidSOAPMessage类,添加静态方法SOAPMessage构建SOAP消息。
using System;
using System.IO;
using System.Xml;
using System.Text;
///
11、ublic static string SOAPMessage(string userID, string password, string acctID) { try { string str=null; using( MemoryStream mStream = new MemoryStream()) //创建内存流对象 { using(XmlTextWriter xtw = new XmlTextWriter(mStream,Enc
12、oding.UTF8)) //创建写内存流对象 { //定义前缀字符串 string xsi = "http://www.w3.org/2001/XMLSchema-instance"; string xsd = "http://www.w3.org/2001/XMLSchema"; string soap = "http://schemas.xmlsoap.org/soap/envelope/
13、"; string namespaceurl = "http://tempuri.org/"; //书写缩进的XML文档,设置用tab键缩进 xtw.Formatting = Formatting.Indented; //书写版本为“1.0”,并具有独立属性的XML声明,即 xtw.WriteStar
14、tDocument(); //书写开始标记"Envelope",并与给定的命名空间"http://schemas.xmlsoap.org/soap/envelope/"和前缀"soap"关联 xtw.WriteStartElement("soap", "Envelope", soap); //声明属性 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xtw.Wr
15、iteAttributeString("xmlns", "xsi", null, xsi); //声明属性 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xtw.WriteAttributeString("xmlns", "xsd", null, xsd); //声明属性 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xt
16、w.WriteAttributeString("xmlns", "soap", null, soap); //声明复合元素“Header”,并与"soap"前缀关联 xtw.WriteStartElement("Header", soap); //声明“Header”的子元素"WoodgroveAuthInfo",与命名空间nsurl关联,不指定前缀 xtw.WriteStartElement(null, "Woodgrov
17、eAuthInfo", namespaceurl); //声明"WoodgroveAuthInfo"元素内包含的基本元素"Username"和"Password",并赋值 xtw.WriteElementString("Username", userID); xtw.WriteElementString("Password", password); //结束"WoodgroveAuthInfo"和“Header”的声明
18、 xtw.WriteEndElement(); xtw.WriteEndElement(); //声明"Body"元素,并与soap命名空间关联 xtw.WriteStartElement("Body", soap); //声明"Body"元素下的嵌套子元素"GetAccount",关联Web服务给定的命名空间,不设前缀 xtw.WriteSt
19、artElement(null, "GetAccount", namespaceurl); //声明"GetAccount"元素下嵌套的基本元素"acctID",并赋值 xtw.WriteElementString("acctID", acctID); //结束"GetAccount"和"Body"声明 xtw.WriteEndElement(); xtw.WriteEndEle
20、ment(); //结束"Envelope"声明 xtw.WriteEndDocument(); //将缓冲区中数据刷新到基础流,并同时刷新基础流 xtw.Flush(); //将基础数据流转换为字符串 str = MemStreamToString(mStream); }
21、} return str; } catch { return null; } } private static string MemStreamToString(MemoryStream mStream) { //将基础流的数据转换为无符号的字节数组 byte[] buffer = mStream.GetBuffer(); //创建把字节序列转换为字符序列的解码器
22、 Decoder d = Encoding.UTF8.GetDecoder(); //创建字符缓冲区,用于存放解码后的字符序列 char[] chars = new char[buffer.Length]; //使用解码器将字节数组转换为字符数组 d.GetChars(buffer, 3, buffer.Length - 3, chars, 0); //将字符数组转换为字符串 string str = new String(chars); retur
23、n str;
}
}
第2步:创建处理类SOAPMessageHandler,使用SOAP协议调用Web服务
using System;
using System.Net;
using System.IO;
///
24、 private string _privateSOAPResponse = null; private string _privateSOAPMessage = null; public SOAPMessageHandler(string userID, string password, string acctID,string uri) { //构建SOAP请求消息 _privateSOAPMessage = BulidSOAPMessage.SOAPMessage(userID, password, acctI
25、D); if (_privateSOAPMessage != null) { _privateSOAPResponse=this.GetSOAPMessage(uri,_privateSOAPMessage); } } private string GetSOAPMessage(string uri,string content) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new
26、 Uri(uri)); request.ContentType = "text/xml;charset=utf-8"; request.ContentLength = _privateSOAPMessage.Length; request.Method = "POST"; request.Headers.Add("http://tempuri.org/GetAccount"); using (StreamWriter sw = new StreamWriter(request.GetR
27、equestStream())) { sw.Write(content); } string str=null; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using(StreamReader reader = new StreamReader(response.GetResponseStream())) { str = reader.ReadToEnd(); } } return str; } } 7






