使用NET类编写SOAP协议调用Web服务.doc_第1页
使用NET类编写SOAP协议调用Web服务.doc_第2页
使用NET类编写SOAP协议调用Web服务.doc_第3页
使用NET类编写SOAP协议调用Web服务.doc_第4页
使用NET类编写SOAP协议调用Web服务.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

使用.NET类编写SOAP协议调用Web服务简介:使用.NET类编写SOAP消息,SOAP消息中包含用户的用户帐号,用户密码和帐号ID。使用HttpWebRequest类发送SOAP请求,请求远程服务器上Web服务程序(客户帐户信息),并使用HttpWebResponse类获取服务响应。知识点:命名空间:System.Xml创建XML文档的类:XmlTextWriter1. 创建 XmlTextWriter 对象,设置用Tab键缩进代码示例:XmlTextWriter BookWriter = new XmlTextWriter(catalogbooks.xml, Encoding.UTF8);BookWriter.Formatting = Formatting.Indented;2. 编写XML文档的根元素使用WriteStartDocument()方法和WriteEndDocument()方法创建XML声明使用WriteStartElement()方法和WriteEndElement()方法创建根元素代码示例:BookWriter.WriteStartDocument();BookWriter.WriteStartElement(books);/ 其他元素BookWriter.WriteEndElement();BookWriter.WriteEndDocument();输出: 3. 编写元素使用WriteElementString()方法创建不包含子元素和属性的元素代码示例:BookWriter.WriteElementString(price, 19.95);输出:19.95使用WriteStartElement()和WriteEndElement() 方法创建含有下级子元素和属性的元素代码示例:BookWriter.WriteStartElement(book);BookWriter.WriteElementString(price, 19.95);BookWriter.WriteEndElement();输出:19.954. 编写属性代码示例:BookWriter.WriteStartElement(book);BookWriter.WriteAttributeString(price, 19.95);BookWriter.WriteEndElement();输出:5. 编写带有命名空间的元素使用WriteElementString()方法或 WriteStartElement()方法编写带命名空间的元素代码示例:BookWriter.WriteStartElement(hr, Name, http:/hrweb);BookWriter.WriteString(Nancy Davolio);BookWriter.WriteEndElement();输出:Nancy Davolio6. 编写带有命名空间的属性使用WriteAttributeString()方法为元素添加带命名空间的属性public void WriteAttributeString ( string prefix, string localName, string ns, string value)参数prefix:属性的命名空间前缀。 localName:属性的本地名称。 ns:属性的命名空间 URI。 value:属性值。此方法写出具有用户定义的命名空间前缀的属性,并将其与给定的命名空间进行关联。如果前缀为“xmlns”,则此方法也将此当做命名空间声明对待,并将声明的前缀与给定属性值中提供的命名空间 URI 进行关联。在这种情况下,ns 参数可以为空引用。代码示例:xtw.WriteStartElement(bookstore);/ Write the namespace declarationxtw.WriteAttributeString(xmlns, bk, null, urn:samples);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 elementxtw.WriteStartElement(prefix, style, urn:samples);xtw.WriteString(hardcover);xtw.WriteEndElement();/ Write the end tag for the book and root elementsxtw.WriteEndElement();xtw.WriteEndElement();输出: hardcover 任务:演示使用.NET类构架SOAP协议调用Web服务第1步:建立BulidSOAPMessage类,添加静态方法SOAPMessage构建SOAP消息。using System;using System.IO;using System.Xml;using System.Text;/ / BulidSOAPMessage 的摘要说明/ public class BulidSOAPMessage public static string SOAPMessage(string userID, string password, string acctID) try string str=null; using( MemoryStream mStream = new MemoryStream() /创建内存流对象 using(XmlTextWriter xtw = new XmlTextWriter(mStream,Encoding.UTF8) /创建写内存流对象 /定义前缀字符串 string xsi = /2001/XMLSchema-instance; string xsd = /2001/XMLSchema; string soap = /soap/envelope/; string namespaceurl = /; /书写缩进的XML文档,设置用tab键缩进 xtw.Formatting = Formatting.Indented; /书写版本为“1.0”,并具有独立属性的XML声明,即 xtw.WriteStartDocument(); /书写开始标记Envelope,并与给定的命名空间/soap/envelope/和前缀soap关联 xtw.WriteStartElement(soap, Envelope, soap); /声明属性 xmlns:xsi=/2001/XMLSchema-instance xtw.WriteAttributeString(xmlns, xsi, null, xsi); /声明属性 xmlns:xsd=/2001/XMLSchema xtw.WriteAttributeString(xmlns, xsd, null, xsd); /声明属性 xmlns:soap=/soap/envelope/ xtw.WriteAttributeString(xmlns, soap, null, soap); /声明复合元素“Header”,并与soap前缀关联 xtw.WriteStartElement(Header, soap); /声明“Header”的子元素WoodgroveAuthInfo,与命名空间nsurl关联,不指定前缀 xtw.WriteStartElement(null, WoodgroveAuthInfo, namespaceurl); /声明WoodgroveAuthInfo元素内包含的基本元素Username和Password,并赋值 xtw.WriteElementString(Username, userID); xtw.WriteElementString(Password, password); /结束WoodgroveAuthInfo和“Header”的声明 xtw.WriteEndElement(); xtw.WriteEndElement(); /声明Body元素,并与soap命名空间关联 xtw.WriteStartElement(Body, soap); /声明Body元素下的嵌套子元素GetAccount,关联Web服务给定的命名空间,不设前缀 xtw.WriteStartElement(null, GetAccount, namespaceurl); /声明GetAccount元素下嵌套的基本元素acctID,并赋值 xtw.WriteElementString(acctID, acctID); /结束GetAccount和Body声明 xtw.WriteEndElement(); xtw.WriteEndElement(); /结束Envelope声明 xtw.WriteEndDocument(); /将缓冲区中数据刷新到基础流,并同时刷新基础流 xtw.Flush(); /将基础数据流转换为字符串 str = MemStreamToString(mStream); return str; catch return null; private static string MemStreamToString(MemoryStream mStream) /将基础流的数据转换为无符号的字节数组 byte buffer = mStream.GetBuffer(); /创建把字节序列转换为字符序列的解码器 Decoder d = Encoding.UTF8.GetDecoder(); /创建字符缓冲区,用于存放解码后的字符序列 char chars = new charbuffer.Length; /使用解码器将字节数组转换为字符数组 d.GetChars(buffer, 3, buffer.Length - 3, chars, 0); /将字符数组转换为字符串 string str = new String(chars); return str; 第2步:创建处理类SOAPMessageHandler,使用SOAP协议调用Web服务using System;using System.Net;using System.IO;/ / SOAPMessageHandler 的摘要说明/ public class SOAPMessageHandler public string SOAPResponse get return _privateSOAPResponse; private string _privateSOAPResponse = null; private string _privateSOAPMessage = null; public SOAPMessageHandler(string userID, string password, string acctID,string uri) /构建SOAP请求消息 _privateSOAPMessage = BulidSOAPMessage.SOAPMessage(userID, password, acctID); if (_privateSOAPMessage != null) _privateSOAPResponse=this.GetSOAPMessage(uri,_privateSOAPMessage); private string GetSOAPMessage(string uri,string content) HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(uri); request.ContentType = text/xml;charset=utf-8; request.ContentLength = _privateSOAPMessage.Length; request.Method = POST; request.Headers.Add(/GetAccount); using

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论