您的位置:首页 > 其它

webservice学习(一)

2018-03-28 17:45 387 查看
一、webservice介绍  
 Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。
   1.使用场景
      - 不同的移动客户端访问
      - 需要访问第三方的项目 
   2.访问第三方应用的方式
      ISO的七层模型  : 物理层、数据链路层、网络层、传输层、表示层、会话层、应用层
     - Socket访问  : Socket属于传输层,它是对Tcp/ip协议的实现,包含TCP/UDP,它是所有通信协议
                       的基础,Http协议需要Socket支持,以Socket作为基础
        Socket通信特点:
  1. 开启端口,该通信是 长连接的通信 ,很容易被防火墙拦截,可以通过心跳机制
               来实现 ,开发难度片段
  2. 传输的数据一般是字符串 ,可读性不强
            lj|16|1|60|up  
          3. socket端口不便于推广 
     http:17.23.23.2:2345   www.jd.com   www.360buy.com
  4. 性能相对于其他的通信协议是最优的
     - Http协议访问 :属于应用层的协议,对Socket进行了封装
          1. 跨平台 
  2. 传数据不够友好 : 
     get请求: http://127.0.0.1:8888?username=lj&pwd=1234   3. 对第三方应用提供的服务,希望对外暴露服务接口    
     问题:
       1. 数据封装不够友好 :可以用xml封装数据 
       2. 希望给第三方应用提供web方式的服务  (http + xml) = web  Service
          
     - Webservice
       1.webService介绍
       2.webservice的规则
         - xml 
- soap :(simple object access 协议) :简单对象访问协议
- wsdl : webservice描述语言 ,它也是xml实现的
       3.webservice专业名称
         1. xml
         2. wsdl

3. soap 
二.实际列子
        1.在使用wsimport之前,我们需要先配好jdk运行的环境变量(详情可见我另外一篇文章)然后在cmd.exe下运行wsimport -s ./ -p com.ys.wc                    http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL得到接口文件然后将其添加在eclipse项目中。This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://WebXml.com.cn/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://WebXml.com.cn/">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<a href="http://www.webxml.com.cn/" target="_blank">WebXml.com.cn</a> <strong>国内手机号码归属地查询WEB服务</strong>,提供最新的国内手机号码段归属地数据,每月更新。<br />使用本站 WEB 服务请注明或链接本站:<a href="http://www.webxml.com.cn/" target="_blank">http://www.webxml.com.cn/</a> 感谢大家的支持!<br /> 
