`
dato0123
  • 浏览: 913482 次
文章分类
社区版块
存档分类
最新评论

用Soap消息调用Web Services(续)

 
阅读更多

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

服务器端WebService

publicclassuserimplementsSerializable
{
privateStringname;
publicuser()
{

}

publicStringgetName(){
returnname;
}

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


}


publicclassclassDemoimplementsSerializable
{
privateuser[]users;

publicuser[]getUsers(){
returnusers;
}


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

}


publicclassHelloWorldServiceImplimplementsIHelloWorldService
{

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;
}


}

客户端代码:

publicstaticvoiddoSoapPost()
{
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>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics