用Soap消息调用Web Services(续)
来源:互联网 发布:网络尖兵是什么 编辑:程序博客网 时间:2024/05/18 15:04
上篇《用Soap消息调用Web Services》只是简单的调用一个返回值为String的无参数WebService,这次改成调用一个参数为int型的返回值为一个类对象的WebService
服务器端WebService:
public class user implements Serializable
{
private String name;
public user()
{
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class classDemo implements Serializable
{
private user[] users;
public user[] getUsers() {
return users;
}
public void setUsers(user[] users) {
this.users = users;
}
}
public class HelloWorldServiceImpl implements IHelloWorldService
{
public classDemo GetUsersInRoom(int rID)
{
classDemo cd = new classDemo();
user[] a = new user[2];
a[0] = new user();
a[0].setName("aa");
a[1] = new user();
a[1].setName("bb");
cd.setUsers(a);
return cd;
}
}
{
private String name;
public user()
{
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class classDemo implements Serializable
{
private user[] users;
public user[] getUsers() {
return users;
}
public void setUsers(user[] users) {
this.users = users;
}
}
public class HelloWorldServiceImpl implements IHelloWorldService
{
public classDemo GetUsersInRoom(int rID)
{
classDemo cd = new classDemo();
user[] a = new user[2];
a[0] = new user();
a[0].setName("aa");
a[1] = new user();
a[1].setName("bb");
cd.setUsers(a);
return cd;
}
}
客户端代码:
public static void doSoapPost()
{
try
{
//First create the connection
SOAPConnectionFactory soapConnFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnFactory.createConnection();
//Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
//Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//Populate the Message
StreamSource preppedMsgSrc = new StreamSource(
new FileInputStream("E://soap.msg"));
soapPart.setContent(preppedMsgSrc);
//Save the message
message.saveChanges();
//Check the input
System.out.println("/nREQUEST:/n");
message.writeTo(System.out);
System.out.println();
//Send the message and get a reply
//Set the destination
String destination =
"http://localhost:8080/HelloWorld/services/HelloWorldService";
//Send the message
SOAPMessage reply = connection.call(message, destination);
// Check the output
System.out.println("/nRESPONSE:/n");
//Create the transformer
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Transformer transformer =
transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
//Close the connection
connection.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
{
try
{
//First create the connection
SOAPConnectionFactory soapConnFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnFactory.createConnection();
//Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
//Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//Populate the Message
StreamSource preppedMsgSrc = new StreamSource(
new FileInputStream("E://soap.msg"));
soapPart.setContent(preppedMsgSrc);
//Save the message
message.saveChanges();
//Check the input
System.out.println("/nREQUEST:/n");
message.writeTo(System.out);
System.out.println();
//Send the message and get a reply
//Set the destination
String destination =
"http://localhost:8080/HelloWorld/services/HelloWorldService";
//Send the message
SOAPMessage reply = connection.call(message, destination);
// Check the output
System.out.println("/nRESPONSE:/n");
//Create the transformer
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Transformer transformer =
transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
//Close the connection
connection.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
客户端Soap请求格式:
REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns: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:GetUsersInRoom xmlns:ns1="http://phinecos.cnblogs.com">
<in0 xsi:type='xsd:int'>3
</in0>
</ns1:GetUsersInRoom>
</soap:Body>
</soap:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns: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:GetUsersInRoom xmlns:ns1="http://phinecos.cnblogs.com">
<in0 xsi:type='xsd:int'>3
</in0>
</ns1:GetUsersInRoom>
</soap:Body>
</soap:Envelope>
服务器端响应结果:
REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns: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:GetUsersInRoom xmlns:ns1="http://phinecos.cnblogs.com">
<in0 xsi:type='xsd:int'>3
</in0>
</ns1:GetUsersInRoom>
</soap:Body>
</soap:Envelope>
服务器端响应结果:
RESPONSE:
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns: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:GetUsersInRoomResponse xmlns:ns1="http://phinecos.cnblogs.com"><ns1:out><users xmlns="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>
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns: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:GetUsersInRoom xmlns:ns1="http://phinecos.cnblogs.com">
<in0 xsi:type='xsd:int'>3
</in0>
</ns1:GetUsersInRoom>
</soap:Body>
</soap:Envelope>
服务器端响应结果:
RESPONSE:
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns: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:GetUsersInRoomResponse xmlns:ns1="http://phinecos.cnblogs.com"><ns1:out><users xmlns="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消息调用Web Services(续)
- 用Soap消息调用Web Services
- 使用Soap消息调用Web Services
- 如何使用Soap消息调用Web Services
- 使用Soap消息调用Web Services
- 使用Soap消息调用Web Services
- 使用Soap消息调用Web Services
- 构造Soap请求调用Web Services(三)
- 使用 Web Services Enhancements 发送带有附件的 SOAP 消息
- 运用Web Services安全机制对SOAP消息加密
- c# Web Services学习笔记(二、SOAP消息)
- c# Web Services学习笔记(三、SOAP消息交换模型)
- web services用soap头实现身份验证
- Java SOAP Web services
- SOAP及Web Services
- PHP5 soap web services
- SOAP Web Services 简介
- wed services SOAP调用实例
- OpenGL入门笔记(十二)
- C语言的变参函数设计
- OpenGL的显示列表
- OpenGL选择机制的一点讨论
- 代码分析
- 用Soap消息调用Web Services(续)
- 【转】用JDOM建立XML文件
- 【翻译】Use a bitmap as a background image
- 用Soap消息调用Web Services
- 自定义ListCtrl中设置背景图片的问题
- OpenGL中的鼠标控制
- 颜色干扰的问题解决了
- OpenGL中的混合(Blending)
- OpenGL中的光照和键盘控制