网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > DotNet > XML&Web > 不用.net和其组件用asp访问webservice  

不用.net和其组件用asp访问webservice

2005-09-28  作者:BitsCN整理  来源:中国网管联盟  点评 投稿 收藏


  可能,大多数的人认为我们需要运行asp.net或使用soap toolkit以访问webservice。但是这不是必需的,使用微软的xml parser我们同样可以利用传统的asp页面来访问webservice,下面我就展示给大家看一看!
  
  我将使用三个文件来实现我的展示。
  
  global.asa,当程序开始运行时,使用application变量
  
  i_soapcall.asp 一个包含文件,用以访问soap service
  
  default.asp 一个基本的asp文件,用以显示soap数据Global.asa
  
  当website运行时global.asa时刻都在运行,在application_onstart中我们加入application变量
  
  <SCRIPT LANGUAGE=VBScript RUNAT=Server>
  Sub Application_OnStart
  Dim ASPNETResources
  ASPNETResources = GetASPNetResources()
  Application("ASPNETExpires") = 12
  If Len(ASPNETResources) >0 then
  Application.Lock
  Application("ASPNETResourcesUpdated")=Now()
  Application("ASPNETResourceList")=ASPNETResources
  Application.UnLock
  End if
  End Sub
  </script>
  <!-- #include file="i_soapcall.asp" --> 网管联盟bitsCN@com
  
  当application第一次执行时,我们定义了一个变量:ASPNETResources,它的值是函数GetASPNetResources()的执行结果,这个函数可以在包含文件i_soapcall.asp中找到,他返回一个字符串,随后我们定义了过期时间的变量:
  
  Application("ASPNETExpires"),我们将在default.asp中使用,最后我们检查了GetASPNetResources()是否执行正确,返回值长度是否大于0,如果成功我们需要纪录时间 Application("ASPNETResourcesUpdated"), 和执行结果Application("ASPNETResourceList"). 在后面我将告诉大家这些变量是做什么的!
  
  Default.asp
  
  default.asp是用来显示我们的webservice请求
  
  <%
  Dim   ASPNETResources
  If len( Application("ASPNETResourceList") )>0 then
  If DateDiff("h",Now(),Application("ASPNETResourcesUpdated")) > Application("ASPNETExpires") Then
  ASPNETResources = GetASPNetResources()
  Application.Lock
  Application("ASPNETResourcesUpdated")=Now()
  Application("ASPNETResourceList")=ASPNETResources
  Application.UnLock
  End if
  Else
网管联盟bitsCN@com

  ASPNETResources = GetASPNetResources()
  Application.Lock
  Application("ASPNETResourcesUpdated")=Now()
  Application("ASPNETResourceList")=ASPNETResources
  Application.UnLock
  End if
  Response.Write   Application("ASPNETResourceList")
  %>
  
  现在是神秘的i_soapcall.asp
  
  大家在想神秘的GetASPNetResources()到底是什么样子的,可以用基本的asp页面调用webservice,不要忘了soap service无论是wsdl文档还是执行结果都是一个xml文档,所以我们可以parse(解析)它,这并不困难!
  
  在函数中我们用到两个object
  
  Function GetASPNetResources()
  Set SoapRequest = Server.CreateObject("MSXML2.XMLHTTP")
  Set myXML =Server.CreateObject("MSXML.DOMDocument")
  
  SoapRequest 是服务器端组件,可以发送post和get请求。
  
  myXML将被用来创建一个soap service的xml文档
  
  myXML.Async=False
  SoapURL = "http://64.85.12.73/WebSvc/whatsnew123apx_ds.asmx/GetNew123aspXResources?"
  SoapRequest.Open "GET",SoapURL , False 网管网www.bitscn.com
  SoapRequest.Send()
  if Not myXML.load(SoapRequest.responseXML) then
  returnString = ""
  Else
  
  我们设定SoapURL 为我们的webservice的url然后我们用下面的语句打开连接SoapRequest.Open "GET",SoapURL , False
  
  SoapRequest.Open有五个参数,但是只有前两个是必需的,这意味着其他三个是可以随意选择的
  
  参数解释:
  
  oServerXMLHTTPRequest.open bstrMethod, bstrUrl, bAsync, bstrUser, bstrPassword
  Parameters
  bstrMethod
  HTTP method used to open the connection, such as PUT or PROPFIND.
  bstrUrl
  Requested URL. This must be an absolute URL, such as "http://Myserver/Mypath/Myfile.asp".
  bAsync (optional)
  Boolean. Indicator as to whether the call is asynchronous. The default is False (the call does not
  return immediately).
  bstrUser (optional)
  Name of the user for authentication.
  bstrPassword (optional)
  Password for authentication. This parameter is ignored if the user parameter is Null or missing

