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

WebService:客户端调用service的四种方式

发布时间:2020-12-16 22:27:49 所属栏目:安全 来源:网络整理
导读:l? 通过wsimport生成客户端代码【封装底层,调用的时候根本都不需要知道SOAP的存在】,由于简单减少出错,所以推荐使用,查看其源码知道他其实就是在客户端编程的基础上搞了一层,底层也是使用客户端编程的方式。 l? 通过客户端编程的方式调用 【需要明白一

l? 通过wsimport生成客户端代码【封装底层,调用的时候根本都不需要知道SOAP的存在】,由于简单减少出错,所以推荐使用,查看其源码知道他其实就是在客户端编程的基础上搞了一层,底层也是使用客户端编程的方式。

l? 通过客户端编程的方式调用 【需要明白一些概念如Qname、WSDL、Service等】

l? 通过ajax调用js+XML【实现在网页中自动调用,属于客户端自己调用的方式,利用ajax和XML解析技术】

l? 通过URLConnection调用【比较底层的方式,比较通用,当然更底层的方式是使用Socket,涉及HTTP、SOAP、XML解析】

l? 不管是哪种方式,最终都是构建HTTP+SOAP协议,只是对调用者表现为不一样。

客户端编程的方式

URL wsdlUrl = newURL("http://192.168.1.100:6789/hello?wsdl");//WSDL文档地址

?????????????????? Service s =Service.create(wsdlUrl,new

//Service的构造方法阿是protected,使用静态工厂方法创建

QName("http://ws.itcast.cn/","HelloServiceService"));

//WSDL来,因为WSDL中有很多Service,需要你指定一个

?????????????????? HelloServicehs = s.getPort(newQName("http://ws.itcast.cn/","HelloServicePort"),HelloService.class);//HelloService来源于wsimport

?????????????????? String ret =hs.sayHello("zhangsan");

?????????????????? System.out.println(ret);

?

js+XML

SOAP的具体协议内容可由MyEclipse的Webservice Explorer知道


<html>

???????? <head>

?????????????????? <title>通过ajax调用WebService服务</title>

?????????????????? <script>

???????????????????????????

??????????????????????????? varxhr = new ActiveXObject("Microsoft.XMLHTTP");

??????????????????????????? functionsendMsg(){

???????????????????????????????????? varname = document.getElementById('name').value;

???????????????????????????????????? //服务的地址

???????????????????????????????????? varwsUrl = 'http://192.168.1.100:6789/hello';

????????????????????????????????????

???????????????????????????????????? //请求体

???????????????????????????????????? varsoap = '<soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:q0="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +

??????????????????????????????????????????????????????????????????????????????????? ?' <soapenv:Body><q0:sayHello><arg0>'+name+'</arg0>? </q0:sayHello> </soapenv:Body></soapenv:Envelope>';

??????????????????????????????????????????????????????????????????????????????????? ?

???????????????????????????????????? //打开连接

???????????????????????????????????? xhr.open('POST',wsUrl,true);

????????????????????????????????????

???????????????????????????????????? //重新设置请求头

???????????????????????????????????? xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8");

????????????????????????????????????

???????????????????????????????????? //设置回调函数

???????????????????????????????????? xhr.onreadystatechange= _back;

????????????????????????????????????

???????????????????????????????????? //发送请求

???????????????????????????????????? xhr.send(soap);

??????????????????????????? }

???????????????????????????

??????????????????????????? function_back(){

???????????????????????????????????? if(xhr.readyState== 4){

?????????????????????????????????????????????? if(xhr.status== 200){

???????????????????????????????????????????????????????????????? //alert('调用Webservice成功了');

???????????????????????????????????????????????????????????????? varret = xhr.responseXML;

???????????????????????????????????????????????????????????????? varmsg = ret.getElementsByTagName('return')[0];

???????????????????????????????????????????????????????????????? document.getElementById('showInfo').innerHTML= msg.text;

???????????????????????????????????????????????????????????????? //alert(msg.text);

??????????????????????????????????????????????????????? }

???????????????????????????????????? }

??????????????????????????? }

???????? ???????? </script>

???????? </head>

???????? <body>

??????????????????????????? <inputtype="button" value="发送SOAP请求"onclick="sendMsg();">

??????????????????????????? <inputtype="text" id="name">

??????????????????????????? <divid="showInfo">

??????????????????????????? </div>

???????? </body>

</html>

?

URLConnection

URL url = newURL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");?????????

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");

conn.setDoInput(true);

conn.setDoOutput(true);

conn.setRequestProperty("Content-Type","text/xml;charset=UTF-8");

OutputStream out = conn.getOutputStream();

String soap = "<soap:Envelopexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">"

+ "<soap:Body><getMobileCodeInfo xmlns="http://WebXml.com.cn/"><mobileCode>13436823445</mobileCode><userID></userID>"

+"</getMobileCodeInfo></soap:Body></soap:Envelope>";

out.write(soap.getBytes());

int code = conn.getResponseCode();

if (code == 200) {

InputStream is = conn.getInputStream();

byte[] b = new byte[1024];

int len = 0;

StringBuffer sb = new StringBuffer();

while ((len = is.read(b)) != -1) {

String s = new String(b,len,"UTF-8");

sb.append(s);

}

System.out.println(sb);//输出的是XML,你需要解析XML中的数据得到你想要的数据

}

conn.disconnect();

(编辑:李大同)

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

    推荐文章
      热点阅读