上篇《用Soap消息调用Web Services》只是简单的调用一个返回值为String的无参数WebService,这次改成调用一个参数为int型的返回值为一个类对象的WebService<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
服务器端WebService:
public
class
userimplementsSerializable
{
privateStringname;
publicuser()

{
}
publicStringgetName()
{
returnname;
}
publicvoidsetName(Stringname)
{
this.name=name;
}
}

public
class
classDemoimplementsSerializable
{
privateuser[]users;

publicuser[]getUsers()
{
returnusers;
}

publicvoidsetUsers(user[]users)
{
this.users=users;
}
}

public
class
HelloWorldServiceImplimplementsIHelloWorldService
{
publicclassDemoGetUsersInRoom(intrID)

{
classDemocd=newclassDemo();
user[]a=newuser[2];
a[0]=newuser();
a[0].setName("aa");
a[1]=newuser();
a[1].setName("bb");
cd.setUsers(a);
returncd;
}
}
客户端代码:
public
static
void
doSoapPost()
{
try

{
//Firstcreatetheconnection
SOAPConnectionFactorysoapConnFactory=
SOAPConnectionFactory.newInstance();
SOAPConnectionconnection=
soapConnFactory.createConnection();
//Next,createtheactualmessage
MessageFactorymessageFactory=MessageFactory.newInstance();
SOAPMessagemessage=messageFactory.createMessage();
//Createobjectsforthemessageparts
SOAPPartsoapPart=message.getSOAPPart();
SOAPEnvelopeenvelope=soapPart.getEnvelope();
SOAPBodybody=envelope.getBody();
//PopulatetheMessage
StreamSourcepreppedMsgSrc=newStreamSource(
newFileInputStream("E://soap.msg"));
soapPart.setContent(preppedMsgSrc);
//Savethemessage
message.saveChanges();
//Checktheinput
System.out.println("/nREQUEST:/n");
message.writeTo(System.out);
System.out.println();
//Sendthemessageandgetareply
//Setthedestination
Stringdestination=
"http://localhost:8080/HelloWorld/services/HelloWorldService";
//Sendthemessage
SOAPMessagereply=connection.call(message,destination);
//Checktheoutput
System.out.println("/nRESPONSE:/n");
//Createthetransformer
TransformerFactorytransformerFactory=
TransformerFactory.newInstance();
Transformertransformer=
transformerFactory.newTransformer();
//Extractthecontentofthereply
SourcesourceContent=reply.getSOAPPart().getContent();
//Settheoutputforthetransformation
StreamResultresult=newStreamResult(System.out);
transformer.transform(sourceContent,result);
System.out.println();
//Closetheconnection
connection.close();
}
catch(Exceptione)

{
System.out.println(e.getMessage());
}
}
客户端Soap请求格式:
REQUEST:
<?
xmlversion
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<
soap:Envelopexmlns:soap
=
"
http://schemas.xmlsoap.org/soap/envelope/
"
xmlns:xsd
=
"
http://www.w3.org/2001/XMLSchema
"
xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance
"
>
<
soap:Body
>
<
ns1:GetUsersInRoomxmlns:ns1
=
"
http://phinecos.cnblogs.com
"
>
<
in0xsi:type
=
'
xsd:int
'
>
3
</
in0
>
</
ns1:GetUsersInRoom
>
</
soap:Body
>
</
soap:Envelope
>
服务器端响应结果:
REQUEST:
<?
xmlversion
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<
soap:Envelopexmlns:soap
=
"
http://schemas.xmlsoap.org/soap/envelope/
"
xmlns:xsd
=
"
http://www.w3.org/2001/XMLSchema
"
xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance
"
>
<
soap:Body
>
<
ns1:GetUsersInRoomxmlns:ns1
=
"
http://phinecos.cnblogs.com
"
>
<
in0xsi:type
=
'
xsd:int
'
>
3
</
in0
>
</
ns1:GetUsersInRoom
>
</
soap:Body
>
</
soap:Envelope
>

服务器端响应结果:
RESPONSE:
<?
xmlversion
=
"
1.0
"
encoding
=
"
UTF-8
"
?><
soap:Envelopexmlns:soap
=
"
http://schemas.xmlsoap.org/soap/envelope/
"
xmlns:xsd
=
"
http://www.w3.org/2001/XMLSchema
"
xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance
"
><
soap:Body
><
ns1:GetUsersInRoomResponsexmlns:ns1
=
"
http://phinecos.cnblogs.com
"
><
ns1:
out
><
usersxmlns
=
"
http://boomga.com
"
><
user
><
name
>
aa
</
name
><
path
>/
a.w3d
</
path
></
user
><
user
><
name
>
bb
</
name
><
path
>/
b.w3d
</
path
></
user
></
users
></
ns1:
out
></
ns1:GetUsersInRoomResponse
></
soap:Body
></
soap:Envelope
>
本文介绍如何使用SOAP消息调用带有整数参数并返回包含多个用户对象的WebService的方法。通过示例代码展示了客户端和服务端的具体实现过程。
1476

被折叠的 条评论
为什么被折叠?



