当前位置: 首页 > news >正文

几种调用WebService的方法

几种调用WebService的方法

几种调用WebService的方法     选择自 Megres 的 Blog  

1.  在JavaScript中调用WebService

<script language="javascript">

function PostRequestData(URL,data){

       var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

       xmlhttp.Open("POST",URL, false);

       xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");

       xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/myService/test/isNumner");

       

       try { 

              xmlhttp.Send(data); 

              var result = xmlhttp.status;

       }

       catch(ex) {

              return("0" + ex.description + "|" + ex.number); 

       }

       if(result==200) { 

              return("1" + xmlhttp.responseText); 

       }

       xmlhttp = null;

}

 

function loadit(value){

       var url = 'http://localhost/myService/test.asmx';

       var data ;

       var r;

       

       data = '<?xml version="1.0" encoding="utf-8"?>';

       data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';

    data = data + '<soap:Body>';

    data = data + '<isNumner xmlns="http://tempuri.org/myService/test">';

    data = data + '<str>'+value+'</str>';

    data = data + '</isNumner>';

    data = data + '</soap:Body>';

    data = data + '</soap:Envelope>';

       

       r=PostRequestData(url,data);

       document.write(r);                

}

loadit('5');

</script>


 还可以使用微软的htc组件来实现,可以到这里下载: 

http://msdn.microsoft.com/workshop/author/webservice/webservice.htc


<script language="javascript">

    function timer(){

        service.useService("http://localhost/myService/test.asmx?WSDL","test");

        service.test.callService(callback,"isNumner",'gdh');

     }

    

    function callback(res){

        if (!res.error)

            time.innerText=res.value;                

    }

 </script>

 

<div id="service" style="behavior:url(webservice.htc)"></div>

<span id="time"></span>

 

2. 在Asp中

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>

<%

             Dim strxml 

        Dim str       

       

        '定义soap消息

        strxml = "<?xml version='1.0' encoding='tf-8'?>"

        strxml = strxml & "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"

        strxml = strxml & "<soap:Body> "

              strxml = strxml & "<isNumner xmlns='http://tempuri.org/myService/test'>"

        strxml = strxml & "<str>4</str>" 

        strxml = strxml & "</isNumner>"

              strxml = strxml & "</soap:Body>" 

        strxml = strxml & "</soap:Envelope>"

              

        '定义一个XML的文档对象,将手写的或者接受的XML内容转换成XML对象

        'set x = createobject("Microsoft.DOMDocument")

        '初始化XML对象

        '将手写的SOAP字符串转换为XML对象

        ' x.loadXML strxml

        '初始化http对象

        Set h = createobject( "Microsoft.XMLHTTP")

        '向指定的URL发送Post消息

        h.open "POST", "http://localhost/myService/test.asmx", False

        h.setRequestHeader "Content-Type", "text/xml"

              h.setRequestHeader "SOAPAction", "http://tempuri.org/myService/test/isNumner"

        h.send (strxml)

        While h.readyState <> 4

        Wend

        '显示返回的XML信息

        str = h.responseText

        '将返回的XML信息解析并且显示返回值

        'Set x = createobject("MSXML2.DOMDocument")

        ' x.loadXML str

        

        'str = x.childNodes(1).Text

         

         response.write(str)

 

%>

 3.在.net中

在.net中调用WebService就方便多了,没有必要自己写soap消息了,以上都是用XMLHTTP来发送WebService请求的,在.net只要添加了web引用,会自动为你创建一个代理类。然后使用代理类就像用自己定义的类一样方便。
作者Blog:http://blog.csdn.net/Megres/

相关文章:

  • [Avalon] Avalon中的Conditional Formatting.
  • 转载:码根码的悲哀
  • [非技术]“西北风”新考:原来是菜和茶!
  • 配置好IIS中的SMTP服务器
  • 印象
  • 今天在做rss解析的时候碰到一个奇怪的难题
  • Microsoft Live全景图
  • 数据仓库分析图象展示--MSChart控件
  • 常见正则表达式运算 Regex 替换字符
  • 刘邦和项羽的民营企业管理
  • 4月11日,string这差距怎么就这么大呢
  • 人生如何成功:闯荡社会的50条忠告!
  • 爱情,你在哪里?
  • Community Server系列之十:让CS2支持中文搜索
  • 激情黄健翔
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • Create React App 使用
  • CSS3 变换
  • Python连接Oracle
  • text-decoration与color属性
  • Web Storage相关
  • 如何用vue打造一个移动端音乐播放器
  • 入手阿里云新服务器的部署NODE
  • 微信小程序:实现悬浮返回和分享按钮
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (10)STL算法之搜索(二) 二分查找
  • (33)STM32——485实验笔记
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (七)Java对象在Hibernate持久化层的状态
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • (转)EXC_BREAKPOINT僵尸错误
  • .NET BackgroundWorker
  • .NET 中使用 TaskCompletionSource 作为线程同步互斥或异步操作的事件
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • /etc/apt/sources.list 和 /etc/apt/sources.list.d
  • /etc/fstab 只读无法修改的解决办法
  • @autowired注解作用_Spring Boot进阶教程——注解大全(建议收藏!)
  • [ HTML + CSS + Javascript ] 复盘尝试制作 2048 小游戏时遇到的问题
  • []串口通信 零星笔记
  • [20180129]bash显示path环境变量.txt
  • [2021ICPC济南 L] Strange Series (Bell 数 多项式exp)
  • [BUUCTF]-Reverse:reverse3解析
  • [bzoj1901]: Zju2112 Dynamic Rankings
  • [BZOJ2208][Jsoi2010]连通数
  • [C/C++] C/C++中数字与字符串之间的转换
  • [C++] cout、wcout无法正常输出中文字符问题的深入调查(1):各种编译器测试
  • [C++] 统计程序耗时