</wsdl:documentation>
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://WebXml.com.cn/">
<s:element name="getMobileCodeInfo">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="mobileCode" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="userID" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getMobileCodeInfoResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getMobileCod
4000
eInfoResult" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getDatabaseInfo">
<s:complexType/>
</s:element>
<s:element name="getDatabaseInfoResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getDatabaseInfoResult" type="tns:ArrayOfString"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfString">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="s:string"/>
</s:sequence>
</s:complexType>
<s:element name="string" nillable="true" type="s:string"/>
<s:element name="ArrayOfString" nillable="true" type="tns:ArrayOfString"/>
</s:schema>
</wsdl:types>
<wsdl:message name="getMobileCodeInfoSoapIn">
<wsdl:part name="parameters" element="tns:getMobileCodeInfo"/>
</wsdl:message>
<wsdl:message name="getMobileCodeInfoSoapOut">
<wsdl:part name="parameters" element="tns:getMobileCodeInfoResponse"/>
</wsdl:message>
<wsdl:message name="getDatabaseInfoSoapIn">
<wsdl:part name="parameters" element="tns:getDatabaseInfo"/>
</wsdl:message>
<wsdl:message name="getDatabaseInfoSoapOut">
<wsdl:part name="parameters" element="tns:getDatabaseInfoResponse"/>
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpGetIn">
<wsdl:part name="mobileCode" type="s:string"/>
<wsdl:part name="userID" type="s:string"/>
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpGetOut">
<wsdl:part name="Body" element="tns:string"/>
</wsdl:message>
<wsdl:message name="getDatabaseInfoHttpGetIn"/>
<wsdl:message name="getDatabaseInfoHttpGetOut">
<wsdl:part name="Body" element="tns:ArrayOfString"/>
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpPostIn">
<wsdl:part name="mobileCode" type="s:string"/>
<wsdl:part name="userID" type="s:string"/>
</wsdl:message>
<wsdl:message name="getMobileCodeInfoHttpPostOut">
<wsdl:part name="Body" element="tns:string"/>
</wsdl:message>
<wsdl:message name="getDatabaseInfoHttpPostIn"/>
<wsdl:message name="getDatabaseInfoHttpPostOut">
<wsdl:part name="Body" element="tns:ArrayOfString"/>
</wsdl:message>
<wsdl:portType name="MobileCodeWSSoap">
<wsdl:operation name="getMobileCodeInfo">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>获得国内手机号码归属地省份、地区和手机卡类型信息</h3><p>输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID) 免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:getMobileCodeInfoSoapIn"/>
<wsdl:output message="tns:getMobileCodeInfoSoapOut"/>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>获得国内手机号码归属地数据库信息</h3><p>输入参数:无;返回数据:一维字符串数组(省份 城市 记录数量)。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:getDatabaseInfoSoapIn"/>
<wsdl:output message="tns:getDatabaseInfoSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:portType name="MobileCodeWSHttpGet">
<wsdl:operation name="getMobileCodeInfo">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>获得国内手机号码归属地省份、地区和手机卡类型信息</h3><p>输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID) 免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:getMobileCodeInfoHttpGetIn"/>
<wsdl:output message="tns:getMobileCodeInfoHttpGetOut"/>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>获得国内手机号码归属地数据库信息</h3><p>输入参数:无;返回数据:一维字符串数组(省份 城市 记录数量)。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:getDatabaseInfoHttpGetIn"/>
<wsdl:output message="tns:getDatabaseInfoHttpGetOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:portType name="MobileCodeWSHttpPost">
<wsdl:operation name="getMobileCodeInfo">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>获得国内手机号码归属地省份、地区和手机卡类型信息</h3><p>输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID) 免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:getMobileCodeInfoHttpPostIn"/>
<wsdl:output message="tns:getMobileCodeInfoHttpPostOut"/>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>获得国内手机号码归属地数据库信息</h3><p>输入参数:无;返回数据:一维字符串数组(省份 城市 记录数量)。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:getDatabaseInfoHttpPostIn"/>
<wsdl:output message="tns:getDatabaseInfoHttpPostOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="MobileCodeWSSoap" type="tns:MobileCodeWSSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getMobileCodeInfo">
<soap:operation soapAction="http://WebXml.com.cn/getMobileCodeInfo" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<soap:operation soapAction="http://WebXml.com.cn/getDatabaseInfo" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="MobileCodeWSSoap12" type="tns:MobileCodeWSSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getMobileCodeInfo">
<soap12:operation soapAction="http://WebXml.com.cn/getMobileCodeInfo" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<soap12:operation soapAction="http://WebXml.com.cn/getDatabaseInfo" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="MobileCodeWSHttpGet" type="tns:MobileCodeWSHttpGet">
<http:binding verb="GET"/>
<wsdl:operation name="getMobileCodeInfo">
<http:operation location="/getMobileCodeInfo"/>
<wsdl:input>
<http:urlEncoded/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<http:operation location="/getDatabaseInfo"/>
<wsdl:input>
<http:urlEncoded/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="MobileCodeWSHttpPost" type="tns:MobileCodeWSHttpPost">
<http:binding verb="POST"/>
<wsdl:operation name="getMobileCodeInfo">
<http:operation location="/getMobileCodeInfo"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getDatabaseInfo">
<http:operation location="/getDatabaseInfo"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MobileCodeWS">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<a href="http://www.webxml.com.cn/" target="_blank">WebXml.com.cn</a> <strong>国内手机号码归属地查询WEB服务</strong>,提供最新的国内手机号码段归属地数据,每月更新。<br />使用本站 WEB 服务请注明或链接本站:<a href="http://www.webxml.com.cn/" target="_blank">http://www.webxml.com.cn/</a> 感谢大家的支持!<br /> 
</wsdl:documentation>
<wsdl:port name="MobileCodeWSSoap" binding="tns:MobileCodeWSSoap">
<soap:address location="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"/>
</wsdl:port>
<wsdl:port name="MobileCodeWSSoap12" binding="tns:MobileCodeWSSoap12">
<soap12:address location="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"/>
</wsdl:port>
<wsdl:port name="MobileCodeWSHttpGet" binding="tns:MobileCodeWSHttpGet">
<http:address location="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"/>
</wsdl:port>
<wsdl:port name="MobileCodeWSHttpPost" binding="tns:MobileCodeWSHttpPost">
<http:address location="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>上面这个文件经wsimport解析后会得到如下类:



分析上面这个wsdl文件


编写Test类:package cn.it.ws.c;

import java.util.List;

public class _Main {
/**通过wsimport生成本地代理 来访问 webservice服务端
*
* @param args
*/
public static void main(String[] args) {
//生成服务对象
MobileCodeWS ws=new MobileCodeWS();
//取得webservice服务的访问方式 : Soap1.1 Soap 1.2 Http-get http-Post
MobileCodeWSSoap mobileCodeWSSoap = ws.getMobileCodeWSSoap();
/**
* 返回的数据有两种类型 :
* 1. 简单的数据类型 。基本数据类型 :整数、布尔、字符串 等
* 2. 复合的数据类型 :结构体 ,数组 ,对象
*/
//1.简单的数据
String result=mobileCodeWSSoap.getMobileCodeInfo("1898384748", "");
System.out.println("返回的结果:"+result);
//2. 复合的数据 List<String> List<Student>
ArrayOfString databaseInfo = mobileCodeWSSoap.getDatabaseInfo();
List<String> results=databaseInfo.getString();
//遍历集合
for(String temp:results){
System.out.println(temp);
}
}

}得到结果:
得到的返回结果:13512375910:重庆 重庆 重庆移动全球通卡
全部 数据 265903
安徽 安庆 658
安徽 蚌埠 456
安徽 亳州 489
安徽 黄山 256
安徽 六安 632
安徽 马鞍山 390
安徽 宿州 607
福建 莆田 597
福建 泉州 2038
福建 三明 501
福建 厦门 1221
福建 漳州 925
甘肃 白银 277
甘肃 定西 351
甘肃 甘南 138
甘肃 嘉峪关 79
甘肃 金昌 213
........
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  webservice wsdl wsimport