加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

[转]几种调用WebService的方法

发布时间:2020-12-17 01:18:52 所属栏目:安全 来源:网络整理
导读:1.? 在JavaScript中调用WebService ?1 script?language = " javascript " ?2 function ?PostRequestData(URL,data){ ?3 ??????? var ?xmlhttp? = ? new ?ActiveXObject( " Microsoft.XMLHTTP " ); ?4 ???????xmlhttp.Open( " POST " ,URL,? false ); ?5 ???

1.? 在JavaScript中调用WebService

?1

< script?language = " javascript " >
?2

function ?PostRequestData(URL,data){
?3

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

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

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

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

???????
?8

???????
try ?{?
?9

??????????????xmlhttp.Send(data);?
10

??????????????
var ?result? = ?xmlhttp.status;
11

???????}
12

???????
catch (ex)?{
13

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

???????}
15

???????
if (result == 200 )?{?
16

??????????????
return ( " 1 " ? + ?xmlhttp.responseText);?
17

???????}
18

???????xmlhttp?
= ? null ;
19

}
20

?
21

function ?loadit(value){
22

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

??????? var ?data?;
24

???????
var ?r;
25

???????
26

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

???????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/ " > ';
28

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

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

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

????data?
= ?data? + ?' </ isNumner > ';
32

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

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

???????
35

???????r
= PostRequestData(url,data);
36

???????document.write(r);????????????????
37

}
38

loadit('
5 ');
39

</ 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中
?1

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

< %
?3

?????????????
Dim ?strxml?
?4

????????
Dim ?str???????
?5

???????
?6

????????
' 定义soap消息
?7

????????strxml? = ? " <?xml?version='1.0'?encoding='tf-8'?>"
?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/'>"
?9

????????strxml? = ?strxml? & ? " <soap:Body>?"
10

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

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

????????strxml?
= ?strxml? & ? " </isNumner>"
13

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

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

??????????????
16

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

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

???????? ' 初始化XML对象
19

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

???????? ' ?x.loadXML?strxml
21

???????? ' 初始化http对象
22

???????? Set ?h? = ? createobject (? " Microsoft.XMLHTTP " )
23

????????
' 向指定的URL发送Post消息
24

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

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

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

????????h.send?(strxml)
28

????????
While ?h.readyState? <> ? 4
29

????????
Wend
30

????????
' 显示返回的XML信息
31

????????str? = ?h.responseText
32

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

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

???????? ' ?x.loadXML?str
35

????????
36

????????
' str?=?x.childNodes(1).Text
37

?????????
38

?????????response.write(str)
39

?
40

%
>
?3.在.net中 在.net中调用WebService就方便多了,没有必要自己写soap消息了,以上都是用XMLHTTP来发送WebService请求的,在.net只要添加了web引用,会自动为你创建一个代理类。然后使用代理类就像用自己定义的类一样方便。 ? 在MSDN里也有: ms-help://MS.MSDNQTR.2003FEB.2052/behavior/workshop/author/webservice/reference/methods/callservice.htm 以下是userService和callService方法的详细说明:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读