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

WebService的四种客户端调用方式(基本)

发布时间:2020-12-16 21:41:24 所属栏目:安全 来源:网络整理
导读:公网服务地址: http://www.webxml.com.cn/zh_cn/index.aspx 一、生成客户端调用方式 1、Wsimport命令介绍 l? Wsimport就是jdk提供的的一个工具,他的作用就是根据WSDL地址生成客户端代码 l? Wsimport位置JAVA_HOME/bin l? Wsimport常用的参数: ?? -s,生成

公网服务地址:

http://www.webxml.com.cn/zh_cn/index.aspx

一、生成客户端调用方式

1、Wsimport命令介绍

l? Wsimport就是jdk提供的的一个工具,他的作用就是根据WSDL地址生成客户端代码

l? Wsimport位置JAVA_HOME/bin

l? Wsimport常用的参数:

?? -s,生成java文件的

?? -d,生成class文件的,默认的参数

?? -p,指定包名的,如果不加该参数,默认包名就是wsdl文档中的命名空间的倒序

l? Wsimport仅支持SOAP1.1客户端的生成


2、 调用公网手机号归属地查询服务

l? 第一步:wsimport生成客户端代码

wsimport -p cn.itcast.mobile -s .http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

l? 第二步:阅读使用说明书,使用生成客户端代码调用服务端

[java]? view plain ?copy
  1. package?cn.itcast.mobile.client;??
  2. ??
  3. import?cn.itcast.mobile.MobileCodeWS;??
  4. import?cn.itcast.mobile.MobileCodeWSSoap;??
  5. ??
  6. /**?
  7. ?*??
  8. ?*?<p>Title:?MobileClient.java</p>?
  9. ?*?<p>Description:公网手机号查询客户端</p>*?
  10. ?*/??
  11. public?class?MobileClient?{??
  12. ????static?void?main(String[]?args)?{??
  13. ????????//创建服务视图??
  14. ????????MobileCodeWS?mobileCodeWS?=?new?MobileCodeWS();??
  15. //获取服务实现类??
  16. ????????MobileCodeWSSoap?mobileCodeWSSoap?=?mobileCodeWS.getPort(MobileCodeWSSoap.class);??
  17. //调用查询方法??
  18. ????????String?reuslt?=?mobileCodeWSSoap.getMobileCodeInfo("13888888",?null);??
  19. ????????System.out.println(reuslt);??
  20. ????}??
  21. }??


3、公网天气服务端查询

