转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
使用vbscript脚本调用web服务         ★★★★

使用vbscript脚本调用web服务

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1928 更新时间:2009/4/23 18:59:02

    最近碰到的一个问题,需要在asp和客户端调用.NET的webservice,也就是说需要用vbscript或javascript来调用webservice。在网上看了看,大多数方案都是利用SOAP Toolkit,但是因为SOAP Toolkit在今年就会被停止后续的支持了,并且要使用soapclient需要专门安装SOAP Toolkit,这对客户端来说不具有通用性,因此想到了使用xmlhttp,利用xmlhttp来和webservice交互。

客户端代码如下:
<script language="vbscript">
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Set xmlDOC =CreateObject("MSXML.DOMDocument")
strWebserviceURL = "
http://localhost/possible/Service1.asmx/add"
''''设置参数及其值
strRequest = "x=2&y=3"
objHTTP.Open "POST", strWebserviceURL, False
''''设置这个Content-Type很重要
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.Send(strRequest)
bOK = xmlDOC.load(objHTTP.responseXML)
''''看看状态值
msgBox objHTTP.Status
msgbox objHTTP.StatusText
''''objHTTP.Status=200,这里就可以处理返回的xml片段了
''''如果需要,可以替换返回的xml字符串当中的&lt;和&gt;

xmlStr = xmlDOC.xml
xmlStr = Replace(xmlStr,"&lt;","<",1,-1,1)
xmlStr = Replace(xmlStr,"&gt;",">",1,-1,1)
msgbox xmlStr
</script>

改为服务器端的asp代码为:
<%
Set objHTTP = Server.CreateObject("MSXML2.XMLHTTP")
Set xmlDOC =Server.CreateObject("MSXML.DOMDocument")
strWebserviceURL = "
http://localhost/possible/Service1.asmx/add"
''''设置参数及其值
strRequest = "x=2&y=3"
objHTTP.Open "POST", strWebserviceURL, False
''''设置这个Content-Type很重要
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.Send(strRequest)
bOK = xmlDOC.load(objHTTP.responseXML)
''''看看状态值
if objHTTP.Status=200 then
xmlStr = xmlDOC.xml
xmlStr = Replace(xmlStr,"&lt;","<",1,-1,1)
xmlStr = Replace(xmlStr,"&gt;",">",1,-1,1)
  Response.Write xmlStr
else
  Response.Write objHTTP.Statu&"<br>"
  Response.Write objHTTP.StatusText
end if
%>

    以上代码在本地测试都没有问题(在部署webservice的本地机器上测试的),然而把strWebserviceURL = "http://localhost/possible/Service1.asmx/add"改为部署在其他机器上的webservice时,却出了问题,结果一直是返回500错误,即objHTTP.Status一直都为500。
    原因在于.Net Framework1.1默认不支持HttpGet和HttpPost。如果修改webservice里的web.config增加
 <webServices>
         <protocols>
                 <add name="HttpPost"/>
                 <add name="HttpGet"/>
                </protocols>
 </webServices>

后,上代码就可以调用远程机器上的webservice了。
    而利用SOAP发送在默认情况下即可得到.Net Framework1.1的支持,因此用构造Soap请求的xml字符串给xmlhttp对象来send的方法就对远程服务器的web.config没有要求了,于是根据local显示的例子构造了一个soapRequest的string,发送给了即将部署的远程主机,结果返回了200的status code,并且可以顺利取得responseXML.类似代码如下:

