杨桃ID:possible_Y
29258次访问,排名3898好友2人,关注者14
An easy-going guy
possible_Y的文章
原创 19 篇
翻译 0 篇
转载 0 篇
评论 23 篇
Possible的公告
最近评论
hdnero:wow gold
Vista_Jktang:关注!!!
hansenfox:很好的一项技术: 谢谢楼主 ! 还想看




我的体育用品网店:

http://shop33269681.taobao.com
陈建华:有人知道委托的Invoke、beginInvok、endInvok方法的机制吗。
Jack:有点问题,
(1).XMLSocket的方法:

①. connect(服务器地址,端口号) :尝试联接远程计算机
这个方法中第一个参数是不支持非Flash源下载地(IIS服务器)所在的IP的socket连接的,有什么好的解决办法吗?

例外:flash所在的IIS服务器IP为 220.10.10.1那当我从192.168.1.1连接上去后,我……
文章分类
收藏
    相册
    Personal
    Forums
    ASP.NET Forums
    WebSite
    15seconds
    4guysfromrolla
    CodeProject
    devx.com
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 使用vbscript脚本调用web服务收藏

    新一篇: 改进 ASP 应用程序中的字符串处理性能 | 旧一篇: 从 SOAP Toolkit 迁移到 Web 服务

        最近碰到的一个问题,需要在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

      [WebMethod(Description="返回两整数之和")]
      public int add(int x,int y)
      {
       return x+y;
      }
     }
    }

    发表于 @ 2004年02月18日 23:39:00|评论(loading...)|编辑

    新一篇: 改进 ASP 应用程序中的字符串处理性能 | 旧一篇: 从 SOAP Toolkit 迁移到 Web 服务

    评论:没有评论。

    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © Possible