webservice--四种客户端调用方式

来源:互联网 发布:java九九乘法表下三角 编辑:程序博客网 时间:2024/05/29 13:50

Webservice的四种客户端调用方式

公网服务地址:

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
 在CODE上查看代码片派生到我的代码片
  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.   
  13.     public static void main(String[] args) {  
  14.         //创建服务视图  
  15.         MobileCodeWS mobileCodeWS = new MobileCodeWS();  
  16.         //获取服务实现类  
  17.         MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getPort(MobileCodeWSSoap.class);  
  18.         //调用查询方法  
  19.         String reuslt = mobileCodeWSSoap.getMobileCodeInfo("13888888"null);  
  20.         System.out.println(reuslt);  
  21.     }  
  22. }  


3、公网天气服务端查询

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.itcast.mobile.client;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.itcast.weather.ArrayOfString;  
  6. import cn.itcast.weather.WeatherWS;  
  7. import cn.itcast.weather.WeatherWSSoap;  
  8.   
  9. /** 
  10.  *  
  11.  * <p>Title: WeatherClient.java</p> 
  12.  * <p>Description:公网天气查询客户端</p> 
  13.  *  
  14.  */  
  15. public class WeatherClient {  
  16.   
  17.     public static void main(String[] args) {  
  18.         WeatherWS weatherWS = new WeatherWS();  
  19.         WeatherWSSoap weatherWSSoap = weatherWS.getPort(WeatherWSSoap.class);  
  20.         ArrayOfString  arrayOfString = weatherWSSoap.getWeather("北京""");  
  21.         List<String> list = arrayOfString.getString();  
  22.           
  23.         for(String str : list){  
  24.             System.out.println(str);  
  25.         }  
  26.     }  
  27. }  


4、特点

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


二、service编程调用方式

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.itcast.mobile.client;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.MalformedURLException;  
  5. import java.net.URL;  
  6.   
  7. import javax.xml.namespace.QName;  
  8. import javax.xml.ws.Service;  
  9.   
  10. import cn.itcast.mobile.MobileCodeWSSoap;  
  11.   
  12. /** 
  13.  *  
  14.  * <p>Title: ServiceClient.java</p> 
  15.  * <p>Description:Service编程实现服务端调用</p> 
  16.  *  
  17.  */  
  18. public class ServiceClient {  
  19.   
  20.     public static void main(String[] args) throws IOException {  
  21.         //创建WSDL的URL,注意不是服务地址  
  22.         URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");  
  23.           
  24.         //创建服务名称  
  25.         //1.namespaceURI - 命名空间地址  
  26.         //2.localPart - 服务视图名  
  27.         QName qname = new QName("http://WebXml.com.cn/""MobileCodeWS");  
  28.           
  29.         //创建服务视图  
  30.         //参数解释:  
  31.         //1.wsdlDocumentLocation - wsdl地址  
  32.         //2.serviceName - 服务名称  
  33.         Service service = Service.create(url, qname);  
  34.         //获取服务实现类  
  35.         MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class);  
  36.         //调用查询方法  
  37.         String result = mobileCodeWSSoap.getMobileCodeInfo("1866666666""");  
  38.         System.out.println(result);  
  39.     }  
  40. }  


特点

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


三、HttpURLConnection调用方式

开发步骤:

第一步:创建服务地址

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