客户端代码如下:
<script language="vbscript">
Dim url,xmlhttp,dom,node,xmlDOC
''''根据webservice的测试页不同的方法构造不同的soap request
SoapRequest = "<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _
    "<soap:Envelope xmlns:xsi="&CHR(34)&"
http://www.w3.org/2001/XMLSchema-instance"&CHR(34)&" "& _
    "xmlns:xsd="&CHR(34)&"
http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _
    "xmlns:soap="&CHR(34)&"
http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _
    "<soap:Body>"& _
    "<add xmlns="&CHR(34)&"
http://localhost"&CHR(34)&">"& _
     "<x>3</x>"& _
     "<y>4</y>"& _
    "</add>"& _
     "</soap:Body>"& _
   "</soap:Envelope>"
url = "
http://www.xxxx.com/Service1.asmx?methodname=Add"
Set xmlDOC =CreateObject("MSXML.DOMDocument")
xmlDOC.loadXML(SoapRequest)
Set xmlhttp = CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",url,false
xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
''''SOAPAction这个Header头同样可以在sample中找到
xmlhttp.setRequestHeader "SOAPAction", "
http://localhost/add"
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.Send(xmlDOC)
msgbox xmlhttp.Status
msgbox xmlhttp.StatusText
msgbox xmlhttp.responseText
If xmlhttp.Status = 200 Then
 xmlDOC.load(xmlhttp.responseXML)
 msgbox "执行结果为:"&xmlDOC.getElementsByTagName("addResult")(0).text
else
 msgbox "failed"
end if
</script>

改为服务器端的asp代码为:
<%
Dim url,xmlhttp,dom,node,xmlDOC
''''根据webservice的测试页不同的方法构造不同的soap request
SoapRequest = "<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _
    "<soap:Envelope xmlns:xsi="&CHR(34)&"
http://www.w3.org/2001/XMLSchema-instance"&CHR(34)&" "& _
    "xmlns:xsd="&CHR(34)&"
http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _
    "xmlns:soap="&CHR(34)&"
http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _
    "<soap:Body>"& _
    "<add xmlns="&CHR(34)&"
http://localhost"&CHR(34)&">"& _
     "<x>3</x>"& _
     "<y>4</y>"& _
    "</add>"& _
     "</soap:Body>"& _
   "</soap:Envelope>"
url = "
http://www.xxxx.com/Service1.asmx?methodname=Add"
Set xmlDOC =server.CreateObject("MSXML.DOMDocument")
xmlDOC.loadXML(SoapRequest)
Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",url,false
xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
xmlhttp.setRequestHeader "SOAPAction", "
http://localhost/add"
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.Send(xmlDOC)
If xmlhttp.Status = 200 Then
 xmlDOC.load(xmlhttp.responseXML)
  Response.Write xmlhttp.Status&"<br>"
  Response.Write xmlhttp.StatusText&"<br>执行结果为:"
 Response.Write xmlDOC.getElementsByTagName("addResult")(0).text
else
  Response.Write xmlhttp.Status&"<br>"
  Response.Write xmlhttp.StatusText
end if
%>

以上用的都是vbscript的,对于javascript基本上都是一样的,只需要做一些小的改动,具体代码这里就省略了。

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

附:
测试时用的webservice文件Service1.asmx的代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace possible
{
 /// <summary>
 /// Service1 的摘要说明。
 /// </summary>
 [WebService(Description="my web service",Name="myService",Namespace="
http://localhost")]
 public class myService : System.Web.Services.WebService
 {
  public myService()
  {
   //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
   InitializeComponent();
  }

  #region 组件设计器生成的代码
  
  //Web 服务设计器所必需的
  private IContainer components = null;
    
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if(disposing && components != null)
   {
    components.Dispose();
   }
   base.Dispose(disposing);  
  }
  
  #endregion

[1] [2]  下一页


[聊天工具]Gmail推出新功能:Web Clip__天极Yesky  [聊天工具]Web MSN你玩了吗__天极Yesky
[系统软件]Web Browser Express 概述  [系统软件]对Internet Explorer Web 控件做一点修改
[常用软件]小技巧:三步实现Web迅雷录制PPLive节目  [常用软件]天网防火墙:打开WEB和FTP服务
[VB.NET程序]使用VB.Net做一个配置web.config功能的WinForm(原…  [VB.NET程序]vb.net控件、web service简述
[VB.NET程序]*** Web 存储系统窗体:窗体注册表 (new)***  [Delphi程序]李维:樂趣無窮,可能無限的新技術-Web Service
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台