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

转载:http://blog.csdn.net/csdn_gia/article/details/54863549

web服务网址:http://www.webxml.com.cn/zh_cn/web_services.aspx

1. 生成客户端调用方式
注意:该种方式使用简单,但一些关键的元素在代码生成时写死到生成代码中,不方便维护,所以仅用于测试。
(1)Wsimport命令介绍
Wsimport就是jdk(1.6版本之后)提供的的一个工具,他的作用就是根据WSDL地址生成客户端代码;
Wsimport位置JAVA_HOME/bin
Wsimport常用的参数:
-s,生成java文件的
-d,生成class文件的,默认的参数
-p,指定包名的,如果不加该参数,默认包名就是wsdl文档中的命名空间的倒序;
Wsimport仅支持SOAP1.1客户端的生成;

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

第一步:wsimport生成客户端代码
wsimport -p cn.itcast.mobile -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
第二步:阅读使用说明书(wsdl文档),使用生成的客户端代码调用服务端

package cn.itcast.mobile.client;

import cn.itcast.mobile.MobileCodeWS;
import cn.itcast.mobile.MobileCodeWSSoap;
/**
 * 
* @ClassName: MobileClient 
* @Description: TODO(公网手机号查询客户端) 
* @author 
* @date 2017年11月8日 上午8:35:02 
*
 */
public class MobileClient {
    public static void main(String[] args) {
        //创建服务访问点集合的对象,例如:<wsdl:service name="MobileCodeWS">
        MobileCodeWS mobileCodeWS = new MobileCodeWS();
        //获取服务实现类,例如:<wsdl:portType name="MobileCodeWSSoap">,port--binding--portType
        //MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getPort(MobileCodeWSSoap.class);
        //根据服务访问点的集合中的服务访问点的绑定对象来获得绑定的服务类
        //获得服务类的方式是get+服务访问点的name:getWSServerPort
        MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
        String mobileCodeInfo = mobileCodeWSSoap.getMobileCodeInfo("18518114962", "");
        System.out.println(mobileCodeInfo);
    }
}

2. service编程调用方式
注意:
(1)该种方式可以自定义关键元素,方便以后维护,是一种标准的开发方式;
(2)这种方式同样需要wsimport生成客户端代码,只不过仅需要将服务接口类引入即可,例如如果需要<wsdl:port name="MobileCodeWSSoap" binding="tns:MobileCodeWSSoap">端口服务,则需要将生成的MobileCodeWSSoap.class类引入;

package cn.itcast.mobile.client;  

import java.io.IOException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import cn.itcast.mobile.MobileCodeWSSoap;  

/** 
 *  
 *  Title: ServiceClient.java
 *  Description:Service编程实现服务端调用
 *  这种方式同样需要wsimport生成客户端代码,只不过仅需要将服务接口类引入即可,例如如果需要
 *  <wsdl:port name="MobileCodeWSSoap" binding="tns:MobileCodeWSSoap">
 *  端口服务,则需要将生成的MobileCodeWSSoap.class类引入
 */  
public class ServiceClient {  

    public static void main(String[] args) throws IOException {  
        //创建WSDL的URL,注意不是服务地址  
        URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");  

        //创建服务名称  
        //1.namespaceURI - 命名空间地址 (wsdl文档中的targetNamespace)
        //2.localPart - 服务视图名  (wsdl文档中服务名称,例如<wsdl:service name="MobileCodeWS">)
        QName qname = new QName("http://WebXml.com.cn/", "MobileCodeWS");  

        //创建服务视图  
        //参数解释:  
        //1.wsdlDocumentLocation - wsdl地址  
        //2.serviceName - 服务名称  
        Service service = Service.create(url, qname);  
        //获取服务实现类 
        //参数解释:serviceEndpointInterface - 服务端口(wsdl文档中服务端口的name属性,例如<wsdl:port name="MobileCodeWSSoap" binding="tns:MobileCodeWSSoap">)
        MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class);  
        //调用查询方法  
        String result = mobileCodeWSSoap.getMobileCodeInfo("1866666666", "");  
        System.out.println(result);  
    }  
} 

3. HttpURLConnection调用方式

开发步骤:
第一步:创建服务地址
第二步:打开一个通向服务地址的连接
第三步:设置参数
设置POST,POST必须大写,如果不大写,报如下异常
这里写图片描述
如果不设置输入输出,会报如下异常
这里写图片描述
第四步:组织SOAP数据,发送请求
第五步:接收服务端响应,打印

package cn.itcast.mobile.client;

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.URL;

/**
 * 
* @ClassName: HttpURLConectionMode 
* @Description: TODO(通过HttpURLConnection发送http请求) 
*   sope协议,比较麻烦的是需要拼接xml格式的请求数据
* @author
* @date 2017年11月8日 上午9:18:24 
*
 */
public class HttpURLConectionMode {
    public static void main(String[] args) throws IOException {
        //第一步:创建服务地址,不是WSDL地址 
        URL url = new URL("http://ws.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", "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()); 

        //第五步:接收服务端响应,打印(xml格式数据)
        int responseCode = connection.getResponseCode();  
        if(200 == responseCode){//表示服务端响应成功
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            StringBuilder sb = null;
            try {
                is = connection.getInputStream();  
                isr = new InputStreamReader(is);  
                br = new BufferedReader(isr);  

                sb = new StringBuilder();  
                String temp = null;  
                while(null != (temp = br.readLine())){  
                    sb.append(temp);  
                }
                 System.out.println(sb.toString()); 
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                br.close();
                isr.close();
                is.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 
     */  
    public 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;  
    }  

}

4. Ajax调用方式(存在跨域问题)
跨域解决参考:http://www.cnblogs.com/zhangzt/p/5966185.html
jsonp无法实现跨域,因为webservice服务的响应格式为xml格式;

<!doctype html>  
<html lang="en">  
 <head>  
  <meta charset="UTF-8">  
  <title>Document</title>  
  <script type="text/javascript">  
    function queryMobile(){  
        //创建XMLHttpRequest对象  
        var xhr = new XMLHttpRequest();  
        //打开连接  
        xhr.open("post","http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",true);  
        //设置数据类型  
        xhr.setRequestHeader("content-type","text/xml;charset=utf-8");  
        //设置回调函数  
        xhr.onreadystatechange=function(){  
            //判断是否发送成功和判断服务端是否响应成功  
            if(4 == xhr.readyState && 200 == xhr.status){  
                alert(xhr.responseText);  
            }  
        }  
        //组织SOAP协议数据  
        var 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>"+document.getElementById("phoneNum").value+"</mobileCode>"  
              +"<userID></userID>"  
            +"</getMobileCodeInfo>"  
          +"</soap:Body>"  
        +"</soap:Envelope>";  
        alert(soapXML);  
        //发送数据  
        xhr.send(soapXML);  
    }  
  </script>  
 </head>  
 <body>  
  手机号查询:<input type="text" id="phoneNum"/> <input type="button" value="查询" onclick="javascript:queryMobile();"/>  
 </body>  
</html> 
  • 12
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值