第三步:设置参数

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


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


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

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


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.itcast.mobile.client;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.io.OutputStream;  
  8. import java.net.HttpURLConnection;  
  9. import java.net.MalformedURLException;  
  10. import java.net.URL;  
  11.   
  12. /** 
  13.  *  
  14.  * <p>Title: HttpClient.java</p> 
  15.  * <p>Description:HttpURLConnection调用方式</p> 
  16.  */  
  17. public class HttpClient {  
  18.   
  19.     public static void main(String[] args) throws IOException {  
  20.         //第一步:创建服务地址,不是WSDL地址  
  21.         URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");  
  22.         //第二步:打开一个通向服务地址的连接  
  23.         HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
  24.         //第三步:设置参数  
  25.         //3.1发送方式设置:POST必须大写  
  26.         connection.setRequestMethod("POST");  
  27.         //3.2设置数据格式:content-type  
  28.         connection.setRequestProperty("content-type""text/xml;charset=utf-8");  
  29.         //3.3设置输入输出,因为默认新创建的connection没有读写权限,  
  30.         connection.setDoInput(true);  
  31.         connection.setDoOutput(true);  
  32.   
  33.         //第四步:组织SOAP数据,发送请求  
  34.         String soapXML = getXML("15226466316");  
  35.         OutputStream os = connection.getOutputStream();  
  36.         os.write(soapXML.getBytes());  
  37.         //第五步:接收服务端响应,打印  
  38.         int responseCode = connection.getResponseCode();  
  39.         if(200 == responseCode){//表示服务端响应成功  
  40.             InputStream is = connection.getInputStream();  
  41.             InputStreamReader isr = new InputStreamReader(is);  
  42.             BufferedReader br = new BufferedReader(isr);  
  43.               
  44.             StringBuilder sb = new StringBuilder();  
  45.             String temp = null;  
  46.             while(null != (temp = br.readLine())){  
  47.                 sb.append(temp);  
  48.             }  
  49.             System.out.println(sb.toString());  
  50.               
  51.             is.close();  
  52.             isr.close();  
  53.             br.close();  
  54.         }  
  55.   
  56.         os.close();  
  57.     }  
  58.       
  59.     /** 
  60.      * <?xml version="1.0" encoding="utf-8"?> 
  61. <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/"> 
  62.   <soap:Body> 
  63.     <getMobileCodeInfo xmlns="http://WebXml.com.cn/"> 
  64.       <mobileCode>string</mobileCode> 
  65.       <userID>string</userID> 
  66.     </getMobileCodeInfo> 
  67.   </soap:Body> 
  68. </soap:Envelope> 
  69.      * @param phoneNum 
  70.      * @return 
  71.      */  
  72.     public static String getXML(String phoneNum){  
  73.         String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"  
  74.         +"<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/\">"  
  75.             +"<soap:Body>"  
  76.             +"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"  
  77.                 +"<mobileCode>"+phoneNum+"</mobileCode>"  
  78.               +"<userID></userID>"  
  79.             +"</getMobileCodeInfo>"  
  80.           +"</soap:Body>"  
  81.         +"</soap:Envelope>";  
  82.         return soapXML;  
  83.     }  
  84. }  

四、Ajax调用方式

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!doctype html>  
  2. <html lang="en">  
  3.  <head>  
  4.   <meta charset="UTF-8">  
  5.   <title>Document</title>  
  6.   <script type="text/javascript">  
  7.     function queryMobile(){  
  8.         //创建XMLHttpRequest对象  
  9.         var xhr = new XMLHttpRequest();  
  10.         //打开连接  
  11.         xhr.open("post","http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",true);  
  12.         //设置数据类型  
  13.         xhr.setRequestHeader("content-type","text/xml;charset=utf-8");  
  14.         //设置回调函数  
  15.         xhr.onreadystatechange=function(){  
  16.             //判断是否发送成功和判断服务端是否响应成功  
  17.             if(4 == xhr.readyState && 200 == xhr.status){  
  18.                 alert(xhr.responseText);  
  19.             }  
  20.         }  
  21.         //组织SOAP协议数据  
  22.         var soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"  
  23.         +"<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/\">"  
  24.             +"<soap:Body>"  
  25.             +"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"  
  26.                 +"<mobileCode>"+document.getElementById("phoneNum").value+"</mobileCode>"  
  27.               +"<userID></userID>"  
  28.             +"</getMobileCodeInfo>"  
  29.           +"</soap:Body>"  
  30.         +"</soap:Envelope>";  
  31.         alert(soapXML);  
  32.         //发送数据  
  33.         xhr.send(soapXML);  
  34.     }  
  35.   </script>  
  36.  </head>  
  37.  <body>  
  38.   手机号查询:<input type="text" id="phoneNum"/> <input type="button" value="查询" onclick="javascript:queryMobile();"/>  
  39.  </body>  
  40. </html>  
链接:http://blog.csdn.net/csdn_gia/article/details/54863549



0 0
原创粉丝点击