惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
月光博客
月光博客
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
IT之家
IT之家
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
博客园 - 司徒正美
爱范儿
爱范儿

博客园 - 骑牛射雕

Visual Studio 安装 旧版本(.NET Framework 4.0 和 4.5) 安装程序提示SWbemObjectSet:无效类处理方案 VSTO开发入门,CustomUI元素详解 线程池ThreadPool wpf combobox 绑定枚举 c# 相同类 赋值 WPF 使用Path绘制几何图形 Prism简介 WPF使用PATH来画圆 经过指定的时间后自动关闭的模式窗体 非对称加密 inno setup 安装前判断进程是否存在,以及停止相应进程 Inno Setup打包CreateProcess 740出错 C#委托与事件应用场景 X.509数字证书的结构与解析 C#action和func的使用 Oracle 分析函数(10G)语法详解 纯JS写的无刷新实时同步购物车系统 图片延时加载插件(lazyload)
几种调用WebService的方法
骑牛射雕 · 2013-04-08 · via 博客园 - 骑牛射雕

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引用,会自动为你创建一个代理类。然后使用代理类就像用自己定义的类一样方便。