import?java.util.List;??
  • import?cn.itcast.weather.ArrayOfString;??
  • import?cn.itcast.weather.WeatherWS;??
  • import?cn.itcast.weather.WeatherWSSoap;??
  • /**?
  • ?*??
  • ?*?<p>Title:?WeatherClient.java</p>?
  • ?*?<p>Description:公网天气查询客户端</p>?
  • class?WeatherClient?{??
  • ????????WeatherWS?weatherWS?=?new?WeatherWS();??
  • ????????WeatherWSSoap?weatherWSSoap?=?weatherWS.getPort(WeatherWSSoap.????????ArrayOfString??arrayOfString?=?weatherWSSoap.getWeather("北京",?"");??
  • ????????List<String>?list?=?arrayOfString.getString();??
  • ??????????
  • ????????for(String?str?:?list){??
  • ????????????System.out.println(str);??
  • ????????}??
  • ????}??
  • }??

  • 4、特点

    该种方式使用简单,但一些关键的元素在代码生成时写死到生成代码中,不方便维护,所以仅用于测试。


    二、service编程调用方式

    import?java.io.IOException;??
  • import?java.net.MalformedURLException;??
  • import?java.net.URL;??
  • import?javax.xml.namespace.QName;??
  • import?javax.xml.ws.Service;??
  • ?*?<p>Title:?ServiceClient.java</p>?
  • ?*?<p>Description:Service编程实现服务端调用</p>?
  • ?*/??
  • class?ServiceClient?{??
  • void?main(String[]?args)?throws?IOException?{??
  • //创建WSDL的URL,注意不是服务地址??
  • ????????URL?url?=?new?URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");??
  • ??????????
  • //创建服务名称??
  • //1.namespaceURI?-?命名空间地址??
  • //2.localPart?-?服务视图名??
  • ????????QName?qname?=?new?QName("http://WebXml.com.cn/",255);background-color:inherit;">"MobileCodeWS");??
  • //创建服务视图??
  • //参数解释:??
  • //1.wsdlDocumentLocation?-?wsdl地址??
  • //2.serviceName?-?服务名称??
  • ????????Service?service?=?Service.create(url,?qname);??
  • ????????MobileCodeWSSoap?mobileCodeWSSoap?=?service.getPort(MobileCodeWSSoap.????????String?result?=?mobileCodeWSSoap.getMobileCodeInfo("1866666666",255);background-color:inherit;">"");??
  • ????????System.out.println(result);??
  • }??

  • 特点

    该种方式可以自定义关键元素,方便以后维护,是一种标准的开发方式


    三、HttpURLConnection调用方式

    开发步骤:

    第一步:创建服务地址

    第二步:打开一个通向服务地址的连接

    第三步:设置参数

    设置POST,POST必须大写,如果不大写,报如下异常


    如果不设置输入输出,会报如下异常


    第四步:组织SOAP数据,发送请求

    第五步:接收服务端响应,打印


    import?java.io.BufferedReader;??
  • import?java.io.IOException;??
  • import?java.io.InputStream;??
  • import?java.io.InputStreamReader;??
  • import?java.io.OutputStream;??
  • import?java.net.HttpURLConnection;??
  • import?java.net.MalformedURLException;??
  • import?java.net.URL;??
  • ?*?<p>Title:?HttpClient.java</p>?
  • ?*?<p>Description:HttpURLConnection调用方式</p>?
  • class?HttpClient?{??
  • throws?IOException?{??
  • //第一步:创建服务地址,不是WSDL地址??
  • "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");??
  • //第二步:打开一个通向服务地址的连接??
  • ????????HttpURLConnection?connection?=?(HttpURLConnection)?url.openConnection();??
  • //第三步:设置参数??
  • //3.1发送方式设置:POST必须大写??
  • ????????connection.setRequestMethod("POST");??
  • //3.2设置数据格式:content-type??
  • ????????connection.setRequestProperty("content-type",255);background-color:inherit;">"text/xml;charset=utf-8");??
  • //3.3设置输入输出,因为默认新创建的connection没有读写权限,??
  • ????????connection.setDoInput(true);??
  • ????????connection.setDoOutput(true);??
  • //第四步:组织SOAP数据,发送请求??
  • ????????String?soapXML?=?getXML("15226466316");??
  • ????????OutputStream?os?=?connection.getOutputStream();??
  • ????????os.write(soapXML.getBytes());??
  • //第五步:接收服务端响应,打印??
  • int?responseCode?=?connection.getResponseCode();??
  • if(200?==?responseCode){//表示服务端响应成功??
  • ????????????InputStream?is?=?connection.getInputStream();??
  • ????????????InputStreamReader?isr?=?new?InputStreamReader(is);??
  • ????????????BufferedReader?br?=?new?BufferedReader(isr);??
  • ??????????????
  • ????????????StringBuilder?sb?=?new?StringBuilder();??
  • ????????????String?temp?=?null;??
  • ????????????while(null?!=?(temp?=?br.readLine())){??
  • ????????????????sb.append(temp);??
  • ????????????}??
  • ????????????System.out.println(sb.toString());??
  • ??????????????
  • ????????????is.close();??
  • ????????????isr.close();??
  • ????????????br.close();??
  • ????????}??
  • ????????os.close();??
  • ??????
  • ?????????*?<?xml?version="1.0"?encoding="utf-8"?>?
  • <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/">?
  • ??<soap:Body>?
  • ????<getMobileCodeInfo?xmlns="http://WebXml.com.cn/">?
  • ??????<mobileCode>string</mobileCode>?
  • ??????<userID>string</userID>?
  • ????</getMobileCodeInfo>?
  • ??</soap:Body>?
  • </soap:Envelope>?
  • ?????*?@param?phoneNum?
  • ?????*?@return?
  • ?????*/??
  • static?String?getXML(String?phoneNum){??
  • ????????String?soapXML?=?"<?xml?version="1.0"?encoding="utf-8"?>"??
  • ????????+"<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/">"??
  • ????????????+"<soap:Body>"??
  • "<getMobileCodeInfo?xmlns="http://WebXml.com.cn/">"??
  • ????????????????+"<mobileCode>"+phoneNum+"</mobileCode>"??
  • ??????????????+"<userID></userID>"??
  • "</getMobileCodeInfo>"??
  • ??????????+"</soap:Body>"??
  • "</soap:Envelope>";??
  • return?soapXML;??
  • }??

  • 四、Ajax调用方式

    [html]? copy
      <!doctype?html>??
    1. <html?lang="en">??
    2. ?head??meta?charset="UTF-8"title>Document</script?type="text/javascript"????function?queryMobile(){??
    3. ????????//创建XMLHttpRequest对象??
    4. ????????var?xhr?=?new?XMLHttpRequest();??
    5. ????????//打开连接??
    6. ????????xhr.open("post","http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",true);??
    7. ????????//设置数据类型??
    8. ????????xhr.setRequestHeader("content-type","text/xml;charset=utf-8");??
    9. ????????//设置回调函数??
    10. ????????xhr.onreadystatechange=function(){??
    11. ????????????//判断是否发送成功和判断服务端是否响应成功??
    12. ????????????if(4?==?xhr.readyState?&&?200?==?xhr.status){??
    13. ????????????????alert(xhr.responseText);??
    14. ????????????}??
    15. ????????//组织SOAP协议数据??
    16. ????????var?soapXML?=?"<?xml?version="1.0"?encoding="utf-8"?>"??
    17. ????????+"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/">"??
    18. ????????????+"soap:Body>"??
    19. getMobileCodeInfo?xmlns="http://WebXml.com.cn/"????????????????+"mobileCode>"+document.getElementById("phoneNum").value+"??????????????+"userID>getMobileCodeInfo??????????+"soap:Envelope>";??
    20. ????????alert(soapXML);??
    21. ????????//发送数据??
    22. ????????xhr.send(soapXML);??
    23. scriptbody??手机号查询:input?type="text"?id="phoneNum"/>?"button"?value="查询"?onclick="javascript:queryMobile();"/>??
    24. html>

    (编辑:李大同)

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

      推荐文章
        热点阅读