webservice实验报告.docx
《webservice实验报告.docx》由会员分享,可在线阅读,更多相关《webservice实验报告.docx(22页珍藏版)》请在冰豆网上搜索。
![webservice实验报告.docx](https://file1.bdocx.com/fileroot1/2023-7/10/5d83f17c-0f34-4071-a344-77e3c9c5b154/5d83f17c-0f34-4071-a344-77e3c9c5b1541.gif)
webservice实验报告
Webservice课程设计报告
1.手机号码归属地服务
(一)WSDL文档:
-types>
-schematargetNamespace="elementFormDefault="qualified">
-elementname="GetMobileOwnership">
-complexType>
-sequence>
elementname="mobile"type="s:
string"maxOccurs="1"minOccurs="0"/>
elementname="authkey"type="s:
string"maxOccurs="1"minOccurs="0"/>
sequence>
complexType>
element>
-elementname="GetMobileOwnershipResponse">
-complexType>
-sequence>
elementname="GetMobileOwnershipResult"type="tns:
ResultOfMobileOwnership"maxOccurs="1"minOccurs="0"/>
sequence>
complexType>
element>
-complexTypename="ResultOfMobileOwnership">
-sequence>
elementname="status"type="s:
int"maxOccurs="1"minOccurs="1"/>
elementname="message"type="s:
string"maxOccurs="1"minOccurs="0"/>
elementname="data"type="tns:
MobileOwnership"maxOccurs="1"minOccurs="0"/>
sequence>
complexType>
-complexTypename="MobileOwnership">
-sequence>
elementname="province"type="s:
string"maxOccurs="1"minOccurs="0"/>
elementname="city"type="s:
string"maxOccurs="1"minOccurs="0"/>
elementname="areaCode"type="s:
string"maxOccurs="1"minOccurs="0"/>
elementname="postCode"type="s:
string"maxOccurs="1"minOccurs="0"/>
elementname="corp"type="s:
string"maxOccurs="1"minOccurs="0"/>
elementname="card"type="s:
string"maxOccurs="1"minOccurs="0"/>
sequence>
complexType>
elementname="ResultOfMobileOwnership"type="tns:
ResultOfMobileOwnership"nillable="true"/>
schema>
types>
1.Types-数据类型定义的容器,它使用某种类型系统(一般地使用XMLSchema中的类型系统)。
2.当elementFormDefault="qualified"时,所有全局元素的子元素将被以缺省方式放到目标命名空间,但是全局元素或者类型将被放到目标命名空间
3.authkey设置的参数可通过url登录,免费网站设置为空。
4.该部分抽象定义了四个元素,GetMobileOwnership、GetMobileOwnershipResponse、ResultOfMobileOwnership、MobileOwnership
5.GetMobileOwnership定义了一个复杂类型,其中包含两个字符串,用来描述操作的参入传入部分。
6.GetMobileOwnershipResponse:
定义了一个复杂类型,包含了一个ResultOfMobileOwnership类型的数据,将来用来描述操作的返回值。
7.ResultOfMobileOwnership:
定义了一个复杂类型,包含了一个int数据返回http状态码,一个string数据返回是否请求成功,一个MobileOwnership数据返回手机号码各项信息。
8.MobileOwnership:
定义了一个复杂类型,包含五个字符串。
plex和element映射到元素方式是不同的,type需要定制类型映射来处理complex类型,element则不需要。
plexType元素定义了一个复杂类型。
如果一个元素有属性和/或子元素,那么就认为该元素的类型是复杂类型。
DVD类型既有属性又有子元素。
元素的子元素可以遵循三种模型:
any、choice和sequence。
类型any意味着任何嵌套内容都可以以任何顺序显示成父节点的子节点。
类型choice意味着只可以使用一组可能的子元素中的一个。
最后,sequence意味着子元素在父元素中的显示顺序必须和它们在sequence元素中的顺序相同。
-messagename="GetMobileOwnershipSoapIn">
partname="parameters"element="tns:
GetMobileOwnership"/>
message>
-messagename="GetMobileOwnershipSoapOut">
partname="parameters"element="tns:
GetMobileOwnershipResponse"/>
message>
-messagename="GetMobileOwnershipHttpGetIn">
partname="mobile"type="s:
string"/>
partname="authkey"type="s:
string"/>
message>
-messagename="GetMobileOwnershipHttpGetOut">
partname="Body"element="tns:
ResultOfMobileOwnership"/>
message>
-messagename="GetMobileOwnershipHttpPostIn">
partname="mobile"type="s:
string"/>
partname="authkey"type="s:
string"/>
message>
-messagename="GetMobileOwnershipHttpPostOut">
partname="Body"element="tns:
ResultOfMobileOwnership"/>
message>
1.message标签:
方法的输入输出参数都用一个消息来表示,message标签表示一个这样的消息,message标签按下面有个part标签,用来具体指示这个消息在schema中的类型,类型以element形式表现出来,即part标签的element属性指定的那个element。
对于输入参数消息,part标签的element属性命名同webmethod方法名。
对于输出参数消息,part标签的element属性命名同webmethod方法名 +response。
表示类型的element都被集中放置在types标签内。
2.抽象定义了六个消息GetMobileOwnershipSoapIn、GetMobileOwnershipSoapOut、GetMobileOwnershipHttpGetIn、GetMobileOwnershipHttpGetOut、GetMobileOwnershipHttpPostIn、GetMobileOwnershipHttpPostOut
3.GetMobileOwnershipSoapIn,GetMobileOwnershipHttpGetIn,GetMobileOwnershipHttpPostIn方法输入消息类型,对应到schema的element,这个element会在wsdl:
types标签中描述。
4.GetMobileOwnershipSoapOut,GetMobileOwnershipHttpGetOut,GetMobileOwnershipHttpPostOut方法输入消息类型,对应到schema的element,这个element会在wsdl:
types标签中描述。
-portTypename="MobileServiceSoap">
-operationname="GetMobileOwnership">
documentationxmlns:
wsdl="http:
//schemas.xmlsoap.org/wsdl/">
手机归属地
documentation>
inputmessage="tns:
GetMobileOwnershipSoapIn"/>
outputmessage="tns:
GetMobileOwnershipSoapOut"/>
operation>
portType>
-portTypename="MobileServiceHttpGet">
-operationname="GetMobileOwnership">
documentationxmlns:
wsdl="http:
//schemas.xmlsoap.org/wsdl/">
手机归属地
documentation>
inputmessage="tns:
GetMobileOwnershipHttpGetIn"/>
outputmessage="tns:
GetMobileOwnershipHttpGetOut"/>
operation>
portType>
-portTypename="MobileServiceHttpPost">
-operationname="GetMobileOwnership">
documentationxmlns:
wsdl="http:
//schemas.xmlsoap.org/wsdl/">
手机归属地
documentation>
inputmessage="tns:
GetMobileOwnershipHttpPostIn"/>
outputmessage="tns:
GetMobileOwnershipHttpPostOut"/>
operation>
portType>
1.portType标签用来描述整个的webservices,portType的name属性即为这个webservices类的类名。
这个标签下包含了所有的可用方法,每个operation标签表示一个方法。
2.portType元素和operation元素类似于java接口和接口中定义的方法声明。
operation元素使用一个或者多个message类型来定义他的输入和输出的有效负载。
3.该操作是一个请求/响应模式,请求消息是前面定义的GetMobileOwnershipSoapIn、GetMobileOwnershipHttpGetIn、GetMobileOwnershipHttpPostIn。
响应消息是前面定义的GetMobileOwnershipSoapOut、GetMobileOwnershipHttpGetOut、GetMobileOwnershipHttpPostOut。
4.Input和output标签分别表示一个operation(webmethod方法)的输入和输出的参数集合,这里叫做消息,不管输入参数有几个,每个参数有多么复杂,只有一个表示这些输入参数的消息,就是input标签的message属性表示的那个消息。
对于输出消息也一样。
-bindingname="MobileServiceSoap"type="tns:
MobileServiceSoap">
bindingtransport="http:
//schemas.xmlsoap.org/soap/http"/>
-operationname="GetMobileOwnership">
operationstyle="document"soapAction="
-input>
bodyuse="literal"/>
input>
-output>
bodyuse="literal"/>
output>
operation>
binding>
-bindingname="MobileServiceSoap12"type="tns:
MobileServiceSoap">
bindingtransport="http:
//schemas.xmlsoap.org/soap/http"/>
-operationname="GetMobileOwnership">
operationstyle="document"soapAction="
-input>
bodyuse="literal"/>
input>
-output>
bodyuse="literal"/>
output>
operation>
binding>
-bindingname="MobileServiceHttpGet"type="tns:
MobileServiceHttpGet">
bindingverb="GET"/>
-operationname="GetMobileOwnership">
operationlocation="/GetMobileOwnership"/>
-input>
urlEncoded/>
input>
-output>
mimeXmlpart="Body"/>
output>
operation>
binding>
-bindingname="MobileServiceHttpPost"type="tns:
MobileServiceHttpPost">
bindingverb="POST"/>
-operationname="GetMobileOwnership">
operationlocation="/GetMobileOwnership"/>
-input>
contenttype="application/x-www-form-urlencoded"/>
input>
-output>
mimeXmlpart="Body"/>
output>
operation>
binding>
1.Binding元素将portType元素和operation元素赋给一个特殊的协议和编码样式。
2.这段xml定义的操作使用的是SoapDocumentProtocol消息格式(style="document")。
输入和输出参数格式都是“Literal”(use="literal")
-servicename="MobileService">
-portname="MobileServiceSoap"binding="tns:
MobileServiceSoap">
addresslocation="
port>
-portname="MobileServiceSoap12"binding="tns:
MobileServiceSoap12">
addresslocation="
port>
-portname="MobileServiceHttpGet"binding="tns:
MobileServiceHttpGet">
addresslocation="
port>
-portname="MobileServiceHttpPost"binding="tns:
MobileServiceHttpPost">
addresslocation="
port>
service>
definitions>
1.service:
service定义了某个特定绑定(binding)的连接信息。
服务可以有一个或多个端口,每个端口都定义一个不同的连接方法(例如HTTP/SMTP等等)。
2.service---相关port元素的集合,用户组织endpoint定义。
port--通过binding和物理地址定义的endpoint,这个元素将所有抽象定义聚集在一起。
3.这部分是具体的Web服务的定义,在这个名为MobileService的Web服务中,提供了一个服务访问入口,访问地址是"
(2)创建java客户端
主要代码如下:
publicclassMobilePhone{
publicstaticvoidmain(String[]args){
MobileServiceservice=newMobileService();
MobileServiceSoapport=service.getMobileServiceSoap();
System.out.println("请输入号码\n");
Scannersc=newScanner(System.in);
Stringa=sc.nextLine();
ResultOfMobileOwnershipresult=port.getMobileOwnership(a,"8a139c9597ce498380fcea89d5d2723c");
//userid一天一更新
MobileOwnershipdata=result.getData();
System.out.print("省份:
"+data.getProvince()+"\n");
System.out.print("城市:
"+data.getCity()+"\n");
System.out.print("运营商:
"+data.getCorp()+"\n");
}
}
效果如下
(3)创建网页客户端
主要代码如下:
index.jsp:
<%@pageimport="com._36wu.ResultOfMobileOwnership"%>
<%@pageimport="com._36wu.MobileServiceSoap"%>
<%@pageimport="com._36wu.MobileService"%>
<%@pagecontentType="text/html"pageEncoding="UTF-8"%>
DOCTYPEhtml>
JSPPage
查询结果
<%--startwebserviceinvocation--%>
<%
MobileServiceservice=newMobileService();
MobileServiceSoapport=service.getMobileServiceSoap();
String[]mynumber=request.getParameterValues("number");
java.lang.StringPhoneNumber=mynumber[0];
java.lang.StringUserId="8a139c9597ce498380fcea89d5d2723c";
ResultOfMobileOwnershipresult=port.getMobileOwnership(PhoneNumber,UserId);
com._36wu.MobileOwnershipdata=result.getData();
%>