Login
升级VIP 登录 注册 安全退出
当前位置: 首页 > word文档 > 合同模板 > 使用.NET类编写SOAP协议调用Web服务

使用.NET类编写SOAP协议调用Web服务

收藏

本作品内容为使用.NET类编写SOAP协议调用Web服务,格式为 doc ,大小 65536 KB ,页数为 7页

使用.NET类编写SOAP协议调用Web服务


('使用.NET类编写SOAP协议调用Web服务简介:使用.NET类编写SOAP消息,SOAP消息中包含用户的用户帐号,用户密码和帐号ID。使用HttpWebRequest类发送SOAP请求,请求远程服务器上Web服务程序(客户帐户信息),并使用HttpWebResponse类获取服务响应。知识点:命名空间:System.Xml创建XML文档的类:XmlTextWriter1.创建XmlTextWriter对象,设置用Tab键缩进代码示例:XmlTextWriterBookWriter=newXmlTextWriter(@"\\catalog\\books.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");输出:119.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("NancyDavolio");BookWriter.WriteEndElement();输出:NancyDavolio6.编写带有命名空间的属性使用WriteAttributeString()方法为元素添加带命名空间的属性publicvoidWriteAttributeString(stringprefix,stringlocalName,stringns,stringvalue2)参数prefix:属性的命名空间前缀。localName:属性的本地名称。ns:属性的命名空间URI。value:属性值。此方法写出具有用户定义的命名空间前缀的属性,并将其与给定的命名空间进行关联。如果前缀为“xmlns”,则此方法也将此当做命名空间声明对待,并将声明的前缀与给定属性值中提供的命名空间URI进行关联。在这种情况下,ns参数可以为空引用。代码示例:xtw.WriteStartElement("bookstore");//Writethenamespacedeclarationxtw.WriteAttributeString("xmlns","bk",null,"urn:samples");xtw.WriteStartElement("book");//LookuptheprefixandthenwritetheISBNattribute.stringprefix=xtw.LookupPrefix("urn:samples");xtw.WriteStartAttribute(prefix,"ISBN","urn:samples");xtw.WriteString("1-861003-78");xtw.WriteEndAttribute();//Writethestyleelementxtw.WriteStartElement(prefix,"style","urn:samples");xtw.WriteString("hardcover");xtw.WriteEndElement();//Writetheendtagforthebookandrootelementsxtw.WriteEndElement();xtw.WriteEndElement();输出:hardcover任务:演示——使用.NET类构架SOAP协议调用Web服务3第1步:建立BulidSOAPMessage类,添加静态方法SOAPMessage构建SOAP消息。usingSystem;usingSystem.IO;usingSystem.Xml;usingSystem.Text;///

///BulidSOAPMessage的摘要说明///publicclassBulidSOAPMessage{publicstaticstringSOAPMessage(stringuserID,stringpassword,stringacctID){try{stringstr=null;using(MemoryStreammStream=newMemoryStream())//创建内存流对象{using(XmlTextWriterxtw=newXmlTextWriter(mStream,Encoding.UTF8))//创建写内存流对象{//定义前缀字符串stringxsi="http://www.w3.org/2001/XMLSchema-instance";stringxsd="http://www.w3.org/2001/XMLSchema";stringsoap="http://schemas.xmlsoap.org/soap/envelope/";stringnamespaceurl="http://tempuri.org/";//书写缩进的XML文档,设置用tab键缩进xtw.Formatting=Formatting.Indented;//书写版本为“1.0”,并具有独立属性的XML声明,即xtw.WriteStartDocument();//书写开始标记"Envelope",并与给定的命名空间"http://schemas.xmlsoap.org/soap/envelope/"和前缀"soap"关联xtw.WriteStartElement("soap","Envelope",soap);//声明属性xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xtw.WriteAttributeString("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/"xtw.WriteAttributeString("xmlns","soap",null,soap);4//声明复合元素“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);}}returnstr;}catch{returnnull;5}}privatestaticstringMemStreamToString(MemoryStreammStream){//将基础流的数据转换为无符号的字节数组byte[]buffer=mStream.GetBuffer();//创建把字节序列转换为字符序列的解码器Decoderd=Encoding.UTF8.GetDecoder();//创建字符缓冲区,用于存放解码后的字符序列char[]chars=newchar[buffer.Length];//使用解码器将字节数组转换为字符数组d.GetChars(buffer,3,buffer.Length-3,chars,0);//将字符数组转换为字符串stringstr=newString(chars);returnstr;}}第2步:创建处理类SOAPMessageHandler,使用SOAP协议调用Web服务usingSystem;usingSystem.Net;usingSystem.IO;//////SOAPMessageHandler的摘要说明///publicclassSOAPMessageHandler{publicstringSOAPResponse{get{return_privateSOAPResponse;}}privatestring_privateSOAPResponse=null;privatestring_privateSOAPMessage=null;publicSOAPMessageHandler(stringuserID,stringpassword,stringacctID,stringuri){//构建SOAP请求消息6_privateSOAPMessage=BulidSOAPMessage.SOAPMessage(userID,password,acctID);if(_privateSOAPMessage!=null){_privateSOAPResponse=this.GetSOAPMessage(uri,_privateSOAPMessage);}}privatestringGetSOAPMessage(stringuri,stringcontent){HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create(newUri(uri));request.ContentType="text/xml;charset=utf-8";request.ContentLength=_privateSOAPMessage.Length;request.Method="POST";request.Headers.Add("http://tempuri.org/GetAccount");using(StreamWritersw=newStreamWriter(request.GetRequestStream())){sw.Write(content);}stringstr=null;using(HttpWebResponseresponse=(HttpWebResponse)request.GetResponse()){using(StreamReaderreader=newStreamReader(response.GetResponseStream())){str=reader.ReadToEnd();}}returnstr;}}7',)


  • 编号:1700675267
  • 分类:合同模板
  • 软件: wps,office word
  • 大小:7页
  • 格式:docx
  • 风格:商务
  • PPT页数:65536 KB
  • 标签:

广告位推荐

相关合同模板更多>