中国网管论坛bbs.bitsCN.com


  
  设置完毕我们用SoapRequest.Send()向服务器发送请求,服务器返回的结果作为文本被存储在SoapRequest.responseXML中。我们要做的就是构析这段文本。
  
  下面给出i_soapcall.asp的全部代码
  
  <script language="vbscript" runat="server">
  Function GetASPNetResources()
  Dim returnString
  Dim myXML
  Dim SoapRequest
  Dim SoapURL
  Set SoapRequest = Server.CreateObject("MSXML2.XMLHTTP")
  Set myXML =Server.CreateObject("MSXML.DOMDocument")
  myXML.Async=False
  SoapURL = "http://64.85.12.73/WebSvc/whatsnew123apx_ds.asmx/GetNew123aspXResources?"
  '这是真实可用的webservice
  SoapRequest.Open "GET",SoapURL , False
  SoapRequest.Send()
  if Not myXML.load(SoapRequest.responseXML) then 'an Error loading XML
  returnString = ""
  Else  'parse the XML
  Dim nodesURL
  Dim nodesName
  Dim nodesDateUpdated
  Dim nodesDomain
  Dim NumOfNodes
  Dim ResourceList
  Dim i
  REM -- The XML Nodes are CASE SENSITIVVE! 网管u家bitscn.net
  Set nodesURL=myXML.documentElement.selectNodes("//URL")
  Set nodesName=myXML.documentElement.selectNodes("//Name")
  REM -- uncomment the following lines if we want to access the DataUpdated and the Domain Nodes
  REM --Set nodesDateUpdated = myXML.documentElement.selectNodes("//DateUpdated")
  REM --Set nodesDomain = myXML.documentElement.selectNodes("//Domain")
  REM -- the number of nodes in the list
  NumOfNodes = nodesURL.Length
  ResourceList = "<font face=verdana size=2>Latest ASP.NET Resources</font><ul>"
  For i = 0 to NumOfNodes -1
  ResourceList = ResourceList & "<li><a href=" & nodesURL(i).text & "><font face=verdana size=2>" &
  nodesName(i).text & "</font></a></li>"
  next
  ResourceList =ResourceList & "</ul>"
  returnString = ResourceList
  Set nodesURL = Nothing
  Set nodesName = Nothing
  End If
  Set SoapRequest = Nothing
  Set myXML = Nothing
  GetASPNetResources = returnString 中国网管联盟bitsCN.com
  End Function
  </script>
  
  同样的创作思路可以用在别的编程语言中
TAGs访问   组件   不用   Application   ASPNETResources   GetASPNetResources    
 上一篇:构建WebService通用性的几点建议   下一篇:用Web Services来整合.NET和J2EE
相关文章列表
不用.net和其组件用asp访问webservice 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
1.33条C#、.Net经典面试题目及答案
2.VS 2005中单步调试Windows Service
3.SqlDataReader的用法
4.一个简单的WEB流程图组件
5.用XmlDocument创建XML文档
6.ExtJs与WCF之间的跨域访问
7.typeof函数与constructor属性异同
8.winForm打开及关闭处理事件一览
9.web.config文件配置
10.在.NET 3.5下,异步调用Web Service
最新推荐文章:
1.关于Debug和Release之本质区别的讨论
2.客户端访问Web Service方法的一个细节
3.如何让WebServer返回指的定XML内容
4.如何用XML保存配置设定
5.使用 XML 通过 Java 类定义 Web服务
6.XML文件读取数据绑定到DropDownList
7.WebService中使用自定义类的解决方法
8.Web Service 中的身份验证策略
9.如何使用XmlSerializer类控制串行化
10.XML Web services 基础结构
网管论坛交流:
·大家来开心一下吧---看了会很开心的东西-
·中国人不可不知道的知识
·@@小鹏◎◎小鹏同志与某位女明星亲密接触
·◎◎小鹏◎◎发现不明生物,居然正在交配
·[图文]^^^版主是什么?????
·泡论坛的女人是好女人
·做个“水性杨花”的女人
·献给mm俱乐部的所有mm
·深圳一集团企业电脑基础应用培训教程
·■■■■十一遊玩照■■■■■